From 73138b3d6c481330566196e8fe98f1c9b44ff76c Mon Sep 17 00:00:00 2001 From: Puru Vijay <47742487+PuruVJ@users.noreply.github.com> Date: Sat, 9 Sep 2023 14:50:40 +0530 Subject: [PATCH] chore: fix deprecated blockquote (#219) * Push * changeset --- .changeset/chilled-houses-sort.md | 5 + .../site-kit/src/lib/markdown/renderer.js | 231 +++++++++--------- 2 files changed, 123 insertions(+), 113 deletions(-) create mode 100644 .changeset/chilled-houses-sort.md diff --git a/.changeset/chilled-houses-sort.md b/.changeset/chilled-houses-sort.md new file mode 100644 index 00000000..c5c2ed6d --- /dev/null +++ b/.changeset/chilled-houses-sort.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/site-kit': patch +--- + +fix: deprecated blockquote diff --git a/packages/site-kit/src/lib/markdown/renderer.js b/packages/site-kit/src/lib/markdown/renderer.js index 05e5be27..c550363a 100644 --- a/packages/site-kit/src/lib/markdown/renderer.js +++ b/packages/site-kit/src/lib/markdown/renderer.js @@ -120,7 +120,7 @@ export async function render_content_markdown( const SNIPPET_CACHE = await create_snippet_cache(cacheCodeSnippets); return parse({ - body: await generate_ts_from_js(replace_export_type_placeholders(body, modules)), + body: await generate_ts_from_js(await replace_export_type_placeholders(body, modules)), type_links, code: (source, language, current) => { const cached_snippet = SNIPPET_CACHE.get(source + language + current); @@ -514,7 +514,7 @@ export async function convert_to_ts(js_code, indent = '', offset = '') { * @param {string} content * @param {import('.').Modules} modules */ -export function replace_export_type_placeholders(content, modules) { +export async function replace_export_type_placeholders(content, modules) { const REGEXES = { EXPANDED_TYPES: /> EXPANDED_TYPES: (.+?)#(.+)$/gm, TYPES: /> TYPES: (.+?)(?:#(.+))?$/gm, @@ -532,131 +532,101 @@ export function replace_export_type_placeholders(content, modules) { .replace(REGEXES.EXPORTS, ''); } - return ( - content - .replace(REGEXES.EXPANDED_TYPES, (_, name, id) => { - const module = modules.find((module) => module.name === name); - if (!module) throw new Error(`Could not find module ${name}`); - if (!module.types) return ''; - - const type = module.types.find((t) => t.name === id); - - if (!type) return ''; - - return ( - type.comment + - type.children - ?.map((child) => { - let section = `### ${child.name}`; - - if (child.bullets) { - section += `\n\n
\n\n${child.bullets.join( - '\n' - )}\n\n
`; - } + content = await async_replace(content, REGEXES.EXPANDED_TYPES, async ([_, name, id]) => { + const module = modules.find((module) => module.name === name); + if (!module) throw new Error(`Could not find module ${name}`); + if (!module.types) return ''; - section += `\n\n${child.comment}`; + const type = module.types.find((t) => t.name === id); - if (child.children) { - section += `\n\n
\n\n${child.children - .map((v) => stringify(v)) - .join('\n')}\n\n
`; - } + if (!type) return ''; - return section; - }) - .join('\n\n') - ); - }) - .replace(REGEXES.TYPES, (_, name, id) => { - const module = modules.find((module) => module.name === name); - if (!module) throw new Error(`Could not find module ${name}`); - if (!module.types) return ''; + return ( + type.comment + + type.children + ?.map((child) => { + let section = `### ${child.name}`; + + if (child.bullets) { + section += `\n\n
\n\n${child.bullets.join( + '\n' + )}\n\n
`; + } - if (id) { - const type = module.types.find((t) => t.name === id); + section += `\n\n${child.comment}`; - if (!type) return ''; + if (child.children) { + section += `\n\n
\n\n${child.children + .map((v) => stringify(v)) + .join('\n')}\n\n
`; + } - return ( - `
${fence(type.snippet, 'dts')}` + - type.children?.map((v) => stringify(v)).join('\n\n') + - `
` - ); - } + return section; + }) + .join('\n\n') + ); + }); - return `${module.comment}\n\n${module.types - .map((t) => { - let children = t.children?.map((val) => stringify(val, 'dts')).join('\n\n'); - if (t.name === 'Config' || t.name === 'KitConfig') { - // special case — we want these to be on a separate page - children = - '
\n\nSee the [configuration reference](/docs/configuration) for details.
'; - } + content = await async_replace(content, REGEXES.TYPES, async ([_, name, id]) => { + const module = modules.find((module) => module.name === name); + if (!module) throw new Error(`Could not find module ${name}`); + if (!module.types) return ''; - const deprecated = t.deprecated - ? `
${transform(t.deprecated)}
` - : ''; + if (id) { + const type = module.types.find((t) => t.name === id); - const markdown = - `
${fence(t.snippet, 'dts')}` + children + `
`; + if (!type) return ''; - return `### ${t.name}\n\n${deprecated}\n\n${t.comment ?? ''}\n\n${markdown}\n\n`; - }) - .join('')}`; - }) - // @ts-ignore - .replace(REGEXES.EXPORT_SNIPPET, (_, name, id) => { - const module = modules.find((module) => module.name === name); - if (!module) throw new Error(`Could not find module ${name} for EXPORT_SNIPPET clause`); + return ( + `
${fence(type.snippet, 'dts')}` + + type.children?.map((v) => stringify(v)).join('\n\n') + + `
` + ); + } - if (!id) { - throw new Error(`id is required for module ${name}`); - } + return `${module.comment}\n\n${( + await Promise.all( + module.types.map(async (t) => { + let children = t.children?.map((val) => stringify(val, 'dts')).join('\n\n'); + if (t.name === 'Config' || t.name === 'KitConfig') { + // special case — we want these to be on a separate page + children = + '
\n\nSee the [configuration reference](/docs/configuration) for details.
'; + } - const exported = module.exports?.filter((t) => t.name === id); + const deprecated = t.deprecated + ? `
${await transform(t.deprecated)}
` + : ''; - return exported - ?.map((exportVal) => `
${fence(exportVal.snippet, 'dts')}
`) - .join('\n\n'); - }) - .replace(REGEXES.MODULES, () => { - return modules - .map((module) => { - if (!module.exports) return; + const markdown = `
${fence(t.snippet, 'dts')}` + children + `
`; - if (module.exports.length === 0 && !module.exempt) return ''; + return `### ${t.name}\n\n${deprecated}\n\n${t.comment ?? ''}\n\n${markdown}\n\n`; + }) + ) + ).join('')}`; + }); - let import_block = ''; + content = await async_replace(content, REGEXES.EXPORT_SNIPPET, async ([_, name, id]) => { + const module = modules.find((module) => module.name === name); + if (!module) throw new Error(`Could not find module ${name} for EXPORT_SNIPPET clause`); - if (module.exports.length > 0) { - // deduplication is necessary for now, because of `error()` overload - const exports = Array.from(new Set(module.exports?.map((x) => x.name))); + if (!id) { + throw new Error(`id is required for module ${name}`); + } - let declaration = `import { ${exports.join(', ')} } from '${module.name}';`; - if (declaration.length > 80) { - declaration = `import {\n\t${exports.join(',\n\t')}\n} from '${module.name}';`; - } + const exported = module.exports?.filter((t) => t.name === id); - import_block = fence(declaration, 'js'); - } + return ( + exported + ?.map((exportVal) => `
${fence(exportVal.snippet, 'dts')}
`) + .join('\n\n') ?? '' + ); + }); - return `## ${module.name}\n\n${import_block}\n\n${module.comment}\n\n${module.exports - .map((type) => { - const markdown = - `
${fence(type.snippet)}` + - type.children?.map((v) => stringify(v)).join('\n\n') + - `
`; - return `### ${type.name}\n\n${type.comment}\n\n${markdown}`; - }) - .join('\n\n')}`; - }) - .join('\n\n'); - }) - .replace(REGEXES.EXPORTS, (_, name) => { - const module = modules.find((module) => module.name === name); - if (!module) throw new Error(`Could not find module ${name} for EXPORTS: clause`); - if (!module.exports) return ''; + content = await async_replace(content, REGEXES.MODULES, async () => { + return modules + .map((module) => { + if (!module.exports) return; if (module.exports.length === 0 && !module.exempt) return ''; @@ -664,7 +634,7 @@ export function replace_export_type_placeholders(content, modules) { if (module.exports.length > 0) { // deduplication is necessary for now, because of `error()` overload - const exports = Array.from(new Set(module.exports.map((x) => x.name))); + const exports = Array.from(new Set(module.exports?.map((x) => x.name))); let declaration = `import { ${exports.join(', ')} } from '${module.name}';`; if (declaration.length > 80) { @@ -674,17 +644,52 @@ export function replace_export_type_placeholders(content, modules) { import_block = fence(declaration, 'js'); } - return `${import_block}\n\n${module.comment}\n\n${module.exports + return `## ${module.name}\n\n${import_block}\n\n${module.comment}\n\n${module.exports .map((type) => { const markdown = - `
${fence(type.snippet, 'dts')}` + - type.children?.map((val) => stringify(val, 'dts')).join('\n\n') + + `
${fence(type.snippet)}` + + type.children?.map((v) => stringify(v)).join('\n\n') + `
`; return `### ${type.name}\n\n${type.comment}\n\n${markdown}`; }) .join('\n\n')}`; }) - ); + .join('\n\n'); + }); + + content = await async_replace(content, REGEXES.EXPORTS, async ([_, name]) => { + const module = modules.find((module) => module.name === name); + if (!module) throw new Error(`Could not find module ${name} for EXPORTS: clause`); + if (!module.exports) return ''; + + if (module.exports.length === 0 && !module.exempt) return ''; + + let import_block = ''; + + if (module.exports.length > 0) { + // deduplication is necessary for now, because of `error()` overload + const exports = Array.from(new Set(module.exports.map((x) => x.name))); + + let declaration = `import { ${exports.join(', ')} } from '${module.name}';`; + if (declaration.length > 80) { + declaration = `import {\n\t${exports.join(',\n\t')}\n} from '${module.name}';`; + } + + import_block = fence(declaration, 'js'); + } + + return `${import_block}\n\n${module.comment}\n\n${module.exports + .map((type) => { + const markdown = + `
${fence(type.snippet, 'dts')}` + + type.children?.map((val) => stringify(val, 'dts')).join('\n\n') + + `
`; + return `### ${type.name}\n\n${type.comment}\n\n${markdown}`; + }) + .join('\n\n')}`; + }); + + return content; } /**