-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable automatic conversion of Markdown to HTML (#12)
- Loading branch information
Showing
13 changed files
with
136 additions
and
1,362 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,93 @@ | ||
const { JSDOM } = require('jsdom') | ||
|
||
const { slugify } = require('./slugify') | ||
const md = require('./md') | ||
|
||
// "If you return a `function`, we'll use the return value from that function." | ||
// Source: https://www.11ty.dev/docs/data-js/ | ||
module.exports = () => | ||
function getTocItems(html) { | ||
const uniqueSlugs = new Set() | ||
const dom = new JSDOM(html) | ||
|
||
return [...dom.window.document.body.querySelectorAll('h2, h3')].reduce( | ||
(result, heading) => { | ||
const item = createItem(heading) | ||
const prevH3 = result[result.length - 1] | ||
|
||
if (heading.tagName === 'H2') { | ||
result.push(item) | ||
} else if (heading.tagName === 'H3') { | ||
prevH3.subItems.push(item) | ||
} | ||
|
||
return result | ||
}, | ||
[] | ||
) | ||
module.exports = () => (markdown) => { | ||
const tokens = md.parse(markdown, {}) | ||
|
||
function createItem(heading) { | ||
const nodes = [...heading.childNodes[0].childNodes] | ||
|
||
const itemHtml = nodes.reduce( | ||
(result, node) => result + (node.outerHTML || node.textContent), | ||
'' | ||
) | ||
|
||
const text = nodes.reduce((result, node) => result + node.textContent, '') | ||
const slug = getUniqueSlug(text) | ||
/* | ||
Example `tokens` for `## Heading text with `inline code`; | ||
the `link_open` and `link_close` tokens are generated by `markdown-it-anchor`: | ||
[ | ||
// ... | ||
{ | ||
type: 'heading_open', | ||
tag: 'h2', | ||
content: '', | ||
// ... | ||
}, | ||
{ | ||
type: 'inline', | ||
tag: '', | ||
content: '', | ||
children: [ | ||
{ | ||
type: 'link_open', | ||
tag: 'a', | ||
content: '', | ||
// ... | ||
}, | ||
{ | ||
type: 'text', | ||
tag: '', | ||
content: 'Heading text with ', | ||
// ... | ||
}, | ||
{ | ||
type: 'code_inline', | ||
tag: 'code', | ||
content: 'inline code', | ||
// ... | ||
}, | ||
{ | ||
type: 'link_close', | ||
tag: 'a', | ||
content: '', | ||
// ... | ||
} | ||
], | ||
// ... | ||
}, | ||
{ | ||
type: 'heading_close', | ||
tag: 'h2', | ||
content: '', | ||
// ... | ||
}, | ||
// ... | ||
] | ||
*/ | ||
|
||
return { | ||
content: itemHtml, | ||
href: `#${slug}`, | ||
subItems: [], | ||
} | ||
const headingIndexes = tokens.reduce((indexes, token, index) => { | ||
if (token.type === 'heading_open' && ['h2', 'h3'].includes(token.tag)) { | ||
indexes.push(index) | ||
} | ||
return indexes | ||
}, []) | ||
|
||
function getUniqueSlug(text) { | ||
const slug = slugify(text) | ||
return headingIndexes.reduce((items, index) => { | ||
const headingOpenToken = tokens[index] | ||
const headingContentTokens = tokens[index + 1].children | ||
const linkOpenToken = headingContentTokens[0] | ||
|
||
let uniqueSlug = slug | ||
let i = 1 | ||
const content = md.renderer.render( | ||
// Omit `link_open` and `link_close` | ||
headingContentTokens.slice(1, -1) | ||
) | ||
|
||
while (uniqueSlugs.has(uniqueSlug)) { | ||
uniqueSlug = `${slug}-${i}` | ||
i += 1 | ||
} | ||
const item = { | ||
content, | ||
href: linkOpenToken.attrGet('href'), | ||
subItems: [], | ||
} | ||
|
||
uniqueSlugs.add(uniqueSlug) | ||
return uniqueSlug | ||
if (headingOpenToken.tag === 'h2') { | ||
items.push(item) | ||
} else { | ||
const prevH2 = items[items.length - 1] | ||
prevH2.subItems.push(item) | ||
} | ||
} | ||
|
||
return items | ||
}, []) | ||
} |
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 |
---|---|---|
|
@@ -12,4 +12,4 @@ block content | |
#continue-reading.mb-12 | ||
|
||
+toc(content) | ||
!= content | ||
!= md.render(content) |
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.