Skip to content

Commit 1879de1

Browse files
authored
remove some unnecessary markdown transformation indirection (#285)
1 parent e51ea45 commit 1879de1

File tree

1 file changed

+43
-65
lines changed

1 file changed

+43
-65
lines changed

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,48 @@ export async function render_content_markdown(
148148
}
149149
}
150150

151-
return parse({
152-
body,
153-
type_links,
154-
code: (raw, language, current) => {
155-
const cached_snippet = SNIPPET_CACHE.get(raw + language + current);
151+
const headings: string[] = [];
152+
153+
// this is a bit hacky, but it allows us to prevent type declarations
154+
// from linking to themselves
155+
let current = '';
156+
157+
return await transform(body, {
158+
text(token) {
159+
// @ts-expect-error I think this is a bug in marked — some text tokens have children,
160+
// but that's not reflected in the types. In these cases we can't just use `token.tokens`
161+
// because that will result in e.g. `<code>` elements not being generated
162+
if (token.tokens) {
163+
// @ts-expect-error
164+
return this.parser!.parseInline(token.tokens);
165+
}
166+
167+
return smart_quotes(token.text, true);
168+
},
169+
heading({ tokens, depth, raw }) {
170+
const text = this.parser!.parseInline(tokens);
171+
172+
const title = text
173+
.replace(/<\/?code>/g, '')
174+
.replace(/&quot;/g, '"')
175+
.replace(/&lt;/g, '<')
176+
.replace(/&gt;/g, '>');
177+
current = title;
178+
const normalized = normalizeSlugify(raw);
179+
headings[depth - 1] = normalized;
180+
headings.length = depth;
181+
const slug = headings.filter(Boolean).join('-');
182+
return `<h${depth} id="${slug}">${text.replace(
183+
/<\/?code>/g,
184+
''
185+
)}<a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${depth}>`;
186+
},
187+
code({ text, lang = 'js' }) {
188+
const cached_snippet = SNIPPET_CACHE.get(text + lang + current);
156189
if (cached_snippet.code) return cached_snippet.code;
157190

158-
let { source, options } = parse_options(raw, language);
159-
source = adjust_tab_indentation(source, language);
191+
let { source, options } = parse_options(text, lang);
192+
source = adjust_tab_indentation(source, lang);
160193

161194
const converted = conversions.get(source);
162195

@@ -182,7 +215,7 @@ export async function render_content_markdown(
182215
html += syntax_highlight({
183216
filename,
184217
highlighter,
185-
language,
218+
language: lang,
186219
source,
187220
twoslashBanner,
188221
options
@@ -192,7 +225,7 @@ export async function render_content_markdown(
192225
html += syntax_highlight({
193226
filename,
194227
highlighter,
195-
language: language === 'js' ? 'ts' : language,
228+
language: lang === 'js' ? 'ts' : lang,
196229
source: converted,
197230
twoslashBanner,
198231
options
@@ -225,7 +258,7 @@ export async function render_content_markdown(
225258

226259
return html;
227260
},
228-
codespan: (text) => {
261+
codespan({ text }) {
229262
return (
230263
'<code>' +
231264
(type_regex
@@ -238,61 +271,6 @@ export async function render_content_markdown(
238271
: text) +
239272
'</code>'
240273
);
241-
}
242-
});
243-
}
244-
245-
async function parse({
246-
body,
247-
code,
248-
codespan
249-
}: {
250-
body: string;
251-
type_links: Map<string, { relativeURL: string; slug: string; page: string }> | null;
252-
code: (source: string, language: string, current: string) => string;
253-
codespan: (source: string) => string;
254-
}) {
255-
const headings: string[] = [];
256-
257-
// this is a bit hacky, but it allows us to prevent type declarations
258-
// from linking to themselves
259-
let current = '';
260-
261-
return await transform(body, {
262-
text(token) {
263-
// @ts-expect-error I think this is a bug in marked — some text tokens have children,
264-
// but that's not reflected in the types. In these cases we can't just use `token.tokens`
265-
// because that will result in e.g. `<code>` elements not being generated
266-
if (token.tokens) {
267-
// @ts-expect-error
268-
return this.parser!.parseInline(token.tokens);
269-
}
270-
271-
return smart_quotes(token.text, true);
272-
},
273-
heading({ tokens, depth, raw }) {
274-
const text = this.parser!.parseInline(tokens);
275-
276-
const title = text
277-
.replace(/<\/?code>/g, '')
278-
.replace(/&quot;/g, '"')
279-
.replace(/&lt;/g, '<')
280-
.replace(/&gt;/g, '>');
281-
current = title;
282-
const normalized = normalizeSlugify(raw);
283-
headings[depth - 1] = normalized;
284-
headings.length = depth;
285-
const slug = headings.filter(Boolean).join('-');
286-
return `<h${depth} id="${slug}">${text.replace(
287-
/<\/?code>/g,
288-
''
289-
)}<a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${depth}>`;
290-
},
291-
code({ text, lang }) {
292-
return code(text, lang ?? 'js', current);
293-
},
294-
codespan({ text }) {
295-
return codespan(text);
296274
},
297275
blockquote(token) {
298276
let content = this.parser?.parse(token.tokens) ?? '';

0 commit comments

Comments
 (0)