'use strict'; const parseAttrs = require('./utils/parseAttrs'); const yaml = require('js-yaml'); const stripIndents = require('textUtil/stripIndents'); module.exports = function(text) { text = text.replace(/[ \t]+$/gim, ''); // remove spaces text = text.replace(/\[(edit.*?)\](.*?)\[\/edit\]/g, '[$1 title="$2"]'); text = text.replace(/\[(edit.*?)\/\]/g, '[$1]'); // remove [pre no-typography]...[/pre] text = text.replace(/^\[pre.*?\]/gim, ''); text = text.replace(/^\[\/pre\]/gim, ''); text = text.replace(/\[\]\(\/(.*?)\)/g, ''); text = text.replace(/\[\]\((http.*?)\)/g, '<$1>'); text = text.replace(/\[(https?:.*?)\]\(\)/g, '<$1>'); text = text.replace(/\[key (.*?)\]/g, '`key:$1`'); text = text.replace(/\[demo([^\]]*?)\s*\/\]/g, '[demo$1]'); text = text.replace(/^```(\w+)[ ]*\n([\s\S]*?)^```/gim, function(match, lang, code) { let comment; code = code.trim(); comment = code.match(/^\/\/\+(.*?)(\n|$)/); if (comment) { comment = comment[1]; code = code.replace(/^\/\/\+(.*?)(\n|$)/, ''); } else { comment = code.match(/^(\n|$)/); if (comment) { comment = comment[1]; code = code.replace(/^(\n|$)/, ''); } } code = stripIndents(code); if (comment) comment = ' ' + comment.replace(/ /g, ' ').trim(); else comment = ''; if (!code) return `[${lang}${comment}]`; else return '```' + lang + comment + '\n' + code + '\n' + '```'; }); // EXTRACT META... let meta = {}; // importance -> meta text = text.replace(/\n\[importance (\d)\]\n/, function(match, importance) { meta.importance = +importance; return '\n'; }); text = text.replace(/\n\[libs\]([\s\S]*?)\[\/libs\]/, function(match, libs) { meta.libs = libs.trim().split('\n'); return '\n'; }); let head = ''; text = text.replace(/\n\[head\]([\s\S]*?)\[\/head\]/, function(match, headContent) { head = headContent.trim(); return '\n'; }); // ...EXTRACT META... // WITH LABELS... let codeBlockLabels = {}; let codeInlineLabels = {}; text = text.replace(/\n```[\s\S]*?^```/gim, function(code) { let label = '~CODELABEL:' + (Math.random() * 1e8 ^ 0) + '\n'; codeBlockLabels[label] = code; return label; }); let r = new RegExp('`[^`\n]+`', 'gim'); text = text.replace(r, function(code) { let label = '~INLINECODE:' + (Math.random() * 1e8 ^ 0); codeInlineLabels[label] = code; return label; }); text = text.replace(/^(\[.*?\])$/gim, '\n$1\n'); // ensure that all block tags are in paragraphs function fixListItem(listItem) { listItem = listItem.replace(/\n\n([.<*!а-яёa-z0-9])/gim, '\n\n $1'); let codeLabels = listItem.match(/~CODELABEL:\d+(\n|$)/g) || []; for (let i = 0; i < codeLabels.length; i++) { let label = codeLabels[i]; if (label[label.length - 1] != '\n') label += '\n'; codeBlockLabels[label] = indent(codeBlockLabels[label]); } return listItem; } text = text.replace(/