Skip to content

Commit

Permalink
fix markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
skorotkiewicz committed Jul 3, 2020
1 parent 63a4840 commit fd0e2f8
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions helpers/slimdown.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';

const hljs = require('highlight.js');
const fc = require('./functions');

/**
* Javascript version of https://gist.github.com/jbroadway/2836900
*
* Slimdown - A very basic regex-based Markdown parser. Supports the
* following elements (and can be extended via Slimdown::add_rule()):
* Slimdown - A very basic regex-based Markdown parser.
*
* Author: Johnny Broadway <[email protected]>
* Modified by Sebastian Korotkiewicz <Kuscos>
* Website: https://gist.github.com/jbroadway/2836900
*
* Modified by: Sebastian Korotkiewicz <Kuscos>
*
* License: MIT
*/
Expand Down Expand Up @@ -49,43 +47,42 @@ class Slimdown {

}

highlight(text) {
var regex = /```(.*)[a-z]*\n\s*([^]+?.*?[^]+?[^]+?)\n```/g;
var match = regex.exec(text);
var language = match[1];
var content = match[2];

highlight(language, content) {
if (language) {
var highlightedCode = hljs.highlight(language, content).value
} else {
highlightedCode = hljs.highlightAuto(content).value
}

text = text.replace(regex, `<pre><code class="hljs">${highlightedCode}</code></pre>`);
return text;
}

// Add a rule.
addRule(regex, replacement) {
regex.global = true;
regex.multiline = true;
this.rules.push({
regex,
replacement,
});
return `<pre><code class="hljs">${highlightedCode}</code></pre>`;
}

// Render some Markdown into HTML.
render(text) {
//text = `\n${text}\n`;
if ( /```\s*([^]+?.*?[^]+?[^]+?)```/g.test(text) ) {
text = this.highlight(text);
} else {
text = fc.cleanHtml(text);
this.rules.forEach( (rule) => {
text = text.replace(rule.regex, rule.replacement);
});

text = text.replace('KUCOSMULTILINECODEID', 'KUCOSMULTILINECODE');

var regex = /```(.*)([^]+?.*?[^]+?[^]+?)\n```/g;
var arr = [];
const matches = text.matchAll(regex);

// Match all multiline codes, push into array and replace codes with temporary string
for (const match of matches) {
var code = this.highlight(match[1].trim(), match[2].trim() )
arr.push(code);
text = text.replace(match[0], 'KUCOSMULTILINECODEID');
}

// Match remaining text and replace with rules
for (let i = 0; i < this.rules.length; i++) {
const rule = this.rules[i];
text = text.replace(rule.regex, rule.replacement);
}

// Match temporary string and replace with multiline codes from array
for (let i = 0; i < arr.length; i++) {
text = text.replace('KUCOSMULTILINECODEID', arr[i]);
}

return text.trim();
}
}
Expand Down

0 comments on commit fd0e2f8

Please sign in to comment.