Skip to content

Commit

Permalink
Fixed duplicate TOC Title jump error
Browse files Browse the repository at this point in the history
  • Loading branch information
enyaxu committed Nov 10, 2018
1 parent fdb1ef5 commit c3a0483
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
9 changes: 8 additions & 1 deletion browser/lib/markdown-toc-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import toc from 'markdown-toc'
import diacritics from 'diacritics-map'
import stripColor from 'strip-color'
import mdlink from "markdown-link"

const EOL = require('os').EOL

Expand Down Expand Up @@ -42,6 +43,12 @@ function caseSensitiveSlugify (str) {
return str
}

function linkify (tok, text, slug, opts) {
var uniqeID = opts.num === 0 ? '' : '-' + opts.num
tok.content = mdlink(text, '#' + slug + uniqeID)
return tok
}

const TOC_MARKER_START = '<!-- toc -->'
const TOC_MARKER_END = '<!-- tocstop -->'

Expand Down Expand Up @@ -84,7 +91,7 @@ export function generateInEditor (editor) {
* @returns generatedTOC String containing generated TOC
*/
export function generate (markdownText) {
const generatedToc = toc(markdownText, {slugify: caseSensitiveSlugify})
const generatedToc = toc(markdownText, {slugify: caseSensitiveSlugify, linkify: linkify})
return TOC_MARKER_START + EOL + EOL + generatedToc.content + EOL + EOL + TOC_MARKER_END
}

Expand Down
9 changes: 5 additions & 4 deletions browser/lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _ from 'lodash'
import ConfigManager from 'browser/main/lib/ConfigManager'
import katex from 'katex'
import { lastFindInArray } from './utils'
import ee from 'browser/main/lib/eventEmitter'
import anchor from '@enyaxu/markdown-it-anchor'

function createGutter (str, firstLineNumber) {
if (Number.isNaN(firstLineNumber)) firstLineNumber = 1
Expand Down Expand Up @@ -118,12 +118,13 @@ class Markdown {
this.md.use(require('markdown-it-imsize'))
this.md.use(require('markdown-it-footnote'))
this.md.use(require('markdown-it-multimd-table'))
this.md.use(require('markdown-it-named-headers'), {
slugify: (header) => {
return encodeURI(header.trim()
this.md.use(anchor, {
slugify: (title) => {
var slug = encodeURI(title.trim()
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~]/g, '')
.replace(/\s+/g, '-'))
.replace(/\-+$/, '')
return slug
}
})
this.md.use(require('markdown-it-kbd'))
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"homepage": "https://boostnote.io",
"dependencies": {
"@enyaxu/markdown-it-anchor": "^5.0.2",
"@rokt33r/markdown-it-math": "^4.0.1",
"@rokt33r/season": "^5.3.0",
"@susisu/mte-kernel": "^2.0.0",
Expand Down Expand Up @@ -80,7 +81,6 @@
"markdown-it-imsize": "^2.0.1",
"markdown-it-kbd": "^1.1.1",
"markdown-it-multimd-table": "^2.0.1",
"markdown-it-named-headers": "^0.0.4",
"markdown-it-plantuml": "^1.1.0",
"markdown-it-smartarrows": "^1.0.1",
"markdown-it-sub": "^1.0.0",
Expand All @@ -104,10 +104,10 @@
"sander": "^0.5.1",
"sanitize-html": "^1.18.2",
"striptags": "^2.2.1",
"turndown": "^4.0.2",
"turndown-plugin-gfm": "^1.0.2",
"unique-slug": "2.0.0",
"uuid": "^3.2.1",
"turndown":"^4.0.2",
"turndown-plugin-gfm":"^1.0.2"
"uuid": "^3.2.1"
},
"devDependencies": {
"ava": "^0.25.0",
Expand Down
15 changes: 5 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
dependencies:
arrify "^1.0.1"

"@enyaxu/markdown-it-anchor@^5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@enyaxu/markdown-it-anchor/-/markdown-it-anchor-5.0.2.tgz#d173f7b60b492aabc17dfba864c4d071f5595f72"
integrity sha512-HBQ+by3IFHh2i5nw8fzn9qrdA+6uwzre68EzHpBX/WrwgnKrfvckPzdi7MphKp2C617edfpeibucslHDNPYkvQ==

"@ladjs/time-require@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@ladjs/time-require/-/time-require-0.1.4.tgz#5c615d75fd647ddd5de9cf6922649558856b21a1"
Expand Down Expand Up @@ -5878,12 +5883,6 @@ markdown-it-multimd-table@^2.0.1:
dependencies:
markdown-it "^5.0.3"

markdown-it-named-headers@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/markdown-it-named-headers/-/markdown-it-named-headers-0.0.4.tgz#82efc28324240a6b1e77b9aae501771d5f351c1f"
dependencies:
string "^3.0.1"

markdown-it-plantuml@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/markdown-it-plantuml/-/markdown-it-plantuml-1.1.0.tgz#9ab8bfa09a02d80845e16e60f87a488edb50fdde"
Expand Down Expand Up @@ -8454,10 +8453,6 @@ string-width@^1.0.1, string-width@^1.0.2:
is-fullwidth-code-point "^2.0.0"
strip-ansi "^4.0.0"

string@^3.0.1:
version "3.3.3"
resolved "https://registry.yarnpkg.com/string/-/string-3.3.3.tgz#5ea211cd92d228e184294990a6cc97b366a77cb0"

string_decoder@^0.10.25, string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
Expand Down

0 comments on commit c3a0483

Please sign in to comment.