diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index d7c02192c..b3c916bcd 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -125,9 +125,15 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { let { data: frontmatter, content: markdownContent } = matter(source); // Turn HTML comments into JS comments - markdownContent = markdownContent.replace(/<\s*!--([^-->]*)(.*?)-->/gs, (whole) => `{/*${whole}*/}`) + markdownContent = markdownContent.replace( + /<\s*!--([^-->]*)(.*?)-->/gs, + (whole) => `{/*${whole}*/}` + ); - let renderResult = await renderMarkdown(markdownContent, { ...renderOpts, fileURL: fileUrl } as any); + let renderResult = await renderMarkdown(markdownContent, { + ...renderOpts, + fileURL: fileUrl, + } as any); let { code: astroResult, metadata } = renderResult; const { layout = '', components = '', setup = '', ...content } = frontmatter; content.astro = metadata; diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js index 0f9d28c86..525522316 100644 --- a/packages/astro/test/astro-markdown.test.js +++ b/packages/astro/test/astro-markdown.test.js @@ -39,7 +39,7 @@ describe('Astro Markdown', () => { const html = await fixture.readFile('/slug/index.html'); const $ = cheerio.load(html); - expect($('h1').attr("id")).to.equal('my-blog-post'); + expect($('h1').attr('id')).to.equal('my-blog-post'); }); it('Can handle code elements without extra spacing', async () => { @@ -47,7 +47,7 @@ describe('Astro Markdown', () => { const $ = cheerio.load(html); $('code').each((_, el) => { - expect($(el).html()).to.equal($(el).html().trim()) + expect($(el).html()).to.equal($(el).html().trim()); }); }); @@ -76,7 +76,7 @@ describe('Astro Markdown', () => { it('Can handle scripts in markdown pages', async () => { const html = await fixture.readFile('/script/index.html'); console.log(html); - expect(html).not.to.match(new RegExp("\/src\/scripts\/test\.js")); + expect(html).not.to.match(new RegExp('/src/scripts/test.js')); }); it('Can load more complex jsxy stuff', async () => { diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index d942bf7bf..5df5566a1 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -36,8 +36,15 @@ export async function renderMarkdown( content: string, opts: MarkdownRenderingOptions = {} ): Promise { - let { fileURL, mode = 'mdx', syntaxHighlight = 'shiki', shikiConfig = {}, remarkPlugins = [], rehypePlugins = [] } = opts; - const input = new VFile({ value: content, path: fileURL }) + let { + fileURL, + mode = 'mdx', + syntaxHighlight = 'shiki', + shikiConfig = {}, + remarkPlugins = [], + rehypePlugins = [], + } = opts; + const input = new VFile({ value: content, path: fileURL }); const scopedClassName = opts.$?.scopedClassName; const isMDX = mode === 'mdx'; const { headers, rehypeCollectHeaders } = createCollectHeaders(); diff --git a/packages/markdown/remark/src/mdast-util-mdxish.ts b/packages/markdown/remark/src/mdast-util-mdxish.ts index 52a99deeb..10b19f5be 100644 --- a/packages/markdown/remark/src/mdast-util-mdxish.ts +++ b/packages/markdown/remark/src/mdast-util-mdxish.ts @@ -1,18 +1,12 @@ -import { - mdxExpressionFromMarkdown, - mdxExpressionToMarkdown -} from 'mdast-util-mdx-expression' -import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'mdast-util-mdx-jsx' +import { mdxExpressionFromMarkdown, mdxExpressionToMarkdown } from 'mdast-util-mdx-expression'; +import { mdxJsxFromMarkdown, mdxJsxToMarkdown } from 'mdast-util-mdx-jsx'; export function mdxFromMarkdown(): any { - return [mdxExpressionFromMarkdown, mdxJsxFromMarkdown] + return [mdxExpressionFromMarkdown, mdxJsxFromMarkdown]; } export function mdxToMarkdown(): any { - return { - extensions: [ - mdxExpressionToMarkdown, - mdxJsxToMarkdown, - ] - } + return { + extensions: [mdxExpressionToMarkdown, mdxJsxToMarkdown], + }; } diff --git a/packages/markdown/remark/src/rehype-collect-headers.ts b/packages/markdown/remark/src/rehype-collect-headers.ts index 77126ab7e..426b782a3 100644 --- a/packages/markdown/remark/src/rehype-collect-headers.ts +++ b/packages/markdown/remark/src/rehype-collect-headers.ts @@ -31,21 +31,22 @@ export default function createCollectHeaders() { return; } } - if (child.type === 'text' || child.type === 'raw') { + if (child.type === 'text' || child.type === 'raw') { raw += child.value; text += child.value; isJSX = isJSX || child.value.includes('{'); } }); - node.properties = node.properties || {}; if (typeof node.properties.id !== 'string') { if (isJSX) { // HACK: for ids that have JSX content, use $$slug helper to generate slug at runtime node.properties.id = `$$slug(\`${text.replace(/\{/g, '${')}\`)`; (node as any).type = 'raw'; - (node as any).value = `<${node.tagName} id={${node.properties.id}}>${raw}`; + ( + node as any + ).value = `<${node.tagName} id={${node.properties.id}}>${raw}`; } else { node.properties.id = slugger.slug(text); } diff --git a/packages/markdown/remark/src/rehype-jsx.ts b/packages/markdown/remark/src/rehype-jsx.ts index 62eb977c0..8549b2624 100644 --- a/packages/markdown/remark/src/rehype-jsx.ts +++ b/packages/markdown/remark/src/rehype-jsx.ts @@ -9,36 +9,36 @@ export default function rehypeJsx(): any { } if (MDX_ELEMENTS.has(child.type)) { const attrs = child.attributes.reduce((acc: any[], entry: any) => { - let attr = entry.value; - if (attr && typeof attr === 'object') { - attr = `{${attr.value}}`; - } else if (attr && entry.type === 'mdxJsxExpressionAttribute') { - attr = `{${attr}}` - } else if (attr === null) { - attr = ""; - } else if (typeof attr === 'string') { - attr = `"${attr}"`; - } - if (!entry.name) { - return acc + ` ${attr}`; - } - return acc + ` ${entry.name}${attr ? '=' : ''}${attr}`; - }, ''); + let attr = entry.value; + if (attr && typeof attr === 'object') { + attr = `{${attr.value}}`; + } else if (attr && entry.type === 'mdxJsxExpressionAttribute') { + attr = `{${attr}}`; + } else if (attr === null) { + attr = ''; + } else if (typeof attr === 'string') { + attr = `"${attr}"`; + } + if (!entry.name) { + return acc + ` ${attr}`; + } + return acc + ` ${entry.name}${attr ? '=' : ''}${attr}`; + }, ''); if (child.children.length === 0) { return { type: 'raw', - value: `<${child.name}${attrs} />` + value: `<${child.name}${attrs} />`, }; } child.children.splice(0, 0, { type: 'raw', - value: `\n<${child.name}${attrs}>` - }) + value: `\n<${child.name}${attrs}>`, + }); child.children.push({ type: 'raw', - value: `\n` - }) + value: `\n`, + }); return { ...child, type: 'element', diff --git a/packages/markdown/remark/src/remark-mark-and-unravel.ts b/packages/markdown/remark/src/remark-mark-and-unravel.ts index 4490e4a93..294d4211d 100644 --- a/packages/markdown/remark/src/remark-mark-and-unravel.ts +++ b/packages/markdown/remark/src/remark-mark-and-unravel.ts @@ -8,7 +8,7 @@ * @typedef {import('remark-mdx')} DoNotTouchAsThisImportItIncludesMdxInTree */ -import {visit} from 'unist-util-visit' +import { visit } from 'unist-util-visit'; /** * A tiny plugin that unravels `

x

` but also @@ -19,63 +19,54 @@ import {visit} from 'unist-util-visit' * @type {import('unified').Plugin, Root>} */ export default function remarkMarkAndUnravel() { - return (tree: any) => { - visit(tree, (node, index, parent_) => { - const parent = /** @type {Parent} */ (parent_) - let offset = -1 - let all = true - /** @type {boolean|undefined} */ - let oneOrMore + return (tree: any) => { + visit(tree, (node, index, parent_) => { + const parent = /** @type {Parent} */ parent_; + let offset = -1; + let all = true; + /** @type {boolean|undefined} */ + let oneOrMore; - if (parent && typeof index === 'number' && node.type === 'paragraph') { - const children = node.children + if (parent && typeof index === 'number' && node.type === 'paragraph') { + const children = node.children; - while (++offset < children.length) { - const child = children[offset] + while (++offset < children.length) { + const child = children[offset]; - if ( - child.type === 'mdxJsxTextElement' || - child.type === 'mdxTextExpression' - ) { - oneOrMore = true - } else if ( - child.type === 'text' && - /^[\t\r\n ]+$/.test(String(child.value)) - ) { - // Empty. - } else { - all = false - break - } - } + if (child.type === 'mdxJsxTextElement' || child.type === 'mdxTextExpression') { + oneOrMore = true; + } else if (child.type === 'text' && /^[\t\r\n ]+$/.test(String(child.value))) { + // Empty. + } else { + all = false; + break; + } + } - if (all && oneOrMore) { - offset = -1 + if (all && oneOrMore) { + offset = -1; - while (++offset < children.length) { - const child = children[offset] + while (++offset < children.length) { + const child = children[offset]; - if (child.type === 'mdxJsxTextElement') { - child.type = 'mdxJsxFlowElement' - } + if (child.type === 'mdxJsxTextElement') { + child.type = 'mdxJsxFlowElement'; + } - if (child.type === 'mdxTextExpression') { - child.type = 'mdxFlowExpression' - } - } + if (child.type === 'mdxTextExpression') { + child.type = 'mdxFlowExpression'; + } + } - parent.children.splice(index, 1, ...children) - return index - } - } + parent.children.splice(index, 1, ...children); + return index; + } + } - if ( - node.type === 'mdxJsxFlowElement' || - node.type === 'mdxJsxTextElement' - ) { - const data = node.data || (node.data = {}) - data._mdxExplicitJsx = true - } - }) - } + if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') { + const data = node.data || (node.data = {}); + data._mdxExplicitJsx = true; + } + }); + }; } diff --git a/packages/markdown/remark/src/remark-mdxish.ts b/packages/markdown/remark/src/remark-mdxish.ts index b5d41d228..0ad8cbf4f 100644 --- a/packages/markdown/remark/src/remark-mdxish.ts +++ b/packages/markdown/remark/src/remark-mdxish.ts @@ -1,15 +1,15 @@ -import {mdxjs} from 'micromark-extension-mdxjs' -import { mdxFromMarkdown, mdxToMarkdown } from './mdast-util-mdxish.js' +import { mdxjs } from 'micromark-extension-mdxjs'; +import { mdxFromMarkdown, mdxToMarkdown } from './mdast-util-mdxish.js'; export default function remarkMdxish(this: any, options = {}) { - const data = this.data() + const data = this.data(); - add('micromarkExtensions', mdxjs(options)) - add('fromMarkdownExtensions', mdxFromMarkdown()) - add('toMarkdownExtensions', mdxToMarkdown()) + add('micromarkExtensions', mdxjs(options)); + add('fromMarkdownExtensions', mdxFromMarkdown()); + add('toMarkdownExtensions', mdxToMarkdown()); - function add(field: string, value: unknown) { - const list = data[field] ? data[field] : (data[field] = []) - list.push(value) - } + function add(field: string, value: unknown) { + const list = data[field] ? data[field] : (data[field] = []); + list.push(value); + } } diff --git a/packages/markdown/remark/test/components.test.js b/packages/markdown/remark/test/components.test.js index 0f3418c24..6e8017aa0 100644 --- a/packages/markdown/remark/test/components.test.js +++ b/packages/markdown/remark/test/components.test.js @@ -49,14 +49,23 @@ describe('components', () => { it('should normalize children', async () => { const { code } = await renderMarkdown(`Hello world!`, {}); - chai.expect(code).to.equal(`\nHello world!\n`); + chai + .expect(code) + .to.equal(`\nHello world!\n`); }); it('should allow markdown without many spaces', async () => { - const { code } = await renderMarkdown(` + const { code } = await renderMarkdown( + ` # Hello world! -`, {}); +`, + {} + ); - chai.expect(code).to.equal(`\n

Hello world!

\n
`); + chai + .expect(code) + .to.equal( + `\n

Hello world!

\n
` + ); }); }); diff --git a/packages/markdown/remark/test/expressions.test.js b/packages/markdown/remark/test/expressions.test.js index bcc95cbed..17674c543 100644 --- a/packages/markdown/remark/test/expressions.test.js +++ b/packages/markdown/remark/test/expressions.test.js @@ -17,24 +17,35 @@ describe('expressions', () => { it('should be able to serialize expression inside markdown', async () => { const { code } = await renderMarkdown(`# {frontmatter.title}`, {}); - chai.expect(code).to.equal(`

{frontmatter.title}

`); + chai + .expect(code) + .to.equal(`

{frontmatter.title}

`); }); it('should be able to serialize complex expression inside markdown', async () => { const { code } = await renderMarkdown(`# Hello {frontmatter.name}`, {}); - chai.expect(code).to.equal(`

Hello {frontmatter.name}

`); + chai + .expect(code) + .to.equal(`

Hello {frontmatter.name}

`); }); it('should be able to serialize complex expression with markup inside markdown', async () => { const { code } = await renderMarkdown(`# Hello {frontmatter.name}`, {}); - chai.expect(code).to.equal(`

Hello {frontmatter.name}

`); + chai + .expect(code) + .to.equal( + `

Hello {frontmatter.name}

` + ); }); it('should be able to serialize function expression', async () => { - const { code } = await renderMarkdown(`{frontmatter.list.map(item =>

{item}

)}` , {}); + const { code } = await renderMarkdown( + `{frontmatter.list.map(item =>

{item}

)}`, + {} + ); chai.expect(code).to.equal(`{frontmatter.list.map(item =>

{item}

)}`); - }) + }); }); diff --git a/packages/markdown/remark/test/plugins.test.js b/packages/markdown/remark/test/plugins.test.js index 4954047b5..a1abeb2ed 100644 --- a/packages/markdown/remark/test/plugins.test.js +++ b/packages/markdown/remark/test/plugins.test.js @@ -16,11 +16,11 @@ describe('plugins', () => { }; return transformer; - } - ] + }, + ], }); chai.expect(typeof context).to.equal('object'); chai.expect(context.path).to.equal(fileURLToPath(new URL('virtual.md', import.meta.url))); }); -}) +});