diff --git a/packages/site-kit/src/lib/markdown/renderer.ts b/packages/site-kit/src/lib/markdown/renderer.ts index 71a8fd36f3..aaafcdfbe3 100644 --- a/packages/site-kit/src/lib/markdown/renderer.ts +++ b/packages/site-kit/src/lib/markdown/renderer.ts @@ -417,7 +417,7 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') { const code = new MagicString(js_code); const imports = new Map(); - async function walk(node: ts.Node) { + async function walk(node: ts.Node, prev: ts.Node | null) { const jsdoc = get_jsdoc(node); if (jsdoc) { @@ -563,19 +563,30 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') { while (start > 0 && code.original[start - 1] === '\t') start -= 1; while (start > 0 && code.original[start - 1] === '\n') start -= 1; - const slice = code.original.slice(node.getStart(), node.getEnd()); - const is_multiline = slice.includes('\n'); + let is_multiline = false; + + if (prev) { + is_multiline = + code.original.slice(prev.getStart(), prev.getEnd()).includes('\n') || + code.original.slice(node.getStart(), node.getEnd()).includes('\n'); + } code.overwrite(start, end, is_multiline ? '\n' : ''); } } + // the TypeScript API is such a hot mess, AFAICT there is no non-stupid way + // to get the previous sibling within the visitor, so since we need it we + // have to pass it in from the parent visitor + let child_prev: ts.Node | null = null; + for (const child_node of node.getChildren()) { - await walk(child_node); + await walk(child_node, child_prev); + child_prev = child_node; } } - await walk(ast); + await walk(ast, null); if (imports.size) { const import_statements = Array.from(imports.entries())