Skip to content
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

Closed
ChiriVulpes opened this issue Mar 30, 2017 · 3 comments
Closed

Duplicate header names aren't differentiated in IDs #879

ChiriVulpes opened this issue Mar 30, 2017 · 3 comments

Comments

@ChiriVulpes
Copy link

Other markdown parsers support having multiple sections with the same name, but this one doesn't.

Input:

# test
# test

Expected Output:

<h1 id="test">test</h1>
<h1 id="test-1">test</h1>

Actual Output:

<h1 id="test">test</h1>
<h1 id="test">test</h1>

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

@charlieTheBotDev
Copy link

I'm using markdown-toc to insert a table of contents in one of my projects and wrote a workaround for this that handles duplicates in the same way.

Input

<!-- toc -->

# test
# test

Output

<!-- toc -->
<ul>
<li><a href="#test">test</a></li>
<li><a href="#test-1">test</a></li>
</ul>
<!-- tocstop -->
<h1 id="test">test</h1>
<h1 id="test-1">test</h1>

ES6 Version

const 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 Version

var 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 <!-- toc --> and don't use toc.insert

@carmacleod
Copy link

This is also causing Eclipse Orion bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=525834

@joshbruce
Copy link
Member

See #981

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants