Skip to content

Commit 0bb915b

Browse files
committed
Fix formatting breaking heading links
1 parent ee7402c commit 0bb915b

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Fixed underscores appearing in the default page title when present in the URL.
99
- Fixed links not being automatically capitalised.
1010
- Fixed named references not working.
11+
- Fixed headings not working when containing formatting.
1112
- Fixed all index numbers in the TOC being '1'.
1213

1314
## 1.3.4

src/compile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export function compile(dir: string = '.', config: Config = {}): void {
5353
let toc = '';
5454
let headings = Array.from(parsedContent.match(/<h\d[^>]*>.+?<\/h\d>/gs) || []);
5555
headings.forEach(match => {
56-
const text: string = match.replace(/\s*<\/?h\d[^>]*>\s*/g, '');
56+
const headingInner: string = match.replace(/\s*<\/?h\d[^>]*>\s*/g, '');
57+
const text = headingInner.replace(/<.+?>/g, ''); // remove tags from inner
5758
const lvl: number = +(match.match(/\d/g)?.[0] || -1);
5859
toc += `${`<ol>`.repeat(lvl - 1)} <li> <a href="#${encodeURI(text.replace(/ /g, '_'))}">${text}</a> </li> ${`</ol>`.repeat(lvl - 1)}`;
5960
});

src/parse.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ export function parse(data: string, config: Config = {}): Result {
311311
.replace(re(r`'' ([^']+?) ''`), '<i>$1</i>')
312312

313313
// Headings: ==heading==
314-
.replace(re(r`^ (=+) \s* (.+?) \s* \1 \s* $`), (_, lvl, txt) => `<h${lvl.length} id="${encodeURI(txt.replace(/ /g, '_'))}">${txt}</h${lvl.length}>`)
314+
.replace(re(r`^ (=+) \s* (.+?) \s* \1 \s* $`), (_, lvl, txt) => {
315+
const linkForm = encodeURI(txt.replace(/ /g, '_').replace(/<.+?>/g, ''));
316+
return `<h${lvl.length} id="${linkForm}">${txt}</h${lvl.length}>`;
317+
})
315318

316319
// Bulleted list: *item
317320
.replace(re(r`^ (\*+) (.+?) $`), (_, lvl, content) => {

test/src/example.wiki

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
=== Subheading ===
99
== Broken heading ===
1010
====== Small heading ======
11+
== ''fancy'' '''heading'''! ==
1112
</div>
1213

1314
<div style="border:1px solid">

0 commit comments

Comments
 (0)