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

Prevent header collisions on generation. #126

Merged
merged 1 commit into from Nov 24, 2014
Merged

Prevent header collisions on generation. #126

merged 1 commit into from Nov 24, 2014

Commits on Nov 24, 2014

  1. Prevent generated header collisions, less naively.

    > This is a rework of an earlier version of this code.
    
    The automatic header ID generation code submitted in #125 has a subtle
    bug where it will use the same ID for multiple headers with identical
    text. In the case below, all the headers are rendered a `<h1
    id="header">Header</h1>`.
    
      ```markdown
      # Header
      # Header
      # Header
      # Header
      ```
    
    This change is a simple but robust approach that uses an incrementing
    counter and pre-checking to prevent header collision. (The above would
    be rendered as `header`, `header-1`, `header-2`, and `header-3`.) In
    more complex cases, it will append a new counter suffix (`-1`), like so:
    
      ```markdown
      # Header
      # Header 1
      # Header
      # Header
      ```
    
    This will generate `header`, `header-1`, `header-1-1`, and `header-1-2`.
    
    This code has two additional changes over the prior version:
    
    1.  Rather than reimplementing @shurcooL’s anchor sanitization code, I
        have imported it as from
        `github.com/shurcooL/go/github_flavored_markdown/sanitized_anchor_name`.
    
    2.  The markdown block parser is now only interested in *generating* a
        sanitized anchor name, not with ensuring its uniqueness. That code
        has been moved to the HTML renderer. This means that if the HTML
        renderer is modified to identify all unique headers prior to
        rendering, the hackish nature of the collision detection can be
        eliminated.
    halostatue committed Nov 24, 2014
    Configuration menu
    Copy the full SHA
    40f28ee View commit details
    Browse the repository at this point in the history