Skip to content

Commit 992ba14

Browse files
committed
Sharing markdown parsing login between both middlewares #119
1 parent 718a1a8 commit 992ba14

File tree

3 files changed

+85
-64
lines changed

3 files changed

+85
-64
lines changed

core/lib/processMd.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var marked = require('marked');
5+
var cheerio = require('cheerio');
6+
var deepExtend = require('deep-extend');
7+
var translit = require(path.join(global.pathToApp,'core/lib/translit'));
8+
9+
var renderer = new marked.Renderer();
10+
11+
// Module configuration
12+
var globalConfig = global.opts.core && global.opts.core.processMd ? global.opts.core.processMd : {};
13+
var config = {
14+
espaceCodeHTML: true
15+
};
16+
// Overwriting base options
17+
deepExtend(config, globalConfig);
18+
19+
marked.setOptions({
20+
renderer: renderer
21+
});
22+
23+
// Processing with native markdown renderer
24+
renderer.code = function (code, language) {
25+
if (config.espaceCodeHTML) code = code.replace(/</g, "&lt;").replace(/>/g, "&gt;");
26+
27+
if (language === 'example') {
28+
return '<div class="source_example">' + code + '</div>';
29+
} else if (language && language !== '') {
30+
return '<code class="src-' + language + ' source_visible">' + code + '</code>';
31+
} else {
32+
return '<pre><code class="lang-source_wide-code">' + code + '</code></pre>';
33+
}
34+
};
35+
36+
renderer.heading = function (text, level) {
37+
var escapedText = translit(text);
38+
39+
return '<h' + level + ' id="' + escapedText + '">' + text + '</h' + level + '>';
40+
};
41+
42+
module.exports = function (markdown) {
43+
var input = markdown;
44+
45+
var $ = cheerio.load('<div id="content">' + marked(input) + '</div>');
46+
47+
// Spec description
48+
var $H1 = $('#content > h1');
49+
var $afterH1 = $H1.nextUntil('h2');
50+
$afterH1.remove();
51+
$H1.after('<div class="source_info">' + $afterH1 + '</div>');
52+
53+
// Spec sections
54+
$('#content > h2').each(function () {
55+
var $this = $(this);
56+
var $filteredElems = $('');
57+
58+
var $sectionElems = $this.nextUntil('h2');
59+
var id = $this.attr('id');
60+
$this.removeAttr('id');
61+
62+
// Adding additional check, since cheerio .nextUntil is not stable
63+
$sectionElems.each(function () {
64+
if (this.tagName === 'h2') return false;
65+
66+
$filteredElems = $filteredElems.add(this);
67+
});
68+
69+
$filteredElems.remove();
70+
71+
$(this).replaceWith([
72+
'<div class="source_section" id="' + id + '">',
73+
$this + $filteredElems,
74+
'</div>'
75+
].join(''));
76+
});
77+
78+
return $('#content').html();
79+
};

core/middleware/md.js

+3-60
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
'use strict';
22

33
var path = require('path');
4-
var cheerio = require('cheerio');
54
var prettyHrtime = require('pretty-hrtime');
6-
var translit = require(path.join(global.pathToApp,'core/lib/translit'));
7-
8-
var marked = require('marked');
9-
var renderer = new marked.Renderer();
10-
marked.setOptions({
11-
renderer: renderer
12-
});
5+
var processMd = require(path.join(global.pathToApp,'core/lib/processMd'));
136

147
/*
158
* Get file content from response and parse contained Markdown markup
@@ -21,59 +14,9 @@ marked.setOptions({
2114
exports.process = function (req, res, next) {
2215
if (req.specData && req.specData.renderedHtml && req.specData.isMd) {
2316
var start = process.hrtime();
24-
var input = req.specData.renderedHtml;
25-
26-
// Processing with native markdown renderer
27-
renderer.code = function (code, language) {
28-
if (language === 'example') {
29-
return '<div class="source_example">'+ code +'</div>';
30-
} else if (language && language !== '') {
31-
return '<code class="src-'+ language +' source_visible">'+ code +'</code>';
32-
} else {
33-
return '<pre><code class="lang-source_wide-code">'+ code +'</code></pre>';
34-
}
35-
};
36-
37-
renderer.heading = function (text, level) {
38-
var escapedText = translit(text);
39-
40-
return '<h' + level + ' id="' + escapedText + '">' + text + '</h' + level + '>';
41-
};
42-
43-
var $ = cheerio.load('<div id="content">'+ marked(input) +'</div>');
44-
45-
// Spec description
46-
var $H1 = $('#content > h1');
47-
var $afterH1 = $H1.nextUntil('h2');
48-
$afterH1.remove();
49-
$H1.after('<div class="source_info">'+ $afterH1 +'</div>');
50-
51-
// Spec sections
52-
$('#content > h2').each(function(){
53-
var $this = $(this);
54-
var $filteredElems = $('');
55-
56-
var $sectionElems = $this.nextUntil('h2');
57-
var id = $this.attr('id');
58-
$this.removeAttr('id');
59-
60-
// Adding additional check, since cheerio .nextUntil is not stable
61-
$sectionElems.each(function(){
62-
if (this.tagName === 'h2') return false;
63-
64-
$filteredElems = $filteredElems.add(this);
65-
});
66-
67-
$filteredElems.remove();
68-
69-
$(this).replaceWith([
70-
'<div class="source_section" id="'+ id +'">',
71-
$this + $filteredElems,
72-
'</div>'
73-
].join(''));
74-
});
17+
var plainMarkdown = req.specData.renderedHtml;
7518

76-
req.specData.renderedHtml = $('#content').html();
19+
req.specData.renderedHtml = processMd(plainMarkdown);
7720

7821
var end = process.hrtime(start);
7922
global.log.debug('Markdown processing took: ', prettyHrtime(end));

core/middleware/mdTag.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
var marked = require('marked');
3+
var path = require('path');
4+
var processMd = require(path.join(global.pathToApp,'core/lib/processMd'));
45

56
/*
67
* Get html from response and parse contained Markdown markup
@@ -22,9 +23,7 @@ exports.process = function (req, res, next) {
2223
match = match.replace(/\t/g, '').replace(/ +(?= )/g, '');
2324

2425
// render markdown
25-
match = marked(match);
26-
27-
return match;
26+
return processMd(match);
2827
});
2928

3029
req.specData.renderedHtml = html;

0 commit comments

Comments
 (0)