Change markdown engine from remarkable to markdown-it

This commit is contained in:
Cheng-Han, Wu 2016-02-22 08:58:06 +08:00
parent 229740e478
commit 2dc2fbe162
8 changed files with 165 additions and 9650 deletions

View file

@ -35,6 +35,14 @@
"lz-string": "~1.4.4", "lz-string": "~1.4.4",
"raphael": "~2.1.4", "raphael": "~2.1.4",
"flowchart": "~1.6.2", "flowchart": "~1.6.2",
"xss": "~0.2.10" "xss": "~0.2.10",
"markdown-it": "^6.0.0",
"markdown-it-abbr": "^1.0.3",
"markdown-it-footnote": "^2.0.0",
"markdown-it-deflist": "^2.0.0",
"markdown-it-mark": "^2.0.0",
"markdown-it-ins": "^2.0.0",
"markdown-it-sub": "^1.0.0",
"markdown-it-sup": "^1.0.0"
} }
} }

View file

@ -662,7 +662,7 @@ emojify.setConfig({
ignore_emoticons: true ignore_emoticons: true
}); });
var md = new Remarkable('full', { var md = window.markdownit('default', {
html: true, html: true,
breaks: true, breaks: true,
langPrefix: "", langPrefix: "",
@ -670,57 +670,57 @@ var md = new Remarkable('full', {
typographer: true, typographer: true,
highlight: highlightRender highlight: highlightRender
}); });
md.renderer.rules.image = function (tokens, idx, options /*, env */ ) { md.use(window.markdownitAbbr);
var src = ' src="' + Remarkable.utils.escapeHtml(tokens[idx].src) + '"'; md.use(window.markdownitFootnote);
var title = tokens[idx].title ? (' title="' + Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].title)) + '"') : ''; md.use(window.markdownitDeflist);
var alt = ' alt="' + (tokens[idx].alt ? Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].alt)) : '') + '"'; md.use(window.markdownitMark);
var suffix = options.xhtmlOut ? ' /' : ''; md.use(window.markdownitIns);
return '<img class="raw"' + src + alt + title + suffix + '>'; md.use(window.markdownitSub);
} md.use(window.markdownitSup);
md.renderer.rules.list_item_open = function ( /* tokens, idx, options, env */ ) {
return '<li class="raw">'; md.renderer.rules.image = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
return self.renderToken.apply(self, arguments);
}; };
md.renderer.rules.blockquote_open = function (tokens, idx /*, options, env */ ) { md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
return '<blockquote class="raw">\n'; tokens[idx].attrJoin('class', 'raw');
return self.renderToken.apply(self, arguments);
}; };
md.renderer.rules.heading_open = function (tokens, idx) { md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
return '<h' + tokens[idx].hLevel + ' class="raw">'; tokens[idx].attrJoin('class', 'raw');
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
return self.renderToken.apply(self, arguments);
}; };
md.renderer.rules.fence = function (tokens, idx, options, env, self) { md.renderer.rules.fence = function (tokens, idx, options, env, self) {
var token = tokens[idx]; var token = tokens[idx],
var langClass = ''; info = token.info ? md.utils.unescapeAll(token.info).trim() : '',
var langPrefix = options.langPrefix; langName = '',
var langName = '', highlighted;
fenceName;
var highlighted;
if (token.params) { if (info) {
langName = info.split(/\s+/g)[0];
// token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, ''));
// ```foo bar token.attrJoin('class', 'hljs');
//
// Try custom renderer "foo" first. That will simplify overwrite
// for diagrams, latex, and any other fenced block with custom look
//
fenceName = token.params.split(/\s+/g)[0];
if (Remarkable.utils.has(self.rules.fence_custom, fenceName)) {
return self.rules.fence_custom[fenceName](tokens, idx, options, env, self);
}
langName = Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(Remarkable.utils.unescapeMd(fenceName)));
langClass = ' class="' + langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, '') + ' hljs"';
} }
if (options.highlight) { if (options.highlight) {
highlighted = options.highlight(token.content, langName) || Remarkable.utils.escapeHtml(token.content); highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content);
} else { } else {
highlighted = Remarkable.utils.escapeHtml(token.content); highlighted = md.utils.escapeHtml(token.content);
} }
return '<pre><code' + langClass + '>' + highlighted + '</code></pre>' + md.renderer.getBreak(tokens, idx); if (highlighted.indexOf('<pre') === 0) {
return highlighted + '\n';
}
return '<pre><code' + self.renderAttrs(token) + '>'
+ highlighted
+ '</code></pre>\n';
}; };
//youtube //youtube
var youtubePlugin = new Plugin( var youtubePlugin = new Plugin(
// regexp to match // regexp to match

View file

@ -1,143 +1,91 @@
// // Inject line numbers for sync scroll.
// Inject line numbers for sync scroll. Notes:
//
// - We track only headings and paragraphs on first level. That's enougth.
// - Footnotes content causes jumps. Level limit filter it automatically.
//
md.renderer.rules.blockquote_open = function (tokens, idx /*, options, env */ ) {
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<blockquote class="raw part" data-startline="' + startline + '" data-endline="' + endline + '">\n';
}
return '<blockquote>\n';
};
md.renderer.rules.table_open = function (tokens, idx /*, options, env */ ) { function addPart(tokens, idx) {
if (tokens[idx].lines && tokens[idx].level === 0) { if (tokens[idx].map && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1; var startline = tokens[idx].map[0] + 1;
var endline = tokens[idx].lines[1]; var endline = tokens[idx].map[1];
return '<table class="part" data-startline="' + startline + '" data-endline="' + endline + '">\n'; tokens[idx].attrJoin('class', 'part');
tokens[idx].attrJoin('data-startline', startline);
tokens[idx].attrJoin('data-endline', endline);
} }
return '<table>\n'; }
};
md.renderer.rules.bullet_list_open = function (tokens, idx /*, options, env */ ) { md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
if (tokens[idx].lines && tokens[idx].level === 0) { tokens[idx].attrJoin('class', 'raw');
var startline = tokens[idx].lines[0] + 1; addPart(tokens, idx);
var endline = tokens[idx].lines[1]; return self.renderToken.apply(self, arguments);
return '<ul class="part" data-startline="' + startline + '" data-endline="' + endline + '">\n';
}
return '<ul>\n';
}; };
md.renderer.rules.table_open = function (tokens, idx, options, env, self) {
md.renderer.rules.list_item_open = function (tokens, idx /*, options, env */ ) { addPart(tokens, idx);
if (tokens[idx].lines) { return self.renderToken.apply(self, arguments);
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<li class="raw" data-startline="' + startline + '" data-endline="' + endline + '">\n';
}
return '<li class="raw">';
}; };
md.renderer.rules.bullet_list_open = function (tokens, idx, options, env, self) {
md.renderer.rules.ordered_list_open = function (tokens, idx /*, options, env */ ) { addPart(tokens, idx);
var token = tokens[idx]; return self.renderToken.apply(self, arguments);
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<ol class="part" data-startline="' + startline + '" data-endline="' + endline + '"' + (token.order > 1 ? ' start="' + token.order + '"' : '') + '>\n';
}
return '<ol' + (token.order > 1 ? ' start="' + token.order + '"' : '') + '>\n';
}; };
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
md.renderer.rules.link_open = function (tokens, idx /*, options, env */ ) { tokens[idx].attrJoin('class', 'raw');
var title = tokens[idx].title ? (' title="' + Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].title)) + '"') : ''; addPart(tokens, idx);
if (tokens[idx].lines && tokens[idx].level === 0) { return self.renderToken.apply(self, arguments);
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<a class="part" data-startline="' + startline + '" data-endline="' + endline + '" href="' + Remarkable.utils.escapeHtml(tokens[idx].href) + '"' + title + '>';
}
return '<a href="' + Remarkable.utils.escapeHtml(tokens[idx].href) + '"' + title + '>';
}; };
md.renderer.rules.ordered_list_open = function (tokens, idx, options, env, self) {
md.renderer.rules.paragraph_open = function (tokens, idx) { addPart(tokens, idx);
if (tokens[idx].lines && tokens[idx].level === 0) { return self.renderToken.apply(self, arguments);
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return tokens[idx].tight ? '' : '<p class="part" data-startline="' + startline + '" data-endline="' + endline + '">';
}
return tokens[idx].tight ? '' : '<p>';
}; };
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
md.renderer.rules.heading_open = function (tokens, idx) { addPart(tokens, idx);
if (tokens[idx].lines && tokens[idx].level === 0) { return self.renderToken.apply(self, arguments);
var startline = tokens[idx].lines[0] + 1; };
var endline = tokens[idx].lines[1]; md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) {
return '<h' + tokens[idx].hLevel + ' class="part raw" data-startline="' + startline + '" data-endline="' + endline + '">'; addPart(tokens, idx);
} return self.renderToken.apply(self, arguments);
return '<h' + tokens[idx].hLevel + ' class="raw">'; };
md.renderer.rules.heading_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
}; };
md.renderer.rules.fence = function (tokens, idx, options, env, self) { md.renderer.rules.fence = function (tokens, idx, options, env, self) {
var token = tokens[idx]; var token = tokens[idx],
var langClass = ''; info = token.info ? md.utils.unescapeAll(token.info).trim() : '',
var langPrefix = options.langPrefix; langName = '',
var langName = '', highlighted;
fenceName;
var highlighted;
if (token.params) { if (info) {
langName = info.split(/\s+/g)[0];
// token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, ''));
// ```foo bar token.attrJoin('class', 'hljs');
//
// Try custom renderer "foo" first. That will simplify overwrite
// for diagrams, latex, and any other fenced block with custom look
//
fenceName = token.params.split(/\s+/g)[0];
if (Remarkable.utils.has(self.rules.fence_custom, fenceName)) {
return self.rules.fence_custom[fenceName](tokens, idx, options, env, self);
}
langName = Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(Remarkable.utils.unescapeMd(fenceName)));
langClass = ' class="' + langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, '') + ' hljs"';
} }
if (options.highlight) { if (options.highlight) {
highlighted = options.highlight(token.content, langName) || Remarkable.utils.escapeHtml(token.content); highlighted = options.highlight(token.content, langName) || md.utils.escapeHtml(token.content);
} else { } else {
highlighted = Remarkable.utils.escapeHtml(token.content); highlighted = md.utils.escapeHtml(token.content);
} }
if (tokens[idx].lines && tokens[idx].level === 0) { if (highlighted.indexOf('<pre') === 0) {
var startline = tokens[idx].lines[0] + 1; return highlighted + '\n';
var endline = tokens[idx].lines[1]; }
return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code' + langClass + '>' + highlighted + '</code></pre>' + md.renderer.getBreak(tokens, idx);
if (tokens[idx].map && tokens[idx].level === 0) {
var startline = tokens[idx].map[0] + 1;
var endline = tokens[idx].map[1];
return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code' + self.renderAttrs(token) + '>'
+ highlighted
+ '</code></pre>\n';
} }
return '<pre><code' + langClass + '>' + highlighted + '</code></pre>' + md.renderer.getBreak(tokens, idx); return '<pre><code' + self.renderAttrs(token) + '>'
+ highlighted
+ '</code></pre>\n';
}; };
md.renderer.rules.code_block = function (tokens, idx, options, env, self) {
md.renderer.rules.code = function (tokens, idx /*, options, env */ ) { if (tokens[idx].map && tokens[idx].level === 0) {
if (tokens[idx].block) { var startline = tokens[idx].map[0] + 1;
if (tokens[idx].lines && tokens[idx].level === 0) { var endline = tokens[idx].map[1];
var startline = tokens[idx].lines[0] + 1; return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code>' + md.utils.escapeHtml(tokens[idx].content) + '</code></pre>\n';
var endline = tokens[idx].lines[1];
return '<pre class="part" data-startline="' + startline + '" data-endline="' + endline + '"><code>' + Remarkable.utils.escapeHtml(tokens[idx].content) + '</code></pre>' + md.renderer.getBreak(tokens, idx);
}
return '<pre><code>' + Remarkable.utils.escapeHtml(tokens[idx].content) + '</code></pre>' + md.renderer.getBreak(tokens, idx);
} }
return '<pre><code>' + md.utils.escapeHtml(tokens[idx].content) + '</code></pre>\n';
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<code class="part" data-startline="' + startline + '" data-endline="' + endline + '">' + Remarkable.utils.escapeHtml(tokens[idx].content) + '</code>';
}
return '<code>' + Remarkable.utils.escapeHtml(tokens[idx].content) + '</code>';
}; };
var preventSyncScroll = false; var preventSyncScroll = false;

View file

@ -1,9 +1,21 @@
/*! /*!
* remarkable-regexp * markdown-it-regexp
* Copyright (c) 2014 Alex Kocharin * Copyright (c) 2014 Alex Kocharin
* MIT Licensed * MIT Licensed
*/ */
var inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
/** /**
* Escape special characters in the given string of html. * Escape special characters in the given string of html.
* *
@ -47,15 +59,15 @@ var counter = 0
function Plugin(regexp, replacer) { function Plugin(regexp, replacer) {
// return value should be a callable function // return value should be a callable function
// with strictly defined options passed by remarkable // with strictly defined options passed by markdown-it
var self = function(remarkable, options) { var self = function (md, options) {
self.options = options self.options = options
self.init(remarkable) self.init(md)
} }
// initialize plugin object // initialize plugin object
Object.setPrototypeOf(self, Plugin.prototype) Object.setPrototypeOf(self, Plugin.prototype)
// clone regexp with all the flags // clone regexp with all the flags
var flags = (regexp.global ? 'g' : '') var flags = (regexp.global ? 'g' : '')
+ (regexp.multiline ? 'm' : '') + (regexp.multiline ? 'm' : '')
@ -65,7 +77,7 @@ function Plugin(regexp, replacer) {
// copy init options // copy init options
self.replacer = replacer self.replacer = replacer
// this plugin can be inserted multiple times, // this plugin can be inserted multiple times,
// so we're generating unique name for it // so we're generating unique name for it
self.id = 'regexp-' + counter self.id = 'regexp-' + counter
@ -74,14 +86,16 @@ function Plugin(regexp, replacer) {
return self return self
} }
// function that registers plugin with remarkable inherits(Plugin, Function)
Plugin.prototype.init = function(remarkable) {
remarkable.inline.ruler.push(this.id, this.parse.bind(this))
remarkable.renderer.rules[this.id] = this.render.bind(this) // function that registers plugin with markdown-it
Plugin.prototype.init = function (md) {
md.inline.ruler.push(this.id, this.parse.bind(this))
md.renderer.rules[this.id] = this.render.bind(this)
} }
Plugin.prototype.parse = function(state, silent) { Plugin.prototype.parse = function (state, silent) {
// slowwww... maybe use an advanced regexp engine for this // slowwww... maybe use an advanced regexp engine for this
var match = this.regexp.exec(state.src.slice(state.pos)) var match = this.regexp.exec(state.src.slice(state.pos))
if (!match) return false if (!match) return false
@ -92,15 +106,12 @@ Plugin.prototype.parse = function(state, silent) {
// don't insert any tokens in silent mode // don't insert any tokens in silent mode
if (silent) return true if (silent) return true
state.push({ var token = state.push(this.id, '', 0)
type : this.id, token.meta = { match: match }
level : state.level,
match : match,
})
return true return true
} }
Plugin.prototype.render = function(tokens, id, options, env) { Plugin.prototype.render = function (tokens, id, options, env) {
return this.replacer(tokens[id].match, stuff) return this.replacer(tokens[id].meta.match, stuff)
} }

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -25,8 +25,15 @@
<!--ot--> <!--ot-->
<script src="<%- url %>/vendor/ot/ot.min.js" defer></script> <script src="<%- url %>/vendor/ot/ot.min.js" defer></script>
<!--others--> <!--others-->
<script src="<%- url %>/vendor/remarkable.js" defer></script> <script src="<%- url %>/vendor/markdown-it/dist/markdown-it.js" defer></script>
<script src="<%- url %>/vendor/remarkable-regex.js" defer></script> <script src="<%- url %>/vendor/markdown-it-abbr/dist/markdown-it-abbr.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-footnote/dist/markdown-it-footnote.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-deflist/dist/markdown-it-deflist.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-mark/dist/markdown-it-mark.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-ins/dist/markdown-it-ins.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sub/dist/markdown-it-sub.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sup/dist/markdown-it-sup.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-regexp.js" defer></script>
<script src="<%- url %>/vendor/gist-embed.js" defer></script> <script src="<%- url %>/vendor/gist-embed.js" defer></script>
<script src="<%- url %>/vendor/lz-string/libs/lz-string.min.js" defer></script> <script src="<%- url %>/vendor/lz-string/libs/lz-string.min.js" defer></script>
<script src="<%- url %>/vendor/xss/dist/xss.min.js" defer></script> <script src="<%- url %>/vendor/xss/dist/xss.min.js" defer></script>

View file

@ -77,8 +77,15 @@
<% } %> <% } %>
<script src="<%- url %>/vendor/lz-string/libs/lz-string.min.js" defer></script> <script src="<%- url %>/vendor/lz-string/libs/lz-string.min.js" defer></script>
<script src="<%- url %>/vendor/xss/dist/xss.min.js" defer></script> <script src="<%- url %>/vendor/xss/dist/xss.min.js" defer></script>
<script src="<%- url %>/vendor/remarkable.min.js" defer></script> <script src="<%- url %>/vendor/markdown-it/dist/markdown-it.min.js" defer></script>
<script src="<%- url %>/vendor/remarkable-regex.js" defer></script> <script src="<%- url %>/vendor/markdown-it-abbr/dist/markdown-it-abbr.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-footnote/dist/markdown-it-footnote.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-deflist/dist/markdown-it-deflist.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-mark/dist/markdown-it-mark.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-ins/dist/markdown-it-ins.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sub/dist/markdown-it-sub.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-sup/dist/markdown-it-sup.min.js" defer></script>
<script src="<%- url %>/vendor/markdown-it-regexp.js" defer></script>
<script src="<%- url %>/vendor/gist-embed.js" defer></script> <script src="<%- url %>/vendor/gist-embed.js" defer></script>
<script src="<%- url %>/vendor/string.min.js" defer></script> <script src="<%- url %>/vendor/string.min.js" defer></script>
<script src="<%- url %>/vendor/highlight-js/highlight.min.js" defer></script> <script src="<%- url %>/vendor/highlight-js/highlight.min.js" defer></script>