refactor: switch to marked (🗜 -81kB to minified bundle size)

This commit is contained in:
Roman Hotsiy 2018-02-09 13:29:18 +02:00
parent 6990cd2f24
commit 213f85b719
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 40 additions and 82 deletions

View File

@ -82,12 +82,14 @@
"react-dom": "^16.0.0" "react-dom": "^16.0.0"
}, },
"dependencies": { "dependencies": {
"@types/marked": "^0.3.0",
"decko": "^1.2.0", "decko": "^1.2.0",
"dompurify": "^1.0.2", "dompurify": "^1.0.2",
"eventemitter3": "^3.0.0", "eventemitter3": "^3.0.0",
"json-pointer": "^0.6.0", "json-pointer": "^0.6.0",
"json-schema-ref-parser": "^4.0.4", "json-schema-ref-parser": "^4.0.4",
"lunr": "^2.1.5", "lunr": "^2.1.5",
"marked": "^0.3.12",
"mobx": "^3.3.0", "mobx": "^3.3.0",
"mobx-react": "^4.3.3", "mobx-react": "^4.3.3",
"openapi-sampler": "1.0.0-beta.8", "openapi-sampler": "1.0.0-beta.8",
@ -98,7 +100,6 @@
"react-hot-loader": "3.1.3", "react-hot-loader": "3.1.3",
"react-tabs": "^2.0.0", "react-tabs": "^2.0.0",
"react-tippy": "^1.2.2", "react-tippy": "^1.2.2",
"remarkable": "^1.7.1",
"slugify": "^1.2.1", "slugify": "^1.2.1",
"stickyfill": "^1.1.1", "stickyfill": "^1.1.1",
"styled-components": "^3.1.0", "styled-components": "^3.1.0",

View File

@ -1,15 +1,14 @@
import * as Remarkable from 'remarkable'; import * as marked from 'marked';
import slugify from 'slugify'; import slugify from 'slugify';
import { MDComponent } from '../components/Markdown/Markdown'; import { MDComponent } from '../components/Markdown/Markdown';
import { highlight, html2Str } from '../utils'; import { highlight, html2Str } from '../utils';
import { SECTION_ATTR } from './MenuStore'; import { SECTION_ATTR } from './MenuStore';
const md = new Remarkable('default', { const renderer = new marked.Renderer();
html: true,
linkify: true, marked.setOptions({
breaks: false, renderer,
typographer: false,
highlight: (str, lang) => { highlight: (str, lang) => {
return highlight(str, lang); return highlight(str, lang);
}, },
@ -31,16 +30,13 @@ export class MarkdownRenderer {
headings: MarkdownHeading[] = []; headings: MarkdownHeading[] = [];
currentTopHeading: MarkdownHeading; currentTopHeading: MarkdownHeading;
private _origRules: any = {}; private headingEnhanceRenderer: marked.Renderer;
private originalHeadingRule: typeof marked.Renderer.prototype.heading;
saveOrigRules() { constructor() {
this._origRules.open = md.renderer.rules.heading_open; this.headingEnhanceRenderer = new marked.Renderer();
this._origRules.close = md.renderer.rules.heading_close; this.originalHeadingRule = this.headingEnhanceRenderer.heading;
} this.headingEnhanceRenderer.heading = this.headingRule;
restoreOrigRules() {
md.renderer.rules.heading_open = this._origRules.open;
md.renderer.rules.heading_close = this._origRules.close;
} }
saveHeading(name: string, container: MarkdownHeading[] = this.headings): MarkdownHeading { saveHeading(name: string, container: MarkdownHeading[] = this.headings): MarkdownHeading {
@ -87,53 +83,32 @@ export class MarkdownRenderer {
prevHeading.description = html2Str(rawText.substring(prevPos)); prevHeading.description = html2Str(rawText.substring(prevPos));
} }
headingOpenRule = (tokens, idx) => { headingRule = (text: string, level: number, raw: string) => {
if (tokens[idx].hLevel > 2) { if (level === 1) {
return this._origRules.open(tokens, idx); this.currentTopHeading = this.saveHeading(text);
} else {
const content = tokens[idx + 1].content;
if (tokens[idx].hLevel === 1) {
this.currentTopHeading = this.saveHeading(content);
const id = this.currentTopHeading.id; const id = this.currentTopHeading.id;
return ( return (
`<a name="${id}"></a>` + `<a name="${id}"></a>` +
`<h${tokens[idx].hLevel} ${SECTION_ATTR}="${id}" id="${id}">` + `<h${level} ${SECTION_ATTR}="${id}" id="${id}">` +
`<a class="share-link" href="#${id}"></a>` `<a class="share-link" href="#${id}"></a>${text}</h${level}>`
);
} else if (tokens[idx].hLevel === 2) {
const { id } = this.saveHeading(
content,
this.currentTopHeading && this.currentTopHeading.items,
); );
} else if (level === 2) {
const { id } = this.saveHeading(text, this.currentTopHeading && this.currentTopHeading.items);
return ( return (
`<a name="${id}"></a>` + `<a name="${id}"></a>` +
`<h${tokens[idx].hLevel} ${SECTION_ATTR}="${id}" id="${id}">` + `<h${level} ${SECTION_ATTR}="${id}" id="${id}">` +
`<a class="share-link" href="#${id}"></a>` `<a class="share-link" href="#${id}"></a>${text}</h${level}>`
); );
}
}
};
headingCloseRule = (tokens, idx) => {
if (tokens[idx].hLevel > 2) {
return this._origRules.close(tokens, idx);
} else { } else {
return `</h${tokens[idx].hLevel}>\n`; return this.originalHeadingRule(text, level, raw);
} }
}; };
renderMd(rawText: string, raw: boolean = true): string { renderMd(rawText: string, raw: boolean = true): string {
if (!raw) { const opts = raw ? undefined : { renderer: this.headingEnhanceRenderer };
this.saveOrigRules();
md.renderer.rules.heading_open = this.headingOpenRule;
md.renderer.rules.heading_close = this.headingCloseRule;
}
const res = md.render(rawText.toString()); const res = marked(rawText.toString(), opts);
if (!raw) {
this.restoreOrigRules();
}
return res; return res;
} }

View File

@ -103,6 +103,10 @@
version "2.1.5" version "2.1.5"
resolved "https://registry.yarnpkg.com/@types/lunr/-/lunr-2.1.5.tgz#afb90226a6d2eb472eb1732cef7493a02b0177fd" resolved "https://registry.yarnpkg.com/@types/lunr/-/lunr-2.1.5.tgz#afb90226a6d2eb472eb1732cef7493a02b0177fd"
"@types/marked@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-0.3.0.tgz#583c223dd33385a1dda01aaf77b0cd0411c4b524"
"@types/minimatch@3.0.1": "@types/minimatch@3.0.1":
version "3.0.1" version "3.0.1"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.1.tgz#b683eb60be358304ef146f5775db4c0e3696a550" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.1.tgz#b683eb60be358304ef146f5775db4c0e3696a550"
@ -368,13 +372,6 @@ argparse@^1.0.7:
dependencies: dependencies:
sprintf-js "~1.0.2" sprintf-js "~1.0.2"
argparse@~0.1.15:
version "0.1.16"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-0.1.16.tgz#cfd01e0fbba3d6caed049fbd758d40f65196f57c"
dependencies:
underscore "~1.7.0"
underscore.string "~2.4.0"
arr-diff@^2.0.0: arr-diff@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-2.0.0.tgz#8f3b827f955a8bd669697e4a4256ac3ceae356cf" resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-2.0.0.tgz#8f3b827f955a8bd669697e4a4256ac3ceae356cf"
@ -524,10 +521,6 @@ atob@^2.0.0:
version "2.0.3" version "2.0.3"
resolved "https://registry.yarnpkg.com/atob/-/atob-2.0.3.tgz#19c7a760473774468f20b2d2d03372ad7d4cbf5d" resolved "https://registry.yarnpkg.com/atob/-/atob-2.0.3.tgz#19c7a760473774468f20b2d2d03372ad7d4cbf5d"
autolinker@~0.15.0:
version "0.15.3"
resolved "https://registry.yarnpkg.com/autolinker/-/autolinker-0.15.3.tgz#342417d8f2f3461b14cf09088d5edf8791dc9832"
autoprefixer@^6.3.1: autoprefixer@^6.3.1:
version "6.7.7" version "6.7.7"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-6.7.7.tgz#1dbd1c835658e35ce3f9984099db00585c782014" resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-6.7.7.tgz#1dbd1c835658e35ce3f9984099db00585c782014"
@ -4634,6 +4627,10 @@ map-visit@^1.0.0:
dependencies: dependencies:
object-visit "^1.0.0" object-visit "^1.0.0"
marked@^0.3.12:
version "0.3.12"
resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.12.tgz#7cf25ff2252632f3fe2406bde258e94eee927519"
math-expression-evaluator@^1.2.14: math-expression-evaluator@^1.2.14:
version "1.2.17" version "1.2.17"
resolved "https://registry.yarnpkg.com/math-expression-evaluator/-/math-expression-evaluator-1.2.17.tgz#de819fdbcd84dccd8fae59c6aeb79615b9d266ac" resolved "https://registry.yarnpkg.com/math-expression-evaluator/-/math-expression-evaluator-1.2.17.tgz#de819fdbcd84dccd8fae59c6aeb79615b9d266ac"
@ -6209,13 +6206,6 @@ relateurl@0.2.x:
version "0.2.7" version "0.2.7"
resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9" resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9"
remarkable@^1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/remarkable/-/remarkable-1.7.1.tgz#aaca4972100b66a642a63a1021ca4bac1be3bff6"
dependencies:
argparse "~0.1.15"
autolinker "~0.15.0"
remove-trailing-separator@^1.0.1: remove-trailing-separator@^1.0.1:
version "1.1.0" version "1.1.0"
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef" resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
@ -7420,18 +7410,10 @@ ultron@~1.1.0:
version "1.1.1" version "1.1.1"
resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c" resolved "https://registry.yarnpkg.com/ultron/-/ultron-1.1.1.tgz#9fe1536a10a664a65266a1e3ccf85fd36302bc9c"
underscore.string@~2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/underscore.string/-/underscore.string-2.4.0.tgz#8cdd8fbac4e2d2ea1e7e2e8097c42f442280f85b"
underscore@~1.4.4: underscore@~1.4.4:
version "1.4.4" version "1.4.4"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.4.4.tgz#61a6a32010622afa07963bf325203cf12239d604" resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.4.4.tgz#61a6a32010622afa07963bf325203cf12239d604"
underscore@~1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.7.0.tgz#6bbaf0877500d36be34ecaa584e0db9fef035209"
union-value@^1.0.0: union-value@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4"