stackchain-dashboard/frontend/markdown.js
timmy f63c8c39b1
All checks were successful
CI / lint (pull_request) Successful in 7s
CI / build-frontend (pull_request) Successful in 4s
fix: escape markdown preview HTML (#52)
2026-08-05 23:48:27 +00:00

30 lines
1.0 KiB
JavaScript

(function (root, factory) {
const renderMarkdown = factory();
if (typeof module === 'object' && module.exports) module.exports = renderMarkdown;
root.renderMarkdown = renderMarkdown;
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
function escapeHtml(value) {
return String(value || '').replace(/[&<>"']/g, (character) => ({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
})[character]);
}
return function renderMarkdown(raw) {
return escapeHtml(raw)
.replace(/^#{1,6}\s.*$/gm, (line) => {
const level = line.match(/^(#{1,6})/)[1].length;
return '<h' + level + '>' + line.replace(/^#{1,6}\s/, '') + '</h' + level + '>';
})
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.+?)\*/g, '<em>$1</em>')
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(/^-\s?(.+)$/gm, '<li>$1</li>')
.replace(/(<li>.*<\/li>)/s, '<ul>$1</ul>')
.replace(/\n/g, '<br>');
};
});