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

Heading link id/name sanitation for GitHub flavored MarkDown #210

Open
S0AndS0 opened this issue Mar 25, 2020 · 0 comments
Open

Heading link id/name sanitation for GitHub flavored MarkDown #210

S0AndS0 opened this issue Mar 25, 2020 · 0 comments
Labels
template Requires a change to a dmd partial/helper

Comments

@S0AndS0
Copy link

S0AndS0 commented Mar 25, 2020

It should be possible, at least for GitHub flavored MarkDown, to output documents with MarkDown style links.

Here's a function that I've pulled out from one of my own Mustache templated projects, github-utilities/dot-github...

/**
 * Strips and/or replaces characters for heading id/name linking
 * @extends renderCallback
 * @function stripForID
 * @return {renderCallback}
 */
const stripForID = () => {
    const renderCallback = (text, render) => {
        if (!!render.gfm) {
            return render(text).trim()
                .toLowerCase()
                .replace(/<[^>]*>?/gm, '--')
                .replace(/[^a-z0-9_\- ]/g, '')
                .replace(/ +/g, '-');
        }
        return render(text).trim()
            .toLowerCase()
            .replace(/[^a-z0-9_\- ]/g, '')
            .replace(/ +/g, '-');
    };
    return renderCallback;
}

... an example of how it's used...

# Heading Text
[heading__tag]:
  #{{#stripForID}}Tag that may contain bad characters?!{{/stripForID}}
  "description shown when hovered over"

Lorem ipsum dolor sit amet, [consectetur][heading__tag] adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

... and an example of MarkDown output...

# Heading Text
[heading__tag]:
  #tag-that-may-contain-bad-characters
  "description shown when hovered over"

Lorem ipsum dolor sit amet, [consectetur][heading__tag] adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

If this seems like a good feature feel free to notify me of what files need changed, and I'll be both able and willing to submit some Pull Request(s).

@75lb 75lb added the template Requires a change to a dmd partial/helper label Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
template Requires a change to a dmd partial/helper
Development

No branches or pull requests

2 participants