-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Allow self-closing tags in Markdown (#3516)
- Loading branch information
1 parent
939fe15
commit 3057801
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/markdown-remark': patch | ||
--- | ||
|
||
Fix: Allow self-closing tags in Markdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,50 @@ | ||
import { mdxjs } from 'micromark-extension-mdxjs'; | ||
import { mdxFromMarkdown, mdxToMarkdown } from './mdast-util-mdxish.js'; | ||
import type * as fromMarkdown from 'mdast-util-from-markdown'; | ||
import type { Tag } from 'mdast-util-mdx-jsx'; | ||
|
||
export default function remarkMdxish(this: any, options = {}) { | ||
const data = this.data(); | ||
|
||
add('micromarkExtensions', mdxjs(options)); | ||
add('fromMarkdownExtensions', mdxFromMarkdown()); | ||
add('fromMarkdownExtensions', makeFromMarkdownLessStrict(mdxFromMarkdown())); | ||
add('toMarkdownExtensions', mdxToMarkdown()); | ||
|
||
function add(field: string, value: unknown) { | ||
const list = data[field] ? data[field] : (data[field] = []); | ||
list.push(value); | ||
} | ||
} | ||
|
||
function makeFromMarkdownLessStrict(extensions: fromMarkdown.Extension[]) { | ||
extensions.forEach(extension => { | ||
// Fix exit handlers that are too strict | ||
['mdxJsxFlowTag', 'mdxJsxTextTag'].forEach(exitHandler => { | ||
if (!extension.exit || !extension.exit[exitHandler]) | ||
return; | ||
extension.exit[exitHandler] = chainHandlers( | ||
fixSelfClosing, | ||
extension.exit[exitHandler] | ||
); | ||
}); | ||
}); | ||
|
||
return extensions; | ||
} | ||
|
||
const selfClosingTags = new Set([ | ||
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'source', | ||
'track', 'wbr' | ||
]); | ||
|
||
function fixSelfClosing(this: fromMarkdown.CompileContext) { | ||
const tag = this.getData('mdxJsxTag') as Tag; | ||
if (tag.name && selfClosingTags.has(tag.name)) | ||
tag.selfClosing = true; | ||
} | ||
|
||
function chainHandlers(...handlers: fromMarkdown.Handle[]) { | ||
return function handlerChain (this: fromMarkdown.CompileContext, token: fromMarkdown.Token) { | ||
handlers.forEach(handler => handler.call(this, token)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { renderMarkdown } from '../dist/index.js'; | ||
import chai from 'chai'; | ||
|
||
describe('strictness', () => { | ||
it('should allow self-closing HTML tags (void elements)', async () => { | ||
const { code } = await renderMarkdown( | ||
`Use self-closing void elements<br>like word<wbr>break and images: <img src="hi.jpg">`, | ||
{} | ||
); | ||
|
||
chai.expect(code).to.equal( | ||
`<p>Use self-closing void elements<br />like word<wbr />break and images: ` + | ||
`<img src="hi.jpg" /></p>` | ||
); | ||
}); | ||
}); |