Skip to content

Commit 49a4b5f

Browse files
committed
no more line anchors
1 parent c6eff15 commit 49a4b5f

3 files changed

Lines changed: 19 additions & 22 deletions

File tree

browser/components/MarkdownPreview.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ export default class MarkdownPreview extends React.Component {
141141
}
142142

143143
scrollTo (targetRow) {
144-
let lineAnchors = this.getWindow().document.querySelectorAll('a.lineAnchor')
144+
let lineAnchors = this.getWindow().document.querySelectorAll('[data-line]')
145145

146146
for (let index = 0; index < lineAnchors.length; index++) {
147147
let lineAnchor = lineAnchors[index]
148-
let row = parseInt(lineAnchor.getAttribute('data-key'))
148+
let row = parseInt(lineAnchor.getAttribute('data-line'))
149149
if (row > targetRow) {
150150
let targetAnchor = lineAnchors[index - 1]
151151
targetAnchor != null && this.getWindow().scrollTo(0, targetAnchor.offsetTop)

browser/components/markdown.styl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ a
102102
background-color alpha(#FFC95C, 0.3)
103103
&:visited
104104
color brandColor
105-
&.lineAnchor
106-
padding 0
107-
margin 0
108-
display inline-block
109-
font-size 0
110-
height 0
111-
width 0
112105
hr
113106
border-top none
114107
border-bottom solid 1px borderColor
@@ -148,9 +141,6 @@ h6
148141
line-height 1.4em
149142
margin 1em 0 1em
150143
color #777
151-
152-
*:not(a.lineAnchor) + p, *:not(a.lineAnchor) + blockquote, *:not(a.lineAnchor) + ul, *:not(a.lineAnchor) + ol, *:not(a.lineAnchor) + pre
153-
margin-top 1em
154144
p
155145
line-height 1.6em
156146
margin 0 0 1em
@@ -196,8 +186,6 @@ code
196186
font-size 0.85em
197187
text-decoration none
198188
margin-right 2px
199-
*:not(a.lineAnchor) + code
200-
margin-left 2px
201189
pre
202190
padding 0.5em !important
203191
border solid 1px alpha(borderColor, 0.5)

browser/lib/markdown.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import markdownit from 'markdown-it'
22
import emoji from 'markdown-it-emoji'
33
import math from '@rokt33r/markdown-it-math'
44
import hljs from 'highlight.js'
5+
import _ from 'lodash'
56

67
const katex = window.katex
78

@@ -61,16 +62,24 @@ md.use(math, {
6162
})
6263
md.use(require('markdown-it-footnote'))
6364

65+
let originalRender = md.renderer.render
66+
md.renderer.render = function render (tokens, options, env) {
67+
tokens.forEach((token) => {
68+
switch (token.type) {
69+
case 'heading_open':
70+
case 'paragraph_open':
71+
case 'blockquote_open':
72+
case 'table_open':
73+
token.attrPush(['data-line', token.map[0]])
74+
}
75+
})
76+
let result = originalRender.call(md.renderer, tokens, options, env)
77+
return result
78+
}
6479
window.md = md
6580

6681
export default function markdown (content) {
67-
if (content == null) content = ''
68-
content = content.toString()
69-
.split('\n')
70-
.map((line, index) => {
71-
if (line.trim().length === 0) return ''
72-
return line + '<a class=\'lineAnchor\' data-key=\'' + (index + 1) + '\'></a>'
73-
})
74-
.join('\n')
82+
if (!_.isString(content)) content = ''
83+
7584
return md.render(content)
7685
}

0 commit comments

Comments
 (0)