Skip to content

remove some unnecessary markdown transformation indirection #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 43 additions & 65 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,48 @@ export async function render_content_markdown(
}
}

return parse({
body,
type_links,
code: (raw, language, current) => {
const cached_snippet = SNIPPET_CACHE.get(raw + language + current);
const headings: string[] = [];

// this is a bit hacky, but it allows us to prevent type declarations
// from linking to themselves
let current = '';

return await transform(body, {
text(token) {
// @ts-expect-error I think this is a bug in marked — some text tokens have children,
// but that's not reflected in the types. In these cases we can't just use `token.tokens`
// because that will result in e.g. `<code>` elements not being generated
if (token.tokens) {
// @ts-expect-error
return this.parser!.parseInline(token.tokens);
}

return smart_quotes(token.text, true);
},
heading({ tokens, depth, raw }) {
const text = this.parser!.parseInline(tokens);

const title = text
.replace(/<\/?code>/g, '')
.replace(/&quot;/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
current = title;
const normalized = normalizeSlugify(raw);
headings[depth - 1] = normalized;
headings.length = depth;
const slug = headings.filter(Boolean).join('-');
return `<h${depth} id="${slug}">${text.replace(
/<\/?code>/g,
''
)}<a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${depth}>`;
},
code({ text, lang = 'js' }) {
const cached_snippet = SNIPPET_CACHE.get(text + lang + current);
if (cached_snippet.code) return cached_snippet.code;

let { source, options } = parse_options(raw, language);
source = adjust_tab_indentation(source, language);
let { source, options } = parse_options(text, lang);
source = adjust_tab_indentation(source, lang);

const converted = conversions.get(source);

Expand All @@ -182,7 +215,7 @@ export async function render_content_markdown(
html += syntax_highlight({
filename,
highlighter,
language,
language: lang,
source,
twoslashBanner,
options
Expand All @@ -192,7 +225,7 @@ export async function render_content_markdown(
html += syntax_highlight({
filename,
highlighter,
language: language === 'js' ? 'ts' : language,
language: lang === 'js' ? 'ts' : lang,
source: converted,
twoslashBanner,
options
Expand Down Expand Up @@ -225,7 +258,7 @@ export async function render_content_markdown(

return html;
},
codespan: (text) => {
codespan({ text }) {
return (
'<code>' +
(type_regex
Expand All @@ -238,61 +271,6 @@ export async function render_content_markdown(
: text) +
'</code>'
);
}
});
}

async function parse({
body,
code,
codespan
}: {
body: string;
type_links: Map<string, { relativeURL: string; slug: string; page: string }> | null;
code: (source: string, language: string, current: string) => string;
codespan: (source: string) => string;
}) {
const headings: string[] = [];

// this is a bit hacky, but it allows us to prevent type declarations
// from linking to themselves
let current = '';

return await transform(body, {
text(token) {
// @ts-expect-error I think this is a bug in marked — some text tokens have children,
// but that's not reflected in the types. In these cases we can't just use `token.tokens`
// because that will result in e.g. `<code>` elements not being generated
if (token.tokens) {
// @ts-expect-error
return this.parser!.parseInline(token.tokens);
}

return smart_quotes(token.text, true);
},
heading({ tokens, depth, raw }) {
const text = this.parser!.parseInline(tokens);

const title = text
.replace(/<\/?code>/g, '')
.replace(/&quot;/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
current = title;
const normalized = normalizeSlugify(raw);
headings[depth - 1] = normalized;
headings.length = depth;
const slug = headings.filter(Boolean).join('-');
return `<h${depth} id="${slug}">${text.replace(
/<\/?code>/g,
''
)}<a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${depth}>`;
},
code({ text, lang }) {
return code(text, lang ?? 'js', current);
},
codespan({ text }) {
return codespan(text);
},
blockquote(token) {
let content = this.parser?.parse(token.tokens) ?? '';
Expand Down
Loading