diff options
author | 2021-10-29 12:45:32 -0700 | |
---|---|---|
committer | 2021-10-29 14:45:32 -0500 | |
commit | 34e03cf912161abd5ca66fd12ce458d51d011855 (patch) | |
tree | af33b1cf19ddd45b4c6effbf8c25b8f74d2e7c23 /packages/markdown/remark/src | |
parent | 93489946ccd59c27d85cc9fa3a255edf6efeb861 (diff) | |
download | astro-34e03cf912161abd5ca66fd12ce458d51d011855.tar.gz astro-34e03cf912161abd5ca66fd12ce458d51d011855.tar.zst astro-34e03cf912161abd5ca66fd12ce458d51d011855.zip |
do not format vite (#1710)
* Revert "[ci] yarn format"
This reverts commit 93489946ccd59c27d85cc9fa3a255edf6efeb861.
* chore: ignore vendor
Diffstat (limited to 'packages/markdown/remark/src')
-rw-r--r-- | packages/markdown/remark/src/index.ts | 21 | ||||
-rw-r--r-- | packages/markdown/remark/src/rehype-islands.ts | 10 | ||||
-rw-r--r-- | packages/markdown/remark/src/rehype-jsx.ts | 10 | ||||
-rw-r--r-- | packages/markdown/remark/src/remark-prism.ts | 27 | ||||
-rw-r--r-- | packages/markdown/remark/src/remark-slug.ts | 24 | ||||
-rw-r--r-- | packages/markdown/remark/src/remark-unwrap.ts | 20 | ||||
-rw-r--r-- | packages/markdown/remark/src/types.ts | 2 |
7 files changed, 63 insertions, 51 deletions
diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index 5c707a4db..a5fb403d4 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -32,17 +32,17 @@ export const DEFAULT_REMARK_PLUGINS = [ 'remark-footnotes', // TODO: reenable smartypants! // '@silvenon/remark-smartypants' -]; +] export const DEFAULT_REHYPE_PLUGINS = [ // empty -]; +] /** Shared utility for rendering markdown */ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOptions | null) { const { remarkPlugins = DEFAULT_REMARK_PLUGINS, rehypePlugins = DEFAULT_REHYPE_PLUGINS } = opts ?? {}; const scopedClassName = opts?.$?.scopedClassName; - const mode = opts?.mode ?? 'mdx'; + const mode = opts?.mode ?? "mdx"; const isMDX = mode === 'mdx'; const { headers, rehypeCollectHeaders } = createCollectHeaders(); @@ -52,7 +52,7 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp .use(markdown) .use(isMDX ? [remarkJsx] : []) .use(isMDX ? [remarkExpressions] : []) - .use([remarkUnwrap]); + .use([remarkUnwrap]) const loadedRemarkPlugins = await Promise.all(loadPlugins(remarkPlugins)); const loadedRehypePlugins = await Promise.all(loadPlugins(rehypePlugins)); @@ -62,25 +62,28 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp }); if (scopedClassName) { - parser.use([scopedStyles(scopedClassName)]); + parser.use([scopedStyles(scopedClassName)]); } parser.use([remarkPrism(scopedClassName)]); - parser.use([[markdownToHtml as any, { allowDangerousHtml: true, passThrough: ['raw', 'mdxTextExpression', 'mdxJsxTextElement', 'mdxJsxFlowElement'] }]]); + parser.use([[markdownToHtml as any, { allowDangerousHtml: true, passThrough: ['raw', 'mdxTextExpression', 'mdxJsxTextElement', 'mdxJsxFlowElement']}]]); loadedRehypePlugins.forEach(([plugin, opts]) => { parser.use([[plugin, opts]]); }); - + parser .use(isMDX ? [rehypeJsx] : []) .use(isMDX ? [rehypeExpressions] : []) .use(isMDX ? [] : [rehypeRaw]) - .use(rehypeIslands); + .use(rehypeIslands) let result: string; try { - const vfile = await parser.use([rehypeCollectHeaders]).use(rehypeStringify, { allowDangerousHtml: true }).process(content); + const vfile = await parser + .use([rehypeCollectHeaders]) + .use(rehypeStringify, { allowDangerousHtml: true }) + .process(content); result = vfile.toString(); } catch (err) { console.error(err); diff --git a/packages/markdown/remark/src/rehype-islands.ts b/packages/markdown/remark/src/rehype-islands.ts index b98419cd4..099dc4d75 100644 --- a/packages/markdown/remark/src/rehype-islands.ts +++ b/packages/markdown/remark/src/rehype-islands.ts @@ -1,4 +1,4 @@ -import { SKIP, visit } from 'unist-util-visit'; +import {SKIP, visit} from 'unist-util-visit'; // This fixes some confusing bugs coming from somewhere inside of our Markdown pipeline. // `unist`/`remark`/`rehype` (not sure) often generate malformed HTML inside of <astro-root> @@ -11,11 +11,11 @@ export default function rehypeIslands(): any { if (el.tagName == 'astro-root') { visit(el, 'text', (child, index, parent) => { if (child.type === 'text') { - // Sometimes comments can be trapped as text, which causes them to be escaped + // Sometimes comments can be trapped as text, which causes them to be escaped // This casts them back to real HTML comments if (parent && child.value.indexOf('<!--') > -1 && index != null) { - parent.children.splice(index, 1, { ...child, type: 'comment', value: child.value.replace('<!--', '').replace('-->', '').trim() }); - return [SKIP, index]; + parent.children.splice(index, 1, { ...child, type: 'comment', value: child.value.replace('<!--', '').replace('-->', '').trim()}); + return [SKIP, index] } // For some reason `rehype` likes to inject extra linebreaks, // but React and Vue throw hydration errors when they see these! @@ -24,7 +24,7 @@ export default function rehypeIslands(): any { child.value = child.value.replace(/\n+/g, ''); return child; } - }); + }) } }); }; diff --git a/packages/markdown/remark/src/rehype-jsx.ts b/packages/markdown/remark/src/rehype-jsx.ts index 94632efed..c5270e2af 100644 --- a/packages/markdown/remark/src/rehype-jsx.ts +++ b/packages/markdown/remark/src/rehype-jsx.ts @@ -5,22 +5,22 @@ export default function rehypeJsx(): any { return function (node: any): any { return map(node, (child: any) => { if (child.type === 'element') { - return { ...child, tagName: `${child.tagName}` }; + return { ...child, tagName: `${child.tagName}` } } if (MDX_ELEMENTS.has(child.type)) { - return { + return { ...child, type: 'element', tagName: `${child.name}`, properties: child.attributes.reduce((acc: any[], entry: any) => { let attr = entry.value; if (attr && typeof attr === 'object') { - attr = `{${attr.value}}`; + attr = `{${attr.value}}` } else if (attr === null) { - attr = `{true}`; + attr = `{true}` } return Object.assign(acc, { [entry.name]: attr }); - }, {}), + }, {}) }; } return child; diff --git a/packages/markdown/remark/src/remark-prism.ts b/packages/markdown/remark/src/remark-prism.ts index d7a4ff996..d8dd8d922 100644 --- a/packages/markdown/remark/src/remark-prism.ts +++ b/packages/markdown/remark/src/remark-prism.ts @@ -4,24 +4,26 @@ import { addAstro } from '@astrojs/prism'; import loadLanguages from 'prismjs/components/index.js'; const noVisit = new Set(['root', 'html', 'text']); -const languageMap = new Map([['ts', 'typescript']]); +const languageMap = new Map([ + ['ts', 'typescript'] +]); function runHighlighter(lang: string, code: string) { - let classLanguage = `language-${lang}`; + let classLanguage = `language-${lang}` if (lang == null) { lang = 'plaintext'; } const ensureLoaded = (lang: string) => { - if (lang && !Prism.languages[lang]) { + if(lang && !Prism.languages[lang]) { loadLanguages([lang]); } }; - if (languageMap.has(lang)) { + if(languageMap.has(lang)) { ensureLoaded(languageMap.get(lang)!); - } else if (lang === 'astro') { + } else if(lang === 'astro') { ensureLoaded('typescript'); addAstro(Prism); } else { @@ -29,7 +31,7 @@ function runHighlighter(lang: string, code: string) { ensureLoaded(lang); } - if (lang && !Prism.languages[lang]) { + if(lang && !Prism.languages[lang]) { console.warn(`Unable to load the language: ${lang}`); } @@ -46,25 +48,26 @@ type MaybeString = string | null | undefined; /** */ function transformer(className: MaybeString) { - return function (tree: any) { + return function(tree: any) { const visitor = (node: any) => { - let { lang, value } = node; + let {lang, value} = node; node.type = 'html'; let { html, classLanguage } = runHighlighter(lang, value); let classes = [classLanguage]; - if (className) { + if(className) { classes.push(className); } node.value = `<pre class="${classes.join(' ')}"><code data-astro-raw class="${classLanguage}">${html}</code></pre>`; return node; }; - return visit(tree, 'code', visitor); - }; + return visit(tree, 'code', visitor) + } } + function plugin(className: MaybeString) { return transformer.bind(null, className); } -export default plugin; +export default plugin;
\ No newline at end of file diff --git a/packages/markdown/remark/src/remark-slug.ts b/packages/markdown/remark/src/remark-slug.ts index 4454d1087..b7c9c29de 100644 --- a/packages/markdown/remark/src/remark-slug.ts +++ b/packages/markdown/remark/src/remark-slug.ts @@ -3,11 +3,11 @@ * @typedef {import('hast').Properties} Properties */ -import { toString } from 'mdast-util-to-string'; -import { visit } from 'unist-util-visit'; -import BananaSlug from 'github-slugger'; +import {toString} from 'mdast-util-to-string' +import {visit} from 'unist-util-visit' +import BananaSlug from 'github-slugger' -const slugs = new BananaSlug(); +const slugs = new BananaSlug() /** * Plugin to add anchors headings using GitHub’s algorithm. @@ -16,17 +16,19 @@ const slugs = new BananaSlug(); */ export default function remarkSlug() { return (tree: any) => { - slugs.reset(); + slugs.reset() visit(tree, (node) => { console.log(node); }); visit(tree, 'heading', (node) => { - const data = node.data || (node.data = {}); - const props = /** @type {Properties} */ data.hProperties || (data.hProperties = {}); - let id = props.id; - id = id ? slugs.slug(String(id), true) : slugs.slug(toString(node)); + const data = node.data || (node.data = {}) + const props = /** @type {Properties} */ ( + data.hProperties || (data.hProperties = {}) + ) + let id = props.id + id = id ? slugs.slug(String(id), true) : slugs.slug(toString(node)) data.id = id; props.id = id; - }); - }; + }) + } } diff --git a/packages/markdown/remark/src/remark-unwrap.ts b/packages/markdown/remark/src/remark-unwrap.ts index d6f3275ab..e43a57a0c 100644 --- a/packages/markdown/remark/src/remark-unwrap.ts +++ b/packages/markdown/remark/src/remark-unwrap.ts @@ -1,4 +1,4 @@ -import { visit, SKIP } from 'unist-util-visit'; +import {visit, SKIP} from 'unist-util-visit' // Remove the wrapping paragraph for <astro-root> islands export default function remarkUnwrap() { @@ -18,17 +18,21 @@ export default function remarkUnwrap() { insideAstroRoot = false; } astroRootNodes.add(node); - }); + }) visit(tree, 'paragraph', (node, index, parent) => { - if (parent && typeof index === 'number' && containsAstroRootNode(node)) { - parent.children.splice(index, 1, ...node.children); - return [SKIP, index]; + if ( + parent && + typeof index === 'number' && + containsAstroRootNode(node) + ) { + parent.children.splice(index, 1, ...node.children) + return [SKIP, index] } - }); - }; + }) + } function containsAstroRootNode(node: any) { - return node.children.map((child: any) => astroRootNodes.has(child)).reduce((all: boolean, v: boolean) => (all ? all : v), false); + return node.children.map((child: any) => astroRootNodes.has(child)).reduce((all: boolean, v: boolean) => all ? all : v, false) } } diff --git a/packages/markdown/remark/src/types.ts b/packages/markdown/remark/src/types.ts index 8ae7795d0..201e50931 100644 --- a/packages/markdown/remark/src/types.ts +++ b/packages/markdown/remark/src/types.ts @@ -4,7 +4,7 @@ export type UnifiedPluginImport = Promise<{ default: unified.Plugin }>; export type Plugin = string | [string, any] | UnifiedPluginImport | [UnifiedPluginImport, any]; export interface AstroMarkdownOptions { - mode?: 'md' | 'mdx'; + mode?: 'md'|'mdx'; remarkPlugins?: Plugin[]; rehypePlugins?: Plugin[]; } |