@zohodesk/docs-builder
Version:
docs-builder is used to build your own docs
68 lines (64 loc) • 2.26 kB
JavaScript
const markdownIt = require('markdown-it');
// const md = new markdownIt({ linkify: true,breaks:true });
const md = new markdownIt({ html: false, linkify: true, typographer: true, breaks:true,xhtmlOut:true });
//const md = new markdownIt();
function markdownParser(source) {
if (!source) {
return '';
}
const startTag = '/* MD:START';
const endTag = 'MD:END */';
// Iterate through all occurrences of the tags
let startIndex = source.indexOf(startTag);
while (startIndex !== -1) {
const endIndex = source.indexOf(endTag, startIndex);
if (endIndex !== -1) {
const extractedMarkdown = source.slice(
startIndex + startTag.length,
endIndex
);
let lines = extractedMarkdown.split('\n');
lines = lines.filter(line => line.trim() !== '');
const firstLineIndentMatch = lines[0].match(/^\s+/);
const firstLineIndent = firstLineIndentMatch
? firstLineIndentMatch[0]
: '';
const modifiedStr = lines
.map(line => line.replace(new RegExp(`^${firstLineIndent}`), ''))
.join('\n').replace(/(:--)|(--:)/g,'---');
let html = md.render(modifiedStr);
//html = html.replace(/"|<hr>|<img src=(".*?")>|<br>|:--|--:|{|}|\$/g, match => {
html = html.replace(/"|{|}|\$/g, match => {
// if (match === '<hr>') {
// return '<hr/>';
// } else if (match.startsWith('<img src=')) {
// return match.replace('>', '/>');
// } else if (match === '<br>') {
// return '<br/>';
if (match === '$') {
return '$';
} else if (match === '{') {
return '{';
} else if (match === '}') {
return '}';
}
// else if (match === ':--' || match === '--:' ) {
// return '---';
// }
else if (match === '}') {
return '}';
}
return match;
});
// console.log(html,"html");
source = source.replace(
source.slice(startIndex, endIndex + endTag.length),
'<><div class="markDown">' + html + '</div></>'
);
}
startIndex = source.indexOf(startTag, startIndex + 1);
// console.log(source)
}
return source;
}
exports.default = markdownParser