Skip to content

Duplicate header names aren't differentiated in IDs #879

Closed

Description

@ChiriVulpes

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

Activity

  1. charlieTheBotDev commented on Apr 27, 2017

    @charlieTheBotDev

    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

  2. joshbruce commented on Dec 25, 2017

    @joshbruce
    Member

    See #981

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions