Skip to content
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
21 changes: 16 additions & 5 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down