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",
"raphael": "~2.1.4",
"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
});
var md = new Remarkable('full', {
var md = window.markdownit('default', {
html: true,
breaks: true,
langPrefix: "",
@ -670,57 +670,57 @@ var md = new Remarkable('full', {
typographer: true,
highlight: highlightRender
});
md.renderer.rules.image = function (tokens, idx, options /*, env */ ) {
var src = ' src="' + Remarkable.utils.escapeHtml(tokens[idx].src) + '"';
var title = tokens[idx].title ? (' title="' + Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].title)) + '"') : '';
var alt = ' alt="' + (tokens[idx].alt ? Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].alt)) : '') + '"';
var suffix = options.xhtmlOut ? ' /' : '';
return '<img class="raw"' + src + alt + title + suffix + '>';
}
md.renderer.rules.list_item_open = function ( /* tokens, idx, options, env */ ) {
return '<li class="raw">';
md.use(window.markdownitAbbr);
md.use(window.markdownitFootnote);
md.use(window.markdownitDeflist);
md.use(window.markdownitMark);
md.use(window.markdownitIns);
md.use(window.markdownitSub);
md.use(window.markdownitSup);
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 */ ) {
return '<blockquote class="raw">\n';
md.renderer.rules.list_item_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.heading_open = function (tokens, idx) {
return '<h' + tokens[idx].hLevel + ' class="raw">';
md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
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) {
var token = tokens[idx];
var langClass = '';
var langPrefix = options.langPrefix;
var langName = '',
fenceName;
var highlighted;
var token = tokens[idx],
info = token.info ? md.utils.unescapeAll(token.info).trim() : '',
langName = '',
highlighted;
if (token.params) {
//
// ```foo bar
//
// 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 (info) {
langName = info.split(/\s+/g)[0];
token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, ''));
token.attrJoin('class', 'hljs');
}
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 {
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
var youtubePlugin = new Plugin(
// regexp to match

View File

@ -1,143 +1,91 @@
//
// 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';
};
// Inject line numbers for sync scroll.
md.renderer.rules.table_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 '<table class="part" data-startline="' + startline + '" data-endline="' + endline + '">\n';
function addPart(tokens, idx) {
if (tokens[idx].map && tokens[idx].level === 0) {
var startline = tokens[idx].map[0] + 1;
var endline = tokens[idx].map[1];
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 */ ) {
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<ul class="part" data-startline="' + startline + '" data-endline="' + endline + '">\n';
}
return '<ul>\n';
md.renderer.rules.blockquote_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.list_item_open = function (tokens, idx /*, options, env */ ) {
if (tokens[idx].lines) {
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.table_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.ordered_list_open = function (tokens, idx /*, options, env */ ) {
var token = tokens[idx];
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.bullet_list_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.link_open = function (tokens, idx /*, options, env */ ) {
var title = tokens[idx].title ? (' title="' + Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].title)) + '"') : '';
if (tokens[idx].lines && tokens[idx].level === 0) {
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.list_item_open = function (tokens, idx, options, env, self) {
tokens[idx].attrJoin('class', 'raw');
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.paragraph_open = function (tokens, idx) {
if (tokens[idx].lines && tokens[idx].level === 0) {
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.ordered_list_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.heading_open = function (tokens, idx) {
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1];
return '<h' + tokens[idx].hLevel + ' class="part raw" data-startline="' + startline + '" data-endline="' + endline + '">';
}
return '<h' + tokens[idx].hLevel + ' class="raw">';
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
md.renderer.rules.paragraph_open = function (tokens, idx, options, env, self) {
addPart(tokens, idx);
return self.renderToken.apply(self, arguments);
};
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) {
var token = tokens[idx];
var langClass = '';
var langPrefix = options.langPrefix;
var langName = '',
fenceName;
var highlighted;
var token = tokens[idx],
info = token.info ? md.utils.unescapeAll(token.info).trim() : '',
langName = '',
highlighted;
if (token.params) {
//
// ```foo bar
//
// 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 (info) {
langName = info.split(/\s+/g)[0];
token.attrJoin('class', options.langPrefix + langName.replace(/\=$|\=\d+$|\=\+$/, ''));
token.attrJoin('class', 'hljs');
}
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 {
highlighted = Remarkable.utils.escapeHtml(token.content);
highlighted = md.utils.escapeHtml(token.content);
}
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
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 (highlighted.indexOf('<pre') === 0) {
return highlighted + '\n';
}
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 = function (tokens, idx /*, options, env */ ) {
if (tokens[idx].block) {
if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1;
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);
md.renderer.rules.code_block = function (tokens, idx, options, env, self) {
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>' + 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>';
return '<pre><code>' + md.utils.escapeHtml(tokens[idx].content) + '</code></pre>\n';
};
var preventSyncScroll = false;

View File

@ -1,9 +1,21 @@
/*!
* remarkable-regexp
* markdown-it-regexp
* Copyright (c) 2014 Alex Kocharin
* 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.
*
@ -47,15 +59,15 @@ var counter = 0
function Plugin(regexp, replacer) {
// return value should be a callable function
// with strictly defined options passed by remarkable
var self = function(remarkable, options) {
// with strictly defined options passed by markdown-it
var self = function (md, options) {
self.options = options
self.init(remarkable)
self.init(md)
}
// initialize plugin object
Object.setPrototypeOf(self, Plugin.prototype)
// clone regexp with all the flags
var flags = (regexp.global ? 'g' : '')
+ (regexp.multiline ? 'm' : '')
@ -65,7 +77,7 @@ function Plugin(regexp, replacer) {
// copy init options
self.replacer = replacer
// this plugin can be inserted multiple times,
// so we're generating unique name for it
self.id = 'regexp-' + counter
@ -74,14 +86,16 @@ function Plugin(regexp, replacer) {
return self
}
// function that registers plugin with remarkable
Plugin.prototype.init = function(remarkable) {
remarkable.inline.ruler.push(this.id, this.parse.bind(this))
inherits(Plugin, Function)
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
var match = this.regexp.exec(state.src.slice(state.pos))
if (!match) return false
@ -92,15 +106,12 @@ Plugin.prototype.parse = function(state, silent) {
// don't insert any tokens in silent mode
if (silent) return true
state.push({
type : this.id,
level : state.level,
match : match,
})
var token = state.push(this.id, '', 0)
token.meta = { match: match }
return true
}
Plugin.prototype.render = function(tokens, id, options, env) {
return this.replacer(tokens[id].match, stuff)
}
Plugin.prototype.render = function (tokens, id, options, env) {
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-->
<script src="<%- url %>/vendor/ot/ot.min.js" defer></script>
<!--others-->
<script src="<%- url %>/vendor/remarkable.js" defer></script>
<script src="<%- url %>/vendor/remarkable-regex.js" defer></script>
<script src="<%- url %>/vendor/markdown-it/dist/markdown-it.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/lz-string/libs/lz-string.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/xss/dist/xss.min.js" defer></script>
<script src="<%- url %>/vendor/remarkable.min.js" defer></script>
<script src="<%- url %>/vendor/remarkable-regex.js" defer></script>
<script src="<%- url %>/vendor/markdown-it/dist/markdown-it.min.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/string.min.js" defer></script>
<script src="<%- url %>/vendor/highlight-js/highlight.min.js" defer></script>