-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a9a353
commit 2e4d1e0
Showing
15 changed files
with
114 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,60 @@ | ||
import {walkRecursiveAsync} from '../markdownNodesUtils.ts'; | ||
import {MarkdownNodes} from '../MarkdownNodes.ts'; | ||
import {MarkdownNodes, MarkdownTextNode} from '../MarkdownNodes.ts'; | ||
|
||
export async function rewriteHeaders(markdownChunks: MarkdownNodes) { | ||
await walkRecursiveAsync(markdownChunks.body, async (chunk) => { | ||
let inPre = false; | ||
await walkRecursiveAsync(markdownChunks.body, async (chunk, ctx: { nodeIdx: number }) => { | ||
if (chunk.isTag && 'PRE' === chunk.tag) { | ||
inPre = true; | ||
} | ||
|
||
if (inPre) { | ||
if (chunk.isTag) { | ||
for (let j = chunk.children.length - 1; j >= 0; j--) { | ||
const child = chunk.children[j]; | ||
if (child.isTag && child.tag === 'BOOKMARK/') { | ||
chunk.children.splice(j, 1); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (chunk.isTag && ['H1', 'H2', 'H3', 'H4'].includes(chunk.tag)) { | ||
if (chunk.children.length > 1) { | ||
const first = chunk.children[0]; | ||
if (first.isTag && first.tag === 'BOOKMARK/') { | ||
const toMove = chunk.children.splice(0, 1); | ||
if (chunk.children.length === 1) { | ||
const child = chunk.children[0]; | ||
if (child.isTag && child.tag === 'BOOKMARK/') { | ||
chunk.parent.children.splice(ctx.nodeIdx, 1, child); | ||
return; | ||
} | ||
} | ||
for (let j = 0; j < chunk.children.length - 1; j++) { | ||
const child = chunk.children[j]; | ||
if (child.isTag && child.tag === 'BOOKMARK/') { | ||
const toMove = chunk.children.splice(j, 1); | ||
chunk.children.splice(chunk.children.length, 0, ...toMove); | ||
break; | ||
} | ||
} | ||
} | ||
}, {}, async (chunk) => { | ||
if (chunk.isTag && 'PRE' === chunk.tag) { | ||
inPre = false; | ||
} | ||
}); | ||
|
||
await walkRecursiveAsync(markdownChunks.body, async (chunk, ctx: { nodeIdx: number }) => { | ||
if (chunk.isTag && 'BOOKMARK/' === chunk.tag) { | ||
if (chunk.parent.children.length < 2) { | ||
return; | ||
} | ||
const space: MarkdownTextNode = { | ||
isTag: false, | ||
text: ' ', | ||
comment: 'rewriteHeaders.ts: space before anchor' | ||
}; | ||
chunk.parent.children.splice(ctx.nodeIdx, 0, space); | ||
return { nodeIdx: ctx.nodeIdx + 1 }; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.