ts-markdown-parser
Version:
TypeScript library that converts markdown to HTML (with code support).
62 lines • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.highlightGo = void 0;
const keywords_1 = require("./keywords");
const highlightGo = (code) => {
let escapedCode = code
.replace(/&/g, "&")
.replace(/:\/\//g, "://") // The `://` part of URLs
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/\\'/g, "'") // Replace escaped single quotes
.replace(/'/g, "'") // Replace unescaped single quotes
.replace(/\\"/g, """) // Replace escaped double quotes
.replace(/"/g, """) // Replace unescaped double quotes
.replace(/`/g, "`"); // Escape backticks as `
// Highlight comments (single-line and block comments)
const commentRegex = /(\/\/.*|\/\*[\s\S]*?\*\/)/g;
escapedCode = escapedCode.replace(commentRegex, '<span class="md-comment">$1</span>');
const commentSpanRegex = /<span class="md-comment">.*?<\/span>|([^<]+)(?=<span class="md-comment">|$)/g;
return escapedCode.replace(commentSpanRegex, (match, code) => {
// If the match is a comment span, return it unchanged
if (match.startsWith('<span class="md-comment">')) {
return match;
}
const stringRegex = /("|'|`)(.*?)(\1)/g;
code = code.replace(stringRegex, '<span class="md-string">$1$2$3</span>');
if (code.includes('<span class="md-string"')) {
// return code;
}
// Highlight reserved keywords
const replaceKeywords = (text) => {
return text.replace(/(<span[^>]*>.*?<\/span>)|(\b\w+\b)/g, (match, span, word) => {
if (span)
return span; // Skip spans
if (word && keywords_1.reservedKeywords.includes(word)) {
return `<span class="md-keyword">${word}</span>`;
}
return word;
});
};
code = replaceKeywords(code);
return code;
// TODO: The code after this is not working, because of `md-string` issues
// TODO: Fix this sometime, test, debug, etc..
const mdSpanRegexComplete = /(<span class="md-[^"]*">)([\s\S]*?)(<\/span>)/;
const mdSpanRegexPart = /(<span class="md-[^"]*">)/;
// Highlight Go numbers
code = code.replace(/(\d+(?:\.\d+)?)/g, '<span class="md-number">$1</span>');
// Highlight Go selectors (assumes anything before a curly brace could be a selector)
const libMethodCallStr = `<span class="md-special">$1</span>$2<span class="md-call-method">$3</span>`;
code = code.replace(/([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9_-]+)(?=\s*[\{(])/g, libMethodCallStr);
// Highlight Go pseudo-classes (like :hover, :nth-child, etc.)
code = code.replace(/(:[a-zA-Z0-9_-]+)/g, '<span class="md-decorator">$1</span>');
// Highlight Go variables (starting with $)
code = code.replace(/(\$[a-zA-Z0-9_-]+)/g, '<span class="md-special">$1</span>');
// Highlight other method calls like `.myMethod(` or `.myFunc(`
const methodCallRegEx = /\.(\w+)\(/g;
return code.replace(methodCallRegEx, '.<span class="md-call-method">$1</span>(');
});
};
exports.highlightGo = highlightGo;
//# sourceMappingURL=highlight.js.map