Commit 37ce116d authored by NGPixel's avatar NGPixel

fix: handle headers for all editors (#1127)

parent 4b0f5fde
......@@ -91,7 +91,6 @@
"lodash": "4.17.15",
"markdown-it": "10.0.0",
"markdown-it-abbr": "1.0.4",
"markdown-it-anchor": "5.2.5",
"markdown-it-attrs": "3.0.1",
"markdown-it-emoji": "1.4.0",
"markdown-it-expand-tabs": "1.0.13",
......
const _ = require('lodash')
const cheerio = require('cheerio')
const uslug = require('uslug')
/* global WIKI */
......@@ -142,6 +143,43 @@ module.exports = {
}
}
// --------------------------------
// Add header handles
// --------------------------------
let headers = []
$('h1,h2,h3,h4,h5,h6').each((i, elm) => {
if ($(elm).attr('id')) {
return
}
let headerSlug = uslug($(elm).text())
// -> Cannot start with a number (CSS selector limitation)
if (headerSlug.match(/^\d/)) {
headerSlug = `h-${headerSlug}`
}
// -> Make sure header is unique
if (headers.indexOf(headerSlug) >= 0) {
let isUnique = false
let hIdx = 1
while (!isUnique) {
const headerSlugTry = `${headerSlug}-${hIdx}`
if (headers.indexOf(headerSlugTry) < 0) {
isUnique = true
headerSlug = headerSlugTry
}
hIdx++
}
}
// -> Add anchor
$(elm).attr('id', headerSlug).addClass('toc-header')
$(elm).prepend(`<a class="toc-anchor" href="#${headerSlug}">&#xB6;</a> `)
headers.push(headerSlug)
})
return $.html('body').replace('<body>', '').replace('</body>', '')
}
}
const md = require('markdown-it')
const mdAnchor = require('markdown-it-anchor')
const mdAttrs = require('markdown-it-attrs')
const _ = require('lodash')
const uslug = require('uslug')
const quoteStyles = {
Chinese: '””‘’',
......@@ -32,14 +30,6 @@ module.exports = {
}
})
mkdown.use(mdAnchor, {
slugify: s => uslug(s),
permalink: true,
permalinkClass: 'toc-anchor',
permalinkSymbol: '¶',
permalinkBefore: true
})
mkdown.use(mdAttrs)
for (let child of this.children) {
......
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment