2016-08-20 21:20:53 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Promise = require('bluebird'),
|
|
|
|
md = require('markdown-it'),
|
|
|
|
mdEmoji = require('markdown-it-emoji'),
|
|
|
|
mdTaskLists = require('markdown-it-task-lists'),
|
|
|
|
mdAbbr = require('markdown-it-abbr'),
|
2016-09-02 03:28:42 +00:00
|
|
|
mdAnchor = require('markdown-it-anchor'),
|
2016-08-20 21:20:53 +00:00
|
|
|
mdFootnote = require('markdown-it-footnote'),
|
|
|
|
mdExternalLinks = require('markdown-it-external-links'),
|
|
|
|
mdExpandTabs = require('markdown-it-expand-tabs'),
|
2016-08-21 03:28:53 +00:00
|
|
|
mdAttrs = require('markdown-it-attrs'),
|
|
|
|
hljs = require('highlight.js'),
|
|
|
|
cheerio = require('cheerio'),
|
2016-09-04 05:12:42 +00:00
|
|
|
_ = require('lodash'),
|
|
|
|
mdRemove = require('remove-markdown');
|
2016-08-20 21:20:53 +00:00
|
|
|
|
|
|
|
// Load plugins
|
|
|
|
|
|
|
|
var mkdown = md({
|
|
|
|
html: true,
|
|
|
|
linkify: true,
|
2016-08-21 03:28:53 +00:00
|
|
|
typography: true,
|
2016-08-24 01:09:09 +00:00
|
|
|
highlight(str, lang) {
|
2016-08-21 03:28:53 +00:00
|
|
|
if (lang && hljs.getLanguage(lang)) {
|
|
|
|
try {
|
|
|
|
return '<pre class="hljs"><code>' + hljs.highlight(lang, str, true).value + '</code></pre>';
|
2016-08-24 01:09:09 +00:00
|
|
|
} catch (err) {
|
|
|
|
return '<pre><code>' + str + '</code></pre>';
|
|
|
|
}
|
2016-08-21 03:28:53 +00:00
|
|
|
}
|
2016-08-28 01:46:10 +00:00
|
|
|
return '<pre><code>' + str + '</code></pre>';
|
2016-08-21 03:28:53 +00:00
|
|
|
}
|
2016-08-20 21:20:53 +00:00
|
|
|
})
|
|
|
|
.use(mdEmoji)
|
|
|
|
.use(mdTaskLists)
|
|
|
|
.use(mdAbbr)
|
|
|
|
.use(mdAnchor, {
|
2016-09-02 03:28:42 +00:00
|
|
|
slugify: _.kebabCase,
|
|
|
|
permalink: true,
|
|
|
|
permalinkClass: 'toc-anchor',
|
|
|
|
permalinkSymbol: '#',
|
|
|
|
permalinkBefore: true
|
2016-08-20 21:20:53 +00:00
|
|
|
})
|
|
|
|
.use(mdFootnote)
|
|
|
|
.use(mdExternalLinks, {
|
|
|
|
externalClassName: 'external-link',
|
|
|
|
internalClassName: 'internal-link'
|
|
|
|
})
|
|
|
|
.use(mdExpandTabs, {
|
|
|
|
tabWidth: 4
|
2016-08-21 03:28:53 +00:00
|
|
|
})
|
|
|
|
.use(mdAttrs);
|
2016-08-20 21:20:53 +00:00
|
|
|
|
|
|
|
// Rendering rules
|
|
|
|
|
|
|
|
mkdown.renderer.rules.emoji = function(token, idx) {
|
2016-08-24 21:17:59 +00:00
|
|
|
return '<i class="twa twa-' + token[idx].markup + '"></i>';
|
2016-08-20 21:20:53 +00:00
|
|
|
};
|
|
|
|
|
2016-08-21 03:28:53 +00:00
|
|
|
/**
|
|
|
|
* Parse markdown content and build TOC tree
|
|
|
|
*
|
|
|
|
* @param {(Function|string)} content Markdown content
|
|
|
|
* @return {Array} TOC tree
|
|
|
|
*/
|
2016-08-20 21:20:53 +00:00
|
|
|
const parseTree = (content) => {
|
|
|
|
|
|
|
|
let tokens = md().parse(content, {});
|
|
|
|
let tocArray = [];
|
|
|
|
|
2016-08-21 03:28:53 +00:00
|
|
|
//-> Extract headings and their respective levels
|
|
|
|
|
2016-08-20 21:20:53 +00:00
|
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
|
|
if (tokens[i].type !== "heading_close") {
|
2016-08-24 21:17:59 +00:00
|
|
|
continue;
|
2016-08-20 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 01:09:09 +00:00
|
|
|
const heading = tokens[i - 1];
|
|
|
|
const heading_close = tokens[i];
|
2016-08-20 21:20:53 +00:00
|
|
|
|
|
|
|
if (heading.type === "inline") {
|
2016-08-24 21:17:59 +00:00
|
|
|
let content = "";
|
|
|
|
let anchor = "";
|
|
|
|
if (heading.children && heading.children[0].type === "link_open") {
|
2016-08-24 01:09:09 +00:00
|
|
|
content = heading.children[1].content;
|
2016-08-30 02:19:47 +00:00
|
|
|
anchor = _.kebabCase(content);
|
2016-08-24 21:17:59 +00:00
|
|
|
} else {
|
2016-10-17 23:52:04 +00:00
|
|
|
content = heading.content;
|
2016-08-30 02:19:47 +00:00
|
|
|
anchor = _.kebabCase(heading.children.reduce((acc, t) => acc + t.content, ""));
|
2016-08-24 21:17:59 +00:00
|
|
|
}
|
2016-08-20 21:20:53 +00:00
|
|
|
|
2016-08-24 21:17:59 +00:00
|
|
|
tocArray.push({
|
2016-08-20 21:20:53 +00:00
|
|
|
content,
|
|
|
|
anchor,
|
|
|
|
level: +heading_close.tag.substr(1, 1)
|
2016-08-24 21:17:59 +00:00
|
|
|
});
|
2016-08-20 21:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 03:28:53 +00:00
|
|
|
//-> Exclude levels deeper than 2
|
|
|
|
|
|
|
|
_.remove(tocArray, (n) => { return n.level > 2; });
|
|
|
|
|
|
|
|
//-> Build tree from flat array
|
|
|
|
|
2016-08-20 21:20:53 +00:00
|
|
|
return _.reduce(tocArray, (tree, v) => {
|
|
|
|
let treeLength = tree.length - 1;
|
|
|
|
if(v.level < 2) {
|
|
|
|
tree.push({
|
|
|
|
content: v.content,
|
|
|
|
anchor: v.anchor,
|
|
|
|
nodes: []
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let lastNodeLevel = 1;
|
|
|
|
let GetNodePath = (startPos) => {
|
|
|
|
lastNodeLevel++;
|
|
|
|
if(_.isEmpty(startPos)) {
|
|
|
|
startPos = 'nodes';
|
|
|
|
}
|
|
|
|
if(lastNodeLevel === v.level) {
|
|
|
|
return startPos;
|
|
|
|
} else {
|
|
|
|
return GetNodePath(startPos + '[' + (_.at(tree[treeLength], startPos).length - 1) + '].nodes');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let lastNodePath = GetNodePath();
|
|
|
|
let lastNode = _.get(tree[treeLength], lastNodePath);
|
2016-08-21 03:28:53 +00:00
|
|
|
if(lastNode) {
|
|
|
|
lastNode.push({
|
|
|
|
content: v.content,
|
|
|
|
anchor: v.anchor,
|
|
|
|
nodes: []
|
|
|
|
});
|
|
|
|
_.set(tree[treeLength], lastNodePath, lastNode);
|
|
|
|
}
|
2016-08-20 21:20:53 +00:00
|
|
|
}
|
|
|
|
return tree;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-08-21 03:28:53 +00:00
|
|
|
/**
|
|
|
|
* Parse markdown content to HTML
|
|
|
|
*
|
|
|
|
* @param {String} content Markdown content
|
|
|
|
* @return {String} HTML formatted content
|
|
|
|
*/
|
|
|
|
const parseContent = (content) => {
|
|
|
|
|
|
|
|
let output = mkdown.render(content);
|
|
|
|
let cr = cheerio.load(output);
|
2016-11-21 01:09:50 +00:00
|
|
|
|
2016-11-21 04:28:15 +00:00
|
|
|
//-> Check for empty first element
|
|
|
|
|
|
|
|
let firstElm = cr.root().children().first()[0];
|
|
|
|
if(firstElm.type === 'tag' && firstElm.name === 'p') {
|
|
|
|
let firstElmChildren = firstElm.children;
|
|
|
|
if(firstElmChildren.length < 1) {
|
|
|
|
firstElm.remove();
|
|
|
|
} else if(firstElmChildren.length === 1 && firstElmChildren[0].type === 'tag' && firstElmChildren[0].name === 'img') {
|
|
|
|
cr(firstElm).addClass('is-gapless');
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 01:09:50 +00:00
|
|
|
|
|
|
|
//-> Remove links in headers
|
|
|
|
|
|
|
|
cr('h1 > a:not(.toc-anchor), h2 > a:not(.toc-anchor), h3 > a:not(.toc-anchor)').each((i, elm) => {
|
|
|
|
let txtLink = cr(elm).text();
|
|
|
|
cr(elm).replaceWith(txtLink);
|
|
|
|
});
|
|
|
|
|
2016-08-21 03:28:53 +00:00
|
|
|
output = cr.html();
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
/**
|
|
|
|
* Parse meta-data tags from content
|
|
|
|
*
|
|
|
|
* @param {String} content Markdown content
|
|
|
|
* @return {Object} Properties found in the content and their values
|
|
|
|
*/
|
2016-08-24 21:17:59 +00:00
|
|
|
const parseMeta = (content) => {
|
|
|
|
|
|
|
|
let commentMeta = new RegExp('<!-- ?([a-zA-Z]+):(.*)-->','g');
|
|
|
|
let results = {}, match;
|
|
|
|
while(match = commentMeta.exec(content)) {
|
|
|
|
results[_.toLower(match[1])] = _.trim(match[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-08-20 21:20:53 +00:00
|
|
|
module.exports = {
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
/**
|
|
|
|
* Parse content and return all data
|
|
|
|
*
|
|
|
|
* @param {String} content Markdown-formatted content
|
|
|
|
* @return {Object} Object containing meta, html and tree data
|
|
|
|
*/
|
2016-08-20 21:20:53 +00:00
|
|
|
parse(content) {
|
|
|
|
return {
|
2016-08-24 21:17:59 +00:00
|
|
|
meta: parseMeta(content),
|
2016-08-21 03:28:53 +00:00
|
|
|
html: parseContent(content),
|
2016-08-20 21:20:53 +00:00
|
|
|
tree: parseTree(content)
|
|
|
|
};
|
2016-08-28 01:46:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
parseContent,
|
|
|
|
parseMeta,
|
2016-09-04 05:12:42 +00:00
|
|
|
parseTree,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips non-text elements from Markdown content
|
|
|
|
*
|
|
|
|
* @param {String} content Markdown-formatted content
|
|
|
|
* @return {String} Text-only version
|
|
|
|
*/
|
|
|
|
removeMarkdown(content) {
|
|
|
|
return mdRemove(_.chain(content)
|
|
|
|
.replace(/<!-- ?([a-zA-Z]+):(.*)-->/g, '')
|
|
|
|
.replace(/```[^`]+```/g, '')
|
|
|
|
.replace(/`[^`]+`/g, '')
|
|
|
|
.replace(new RegExp('(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?', 'g'), '')
|
|
|
|
.replace(/\r?\n|\r/g, ' ')
|
|
|
|
.deburr()
|
|
|
|
.toLower()
|
|
|
|
.replace(/(\b([^a-z]+)\b)/g, ' ')
|
|
|
|
.replace(/[^a-z]+/g, ' ')
|
|
|
|
.replace(/(\b(\w{1,2})\b(\W|$))/g, '')
|
|
|
|
.replace(/\s\s+/g, ' ')
|
|
|
|
.value()
|
|
|
|
);
|
|
|
|
}
|
2016-08-20 21:20:53 +00:00
|
|
|
|
|
|
|
};
|