-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate header names aren't differentiated in IDs #879
Comments
I'm using Input
Output
ES6 Versionconst content = `
<!-- toc -->
# test
# test
`
const marked = require('marked')
const toc = require('markdown-toc')
const renderer = new marked.Renderer()
const headings = []
renderer.heading = (text, level) => {
const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-')
const duplicateIndex = headings.map(({ text }) => text).indexOf(escapedText)
let duplicateText = undefined
if (duplicateIndex === -1) {
headings.push({
text: escapedText,
count: 0
})
} else {
headings[duplicateIndex].count++
duplicateText = `${escapedText}-${headings[duplicateIndex].count}`
}
return `<h${level} id="${duplicateText || escapedText}">${text}</h${level}>\n`
}
const parsedMarkdown = marked(toc.insert(content), { renderer })
console.log('INPUT\n' + content + '\nEND INPUT')
console.log('OUTPUT\n' + parsedMarkdown + '\nEND OUTPUT') ES5 Versionvar content = '<!-- toc -->\n\n# test\n# test'
var marked = require('marked')
var toc = require('markdown-toc')
var renderer = new marked.Renderer()
var headings = []
renderer.heading = function (text, level) {
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-')
var duplicateIndex = headings.map(function (h) { return h.text }).indexOf(escapedText)
var duplicateText
if (duplicateIndex === -1) {
headings.push({
text: escapedText,
count: 0
})
} else {
headings[duplicateIndex].count++
duplicateText = escapedText + '-' + headings[duplicateIndex].count
}
return '<h' + level + ' id="'+ (duplicateText || escapedText) + '">' + text + '</h' + level + '>\n'
}
var parsedMarkdown = marked(toc.insert(content), { renderer: renderer })
console.log('INPUT\n' + content + '\nEND INPUT')
console.log('OUTPUT\n' + parsedMarkdown + '\nEND OUTPUT') I'm pretty sure this is how all markdown parsers handle duplicates It also works fine if you remove the |
This is also causing Eclipse Orion bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=525834 |
See #981 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Other markdown parsers support having multiple sections with the same name, but this one doesn't.
Input:
Expected Output:
Actual Output:
Supporting this means subsections can go by the same name. (for instance, in an md file I have, I have a section for specifications of files, and another section for examples of the files in action, and each section needs a subsection of the same name, as they're the same file)
There are probably other use cases as well. Also supporting this would make it not be invalid html =P
The text was updated successfully, but these errors were encountered: