Skip to content

Commit 3a4bc33

Browse files
committed
Fixed duplicate TOC Title jump error
1 parent fdb1ef5 commit 3a4bc33

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

browser/lib/markdown-toc-generator.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import toc from 'markdown-toc'
66
import diacritics from 'diacritics-map'
77
import stripColor from 'strip-color'
8+
import mdlink from 'markdown-link'
89

910
const EOL = require('os').EOL
1011

@@ -42,6 +43,12 @@ function caseSensitiveSlugify (str) {
4243
return str
4344
}
4445

46+
function linkify (tok, text, slug, opts) {
47+
var uniqeID = opts.num === 0 ? '' : '-' + opts.num
48+
tok.content = mdlink(text, '#' + slug + uniqeID)
49+
return tok
50+
}
51+
4552
const TOC_MARKER_START = '<!-- toc -->'
4653
const TOC_MARKER_END = '<!-- tocstop -->'
4754

@@ -84,7 +91,7 @@ export function generateInEditor (editor) {
8491
* @returns generatedTOC String containing generated TOC
8592
*/
8693
export function generate (markdownText) {
87-
const generatedToc = toc(markdownText, {slugify: caseSensitiveSlugify})
94+
const generatedToc = toc(markdownText, {slugify: caseSensitiveSlugify, linkify: linkify})
8895
return TOC_MARKER_START + EOL + EOL + generatedToc.content + EOL + EOL + TOC_MARKER_END
8996
}
9097

browser/lib/markdown.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import _ from 'lodash'
77
import ConfigManager from 'browser/main/lib/ConfigManager'
88
import katex from 'katex'
99
import { lastFindInArray } from './utils'
10-
import ee from 'browser/main/lib/eventEmitter'
10+
import anchor from '@enyaxu/markdown-it-anchor'
1111

1212
function createGutter (str, firstLineNumber) {
1313
if (Number.isNaN(firstLineNumber)) firstLineNumber = 1
@@ -118,12 +118,13 @@ class Markdown {
118118
this.md.use(require('markdown-it-imsize'))
119119
this.md.use(require('markdown-it-footnote'))
120120
this.md.use(require('markdown-it-multimd-table'))
121-
this.md.use(require('markdown-it-named-headers'), {
122-
slugify: (header) => {
123-
return encodeURI(header.trim()
121+
this.md.use(anchor, {
122+
slugify: (title) => {
123+
var slug = encodeURI(title.trim()
124124
.replace(/[\]\[\!\"\#\$\%\&\'\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\_\{\|\}\~]/g, '')
125125
.replace(/\s+/g, '-'))
126126
.replace(/\-+$/, '')
127+
return slug
127128
}
128129
})
129130
this.md.use(require('markdown-it-kbd'))

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"homepage": "https://boostnote.io",
4949
"dependencies": {
50+
"@enyaxu/markdown-it-anchor": "^5.0.2",
5051
"@rokt33r/markdown-it-math": "^4.0.1",
5152
"@rokt33r/season": "^5.3.0",
5253
"@susisu/mte-kernel": "^2.0.0",
@@ -80,7 +81,6 @@
8081
"markdown-it-imsize": "^2.0.1",
8182
"markdown-it-kbd": "^1.1.1",
8283
"markdown-it-multimd-table": "^2.0.1",
83-
"markdown-it-named-headers": "^0.0.4",
8484
"markdown-it-plantuml": "^1.1.0",
8585
"markdown-it-smartarrows": "^1.0.1",
8686
"markdown-it-sub": "^1.0.0",
@@ -104,10 +104,10 @@
104104
"sander": "^0.5.1",
105105
"sanitize-html": "^1.18.2",
106106
"striptags": "^2.2.1",
107+
"turndown": "^4.0.2",
108+
"turndown-plugin-gfm": "^1.0.2",
107109
"unique-slug": "2.0.0",
108-
"uuid": "^3.2.1",
109-
"turndown":"^4.0.2",
110-
"turndown-plugin-gfm":"^1.0.2"
110+
"uuid": "^3.2.1"
111111
},
112112
"devDependencies": {
113113
"ava": "^0.25.0",

yarn.lock

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
dependencies:
5959
arrify "^1.0.1"
6060

61+
"@enyaxu/markdown-it-anchor@^5.0.2":
62+
version "5.0.2"
63+
resolved "https://registry.yarnpkg.com/@enyaxu/markdown-it-anchor/-/markdown-it-anchor-5.0.2.tgz#d173f7b60b492aabc17dfba864c4d071f5595f72"
64+
integrity sha512-HBQ+by3IFHh2i5nw8fzn9qrdA+6uwzre68EzHpBX/WrwgnKrfvckPzdi7MphKp2C617edfpeibucslHDNPYkvQ==
65+
6166
"@ladjs/time-require@^0.1.4":
6267
version "0.1.4"
6368
resolved "https://registry.yarnpkg.com/@ladjs/time-require/-/time-require-0.1.4.tgz#5c615d75fd647ddd5de9cf6922649558856b21a1"
@@ -5878,12 +5883,6 @@ markdown-it-multimd-table@^2.0.1:
58785883
dependencies:
58795884
markdown-it "^5.0.3"
58805885

5881-
markdown-it-named-headers@^0.0.4:
5882-
version "0.0.4"
5883-
resolved "https://registry.yarnpkg.com/markdown-it-named-headers/-/markdown-it-named-headers-0.0.4.tgz#82efc28324240a6b1e77b9aae501771d5f351c1f"
5884-
dependencies:
5885-
string "^3.0.1"
5886-
58875886
markdown-it-plantuml@^1.1.0:
58885887
version "1.1.0"
58895888
resolved "https://registry.yarnpkg.com/markdown-it-plantuml/-/markdown-it-plantuml-1.1.0.tgz#9ab8bfa09a02d80845e16e60f87a488edb50fdde"
@@ -8454,10 +8453,6 @@ string-width@^1.0.1, string-width@^1.0.2:
84548453
is-fullwidth-code-point "^2.0.0"
84558454
strip-ansi "^4.0.0"
84568455

8457-
string@^3.0.1:
8458-
version "3.3.3"
8459-
resolved "https://registry.yarnpkg.com/string/-/string-3.3.3.tgz#5ea211cd92d228e184294990a6cc97b366a77cb0"
8460-
84618456
string_decoder@^0.10.25, string_decoder@~0.10.x:
84628457
version "0.10.31"
84638458
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"

0 commit comments

Comments
 (0)