Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions packages/exporters/src/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe('htmlToMarkdown', () => {
expect(out.length).toBeGreaterThan(0);
});

it('quotes titles starting with YAML indicator chars so frontmatter stays valid', () => {
const cases = ['- TODO List', '? maybe', '[bracket', '{brace', '> quote', '| pipe', '! bang'];
for (const title of cases) {
const out = htmlToMarkdown('<p>x</p>', { title, schemaVersion: 1 });
expect(out).toContain(`title: ${JSON.stringify(title)}\n`);
}
});

it('quotes titles with leading or trailing whitespace', () => {
const out = htmlToMarkdown('<p>x</p>', { title: ' padded ', schemaVersion: 1 });
expect(out).toContain('title: " padded "\n');
});

it('strips javascript: links but keeps the visible text', () => {
const out = htmlToMarkdown('<p><a href="javascript:alert(1)">x</a></p>', META);
expect(out).toContain('x');
Expand Down
14 changes: 13 additions & 1 deletion packages/exporters/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,20 @@ function renderFrontmatter(meta: MarkdownMeta): string {
return `${lines.join('\n')}\n`;
}

// YAML 1.2 reserved indicator chars that, at the start of a plain scalar,
// change parsing (sequence/flow/anchor/alias/directive/etc). Strings with
// leading/trailing whitespace also need quoting to round-trip correctly.
const YAML_LEADING_INDICATOR = /^[-?:,[\]{}#&*!|>'"%@`]/;
const YAML_NEEDS_QUOTING = /[:#"'\n]/;

function escapeYaml(value: string): string {
if (/[:#"'\n]/.test(value)) return JSON.stringify(value);
if (
YAML_NEEDS_QUOTING.test(value) ||
YAML_LEADING_INDICATOR.test(value) ||
value !== value.trim()
) {
return JSON.stringify(value);
}
return value;
}

Expand Down
Loading