<\/div>/g, '
');
- // } else if (language === 'diff') {
- // const lines = source.split('\n').map((content) => {
- // let type = null;
- // if (/^[\+\-]/.test(content)) {
- // type = content[0] === '+' ? 'inserted' : 'deleted';
- // content = content.slice(1);
- // }
-
- // return {
- // type,
- // content: escape(content),
- // };
- // });
-
- // html = `
${lines
- // .map((line) => {
- // if (line.type) return `${line.content}\n`;
- // return line.content + '\n';
- // })
- // .join('')}
`;
- // } else {
- // const plang = languages[language];
- // const highlighted = plang
- // ? PrismJS.highlight(source, PrismJS.languages[plang], language)
- // : source.replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c]));
-
- // html = `
${highlighted}
`;
- // }
+ if (language === 'dts') {
+ html = renderCodeToHTML(
+ source,
+ 'ts',
+ { twoslash: false },
+ { themeName: 'css-variables' },
+ highlighter
+ );
+ } else if (language === 'js' || language === 'ts') {
+ try {
+ const injected = [];
+
+ if (/(svelte)/.test(source) || page.file.includes('typescript')) {
+ injected.push(
+ `// @filename: ambient.d.ts`,
+ `///
`,
+ `///
`,
+ `///
`,
+ `///
`,
+ `///
`,
+ `///
`,
+ `///
`,
+ `///
`
+ );
+ }
+
+ if (page.file.includes('svelte-compiler')) {
+ injected.push('// @esModuleInterop');
+ }
+
+ if (page.file.includes('svelte.md')) {
+ injected.push('// @errors: 2304');
+ }
+
+ // Actions JSDoc examples are invalid. Too many errors, edge cases
+ if (page.file.includes('svelte-action')) {
+ injected.push('// @noErrors');
+ }
+
+ if (page.file.includes('typescript')) {
+ injected.push('// @errors: 2304');
+ }
+
+ if (injected.length) {
+ const injected_str = injected.join('\n');
+ if (source.includes('// @filename:')) {
+ source = source.replace('// @filename:', `${injected_str}\n\n// @filename:`);
+ } else {
+ source = source.replace(
+ /^(?!\/\/ @)/m,
+ `${injected_str}\n\n// @filename: index.${language}\n` + ` // ---cut---\n`
+ );
+ }
+ }
+
+ const twoslash = runTwoSlash(source, language, {
+ defaultCompilerOptions: {
+ allowJs: true,
+ checkJs: true,
+ target: ts.ScriptTarget.ES2022
+ }
+ });
+
+ html = renderCodeToHTML(
+ twoslash.code,
+ 'ts',
+ { twoslash: true },
+ // @ts-ignore Why shiki-twoslash requires a theme name?
+ {},
+ highlighter,
+ twoslash
+ );
+ } catch (e) {
+ console.error(`Error compiling snippet in ${page.file}`);
+ console.error(e.code);
+ throw e;
+ }
+
+ // we need to be able to inject the LSP attributes as HTML, not text, so we
+ // turn < into <
+ html = html.replace(
+ /
]*)>(\w+)<\/data-lsp>/g,
+ (match, lsp, attrs, name) => {
+ if (!lsp) return name;
+ return `${name}`;
+ }
+ );
+
+ // preserve blank lines in output (maybe there's a more correct way to do this?)
+ html = html.replace(/<\/div>/g, '
');
+ } else if (language === 'diff') {
+ const lines = source.split('\n').map((content) => {
+ let type = null;
+ if (/^[\+\-]/.test(content)) {
+ type = content[0] === '+' ? 'inserted' : 'deleted';
+ content = content.slice(1);
+ }
+
+ return {
+ type,
+ content: escape(content)
+ };
+ });
+
+ html = `
${lines
+ .map((line) => {
+ if (line.type) return `${line.content}\n`;
+ return line.content + '\n';
+ })
+ .join('')}
`;
+ } else {
+ const highlighted = highlighter.codeToHtml(source, {
+ lang: SHIKI_LANGUAGE_MAP[language]
+ });
+
+ html = highlighted.replace(/
<\/div>/g, '
');
+ }
if (options.file) {
html = `
${options.file}${html}
`;
@@ -195,18 +224,20 @@ export async function get_parsed_docs(docs_data, slug) {
html = html.replace(/class=('|")/, `class=$1${version_class} `);
}
- // type_regex.lastIndex = 0;
+ type_regex.lastIndex = 0;
html = html
- // .replace(type_regex, (match, prefix, name) => {
- // if (options.link === 'false' || name === current) {
- // // we don't want e.g. RequestHandler to link to RequestHandler
- // return match;
- // }
-
- // const link = `
${name}`;
- // return `${prefix || ''}${link}`;
- // })
+ .replace(type_regex, (match, prefix, name, pos, str) => {
+ const char_after = str.slice(pos + match.length, pos + match.length + 1);
+
+ if (options.link === 'false' || name === current || /(\$|\d|\w)/.test(char_after)) {
+ // we don't want e.g. RequestHandler to link to RequestHandler
+ return match;
+ }
+
+ const link = `
${name}`;
+ return `${prefix || ''}${link}`;
+ })
.replace(
/^(\s+)