Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Added slugified ids to md headers #1402

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions helpers/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,46 @@
return defaultLinkOpenRenderer(tokens, idx, options, env, self)
}


Check failure on line 145 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Replace `⏎··const·slugify_header·=·function` with `··const·slugify_header·=·function·`
const slugify_header = function(str, used_headers) {

Check failure on line 146 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Identifier 'slugify_header' is not in camel case

Check failure on line 146 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Identifier 'used_headers' is not in camel case
let slug = str.trim().toLowerCase()

Check failure on line 147 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Replace `.trim()` with `⏎······.trim()⏎······`
.normalize("NFKD") // Splits graphemes and diacritics (so é → e and ̀)

Check failure on line 148 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Replace `"NFKD"` with `'NFKD'`
.replace(/[./_,:;]/g, '-')
.replace(/[^a-z0-9 -]/g, '') // Removes invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace with -
.replace(/-+/g, '-') // collapse dashes
if( used_headers[slug] ) {

Check failure on line 153 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Replace `(·used_headers[slug]·` with `·(used_headers[slug]`

Check failure on line 153 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Identifier 'used_headers' is not in camel case
used_headers[slug]++

Check failure on line 154 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Identifier 'used_headers' is not in camel case
slug += used_headers[slug]

Check failure on line 155 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Identifier 'used_headers' is not in camel case
} else {
used_headers[slug] += '-' + 1

Check failure on line 157 in helpers/parse.js

View workflow job for this annotation

GitHub Actions / build

Identifier 'used_headers' is not in camel case
}
return slug;
};

// Mostly Based on:
// https://github.com/leff/markdown-it-named-headers
// https://gist.github.com/codeguy/6684588
const originalHeadingOpen = md.renderer.rules.heading_open
if (originalHeadingOpen) {
console.log("TRUE")
}
console.log(originalHeadingOpen)
md.renderer.rules.heading_open = function (tokens, idx, something, somethingelse, self) {
const used_headers = {}

tokens[idx].attrs = tokens[idx].attrs || []

const header = tokens[idx + 1].children.reduce(function (acc, t) {
return acc + t.content
}, '');

const slug = slugify_header(header, used_headers)
tokens[idx].attrs.push(['id', slug])

return self.renderToken.apply(self, arguments)
};

return md
}

Expand Down