UNPKG

ts-markdown-parser

Version:

TypeScript library that converts markdown to HTML (with code support).

147 lines 5.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.markdownReplace = exports.countOccurrences = exports.stripLeadingWhitespace = exports.highlightCode = exports.getMultilineCommentRegex = exports.languageAliases = void 0; const highlight_1 = require("./javascript/highlight"); const highlight_2 = require("./python/highlight"); const highlight_3 = require("./html/highlight"); const highlight_4 = require("./css/highlight"); const highlight_5 = require("./golang/highlight"); const highlight_6 = require("./jsx/highlight"); const highlight_7 = require("./sql/highlight"); const highlight_8 = require("./lua/highlight"); const highlight_9 = require("./rust/highlight"); // Mapping from aliases to full language names exports.languageAliases = { js: "javascript", javascript: "javascript", typescript: "javascript", ts: "javascript", python: "python", py: "python", html: "html", css: "css", scss: "scss", go: "golang", golang: "golang", jsx: "jsx", tsx: "jsx", sql: "sql", lua: "lua", rust: "rust", rs: "rust", }; const highlightFunctions = { javascript: highlight_1.highlightJavaScript, js: highlight_1.highlightJavaScript, ts: highlight_1.highlightJavaScript, typescript: highlight_1.highlightJavaScript, python: highlight_2.highlightPython, py: highlight_2.highlightPython, html: highlight_3.highlightHtml, css: highlight_4.highlightCss, scss: highlight_4.highlightCss, go: highlight_5.highlightGo, golang: highlight_5.highlightGo, jsx: highlight_6.highlightJSX, tsx: highlight_6.highlightJSX, sql: highlight_7.highlightSQL, lua: highlight_8.highlightLua, rust: highlight_9.highlightRust, rs: highlight_9.highlightRust, }; const cssStart = /(^\/\*)/; const cssEnd = /(^\*\/|^\s\*\/)/; const regexJavaScriptStart = /(^\/\*\*|^\/\*|\s\/\*\*|\s\/\*)/; // Matches /* and /** but not * or ** const regexJavaScriptEnd = /(^\*\/|\s\*\/)/; // Matches `*/` or ` */` const regexPython = /('''|""")/; // Matches the start of a Python multiline comment (''' or """) const goRegexStart = /(\/\*)/; // Go block comment start const goRegexEnd = /(\*\/)/; // Go block comment end const multilineCommentMap = { javascript: { start: regexJavaScriptStart, end: regexJavaScriptEnd }, js: { start: regexJavaScriptStart, end: regexJavaScriptEnd }, typescript: { start: regexJavaScriptStart, end: regexJavaScriptEnd }, jsx: { start: regexJavaScriptStart, end: regexJavaScriptEnd }, tsx: { start: regexJavaScriptStart, end: regexJavaScriptEnd }, ts: { start: regexJavaScriptStart, end: regexJavaScriptEnd }, python: { start: regexPython, end: regexPython }, py: { start: regexPython, end: regexPython }, html: { start: /<!--/, end: /-->/ }, // HTML comments are not multiline but for structure css: { start: cssStart, end: cssEnd }, scss: { start: cssStart, end: cssEnd }, go: { start: goRegexStart, end: goRegexEnd }, golang: { start: goRegexStart, end: goRegexEnd }, sql: { start: /\/\*/, end: /\*\// }, lua: { start: /(^--\[\[)/, end: /(^--\]\]|^]])/ }, // Lua block comments rust: { start: /\/\*/, end: /\*\// }, }; const getMultilineCommentRegex = (language) => { return multilineCommentMap[language] || null; }; exports.getMultilineCommentRegex = getMultilineCommentRegex; const highlightCode = (language, code) => { const normalizedLanguage = exports.languageAliases[language] || language; const highlightFunction = highlightFunctions[normalizedLanguage]; return highlightFunction ? highlightFunction(code) : code; }; exports.highlightCode = highlightCode; /** * Removes leading whitespace and newline characters from a string * until a non-whitespace/non-newline character is encountered. * * @param str - The input string to be processed. * @returns A string with leading whitespace/newlines removed. */ const stripLeadingWhitespace = (str) => { // Regular expression to match leading newlines and spaces const regex = /^[\s\n]+/; // Replace the leading whitespace and newlines with an empty string return str.replace(regex, "").trim(); }; exports.stripLeadingWhitespace = stripLeadingWhitespace; /** * Counts the occurrences of a target, trimmed string in an array. * * @param {string[]} arr * @returns {number} */ const countOccurrences = (arr, target) => { if (!arr || !arr.length) { return 0; } let total = 0; for (let i = 0; i < arr.length; i++) { const str = arr[i].trim(); if (target === str) { total++; } } return total; }; exports.countOccurrences = countOccurrences; /** * Replaces specified characters in a string, ignoring any characters that are part of HTML entities. * * @param {string} text * @param {string} replaceRegex * @param {string} replacement * @returns {string} */ const markdownReplace = (text, replaceRegex, replacement) => { const htmlEntityRegex = /(&.+;)(.*?)(\1)/gi; // Split the text by HTML entities const parts = text.split(htmlEntityRegex); const replacedParts = parts.map((item) => { // If it's an HTML entity, return it unchanged const isHTMLEntity = /(&.+;)(.*?)/g.test(item); if (isHTMLEntity) return item; // If it's a `<span class="md` element, return it unchanged // const spanRegex = new RegExp(`<span class="md-'`, "g"); // const isSpan = spanRegex.test(item); // if (isSpan) return item; return item.replace(replaceRegex, replacement); }); return replacedParts.join(""); }; exports.markdownReplace = markdownReplace; //# sourceMappingURL=index.js.map