Skip to content

Commit

Permalink
chore: fix deprecated blockquote (#219)
Browse files Browse the repository at this point in the history
* Push

* changeset
  • Loading branch information
PuruVJ committed Sep 9, 2023
1 parent d99a85c commit 73138b3
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-houses-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/site-kit': patch
---

fix: deprecated blockquote
231 changes: 118 additions & 113 deletions packages/site-kit/src/lib/markdown/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -532,139 +532,109 @@ 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<div class="ts-block-property-bullets">\n\n${child.bullets.join(
'\n'
)}\n\n</div>`;
}
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<div class="ts-block-property-children">\n\n${child.children
.map((v) => stringify(v))
.join('\n')}\n\n</div>`;
}
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<div class="ts-block-property-bullets">\n\n${child.bullets.join(
'\n'
)}\n\n</div>`;
}

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<div class="ts-block-property-children">\n\n${child.children
.map((v) => stringify(v))
.join('\n')}\n\n</div>`;
}

return (
`<div class="ts-block">${fence(type.snippet, 'dts')}` +
type.children?.map((v) => stringify(v)).join('\n\n') +
`</div>`
);
}
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 =
'<div class="ts-block-property-details">\n\nSee the [configuration reference](/docs/configuration) for details.</div>';
}
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
? ` <blockquote class="tag deprecated">${transform(t.deprecated)}</blockquote>`
: '';
if (id) {
const type = module.types.find((t) => t.name === id);

const markdown =
`<div class="ts-block">${fence(t.snippet, 'dts')}` + children + `</div>`;
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 (
`<div class="ts-block">${fence(type.snippet, 'dts')}` +
type.children?.map((v) => stringify(v)).join('\n\n') +
`</div>`
);
}

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 =
'<div class="ts-block-property-details">\n\nSee the [configuration reference](/docs/configuration) for details.</div>';
}
const exported = module.exports?.filter((t) => t.name === id);
const deprecated = t.deprecated
? ` <blockquote class="tag deprecated">${await transform(t.deprecated)}</blockquote>`
: '';
return exported
?.map((exportVal) => `<div class="ts-block">${fence(exportVal.snippet, 'dts')}</div>`)
.join('\n\n');
})
.replace(REGEXES.MODULES, () => {
return modules
.map((module) => {
if (!module.exports) return;
const markdown = `<div class="ts-block">${fence(t.snippet, 'dts')}` + children + `</div>`;
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) => `<div class="ts-block">${fence(exportVal.snippet, 'dts')}</div>`)
.join('\n\n') ?? ''
);
});

return `## ${module.name}\n\n${import_block}\n\n${module.comment}\n\n${module.exports
.map((type) => {
const markdown =
`<div class="ts-block">${fence(type.snippet)}` +
type.children?.map((v) => stringify(v)).join('\n\n') +
`</div>`;
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 '';

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)));
const exports = Array.from(new Set(module.exports?.map((x) => x.name)));

let declaration = `import { ${exports.join(', ')} } from '${module.name}';`;
if (declaration.length > 80) {
Expand All @@ -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 =
`<div class="ts-block">${fence(type.snippet, 'dts')}` +
type.children?.map((val) => stringify(val, 'dts')).join('\n\n') +
`<div class="ts-block">${fence(type.snippet)}` +
type.children?.map((v) => stringify(v)).join('\n\n') +
`</div>`;
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 =
`<div class="ts-block">${fence(type.snippet, 'dts')}` +
type.children?.map((val) => stringify(val, 'dts')).join('\n\n') +
`</div>`;
return `### ${type.name}\n\n${type.comment}\n\n${markdown}`;
})
.join('\n\n')}`;
});

return content;
}

/**
Expand Down

0 comments on commit 73138b3

Please sign in to comment.