From 49a4b5feb4f7a06a1f606450d9eda810d072297a Mon Sep 17 00:00:00 2001 From: Dick Choi Date: Tue, 26 Jul 2016 17:07:56 +0900 Subject: [PATCH] no more line anchors --- browser/components/MarkdownPreview.js | 4 ++-- browser/components/markdown.styl | 12 ------------ browser/lib/markdown.js | 25 +++++++++++++++++-------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 8a5b1353..9936f9fa 100644 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -141,11 +141,11 @@ export default class MarkdownPreview extends React.Component { } scrollTo (targetRow) { - let lineAnchors = this.getWindow().document.querySelectorAll('a.lineAnchor') + let lineAnchors = this.getWindow().document.querySelectorAll('[data-line]') for (let index = 0; index < lineAnchors.length; index++) { let lineAnchor = lineAnchors[index] - let row = parseInt(lineAnchor.getAttribute('data-key')) + let row = parseInt(lineAnchor.getAttribute('data-line')) if (row > targetRow) { let targetAnchor = lineAnchors[index - 1] targetAnchor != null && this.getWindow().scrollTo(0, targetAnchor.offsetTop) diff --git a/browser/components/markdown.styl b/browser/components/markdown.styl index 5d7ea39c..c563d9ef 100644 --- a/browser/components/markdown.styl +++ b/browser/components/markdown.styl @@ -102,13 +102,6 @@ a background-color alpha(#FFC95C, 0.3) &:visited color brandColor - &.lineAnchor - padding 0 - margin 0 - display inline-block - font-size 0 - height 0 - width 0 hr border-top none border-bottom solid 1px borderColor @@ -148,9 +141,6 @@ h6 line-height 1.4em margin 1em 0 1em color #777 - -*:not(a.lineAnchor) + p, *:not(a.lineAnchor) + blockquote, *:not(a.lineAnchor) + ul, *:not(a.lineAnchor) + ol, *:not(a.lineAnchor) + pre - margin-top 1em p line-height 1.6em margin 0 0 1em @@ -196,8 +186,6 @@ code font-size 0.85em text-decoration none margin-right 2px -*:not(a.lineAnchor) + code - margin-left 2px pre padding 0.5em !important border solid 1px alpha(borderColor, 0.5) diff --git a/browser/lib/markdown.js b/browser/lib/markdown.js index 31111e61..bd3d9a6b 100644 --- a/browser/lib/markdown.js +++ b/browser/lib/markdown.js @@ -2,6 +2,7 @@ import markdownit from 'markdown-it' import emoji from 'markdown-it-emoji' import math from '@rokt33r/markdown-it-math' import hljs from 'highlight.js' +import _ from 'lodash' const katex = window.katex @@ -61,16 +62,24 @@ md.use(math, { }) md.use(require('markdown-it-footnote')) +let originalRender = md.renderer.render +md.renderer.render = function render (tokens, options, env) { + tokens.forEach((token) => { + switch (token.type) { + case 'heading_open': + case 'paragraph_open': + case 'blockquote_open': + case 'table_open': + token.attrPush(['data-line', token.map[0]]) + } + }) + let result = originalRender.call(md.renderer, tokens, options, env) + return result +} window.md = md export default function markdown (content) { - if (content == null) content = '' - content = content.toString() - .split('\n') - .map((line, index) => { - if (line.trim().length === 0) return '' - return line + '' - }) - .join('\n') + if (!_.isString(content)) content = '' + return md.render(content) }