MCPcopy Create free account
hub / github.com/hackmdio/codimd / createHeaderId

Function createHeaderId

public/js/extra.js:1047–1075  ·  view source on GitHub ↗
(headerContent, headerIds = null)

Source from the content-addressed store, hash-verified

1045}
1046
1047const createHeaderId = (headerContent, headerIds = null) => {
1048 // to escape characters not allow in css and humanize
1049 const slug = slugifyWithUTF8(headerContent)
1050 let id
1051 if (window.linkifyHeaderStyle === 'keep-case') {
1052 id = slug
1053 } else if (window.linkifyHeaderStyle === 'lower-case') {
1054 // to make compatible with GitHub, GitLab, Pandoc and many more
1055 id = slug.toLowerCase()
1056 } else if (window.linkifyHeaderStyle === 'gfm') {
1057 // see GitHub implementation reference:
1058 // https://gist.github.com/asabaylus/3071099#gistcomment-1593627
1059 // it works like 'lower-case', but ...
1060 const idBase = slug.toLowerCase()
1061 id = idBase
1062 if (headerIds !== null) {
1063 // ... making sure the id is unique
1064 let i = 1
1065 while (headerIds.has(id)) {
1066 id = idBase + '-' + i
1067 i++
1068 }
1069 headerIds.add(id)
1070 }
1071 } else {
1072 throw new Error('Unknown linkifyHeaderStyle value "' + window.linkifyHeaderStyle + '"')
1073 }
1074 return id
1075}
1076
1077const linkifyAnchors = (level, containingElement) => {
1078 const headers = containingElement.getElementsByTagName(`h${level}`)

Callers 2

linkifyAnchorsFunction · 0.85
deduplicatedHeaderIdFunction · 0.85

Calls 1

slugifyWithUTF8Function · 0.85

Tested by

no test coverage detected