Changeset 0.37.8 (#220)
Co-authored-by: gobha <jasafpro@gmail.com> Co-committed-by: gobha <jasafpro@gmail.com>
This commit is contained in:
41
src/js/sw/components/chat-pane/markdown.js
Normal file
41
src/js/sw/components/chat-pane/markdown.js
Normal file
@@ -0,0 +1,41 @@
|
||||
// ==========================================
|
||||
// ChatPane Kit — Markdown Renderer
|
||||
// ==========================================
|
||||
// Wraps window.marked + window.DOMPurify with fallback.
|
||||
// Independently importable utility.
|
||||
//
|
||||
// Usage:
|
||||
// import { renderMarkdown } from './markdown.js';
|
||||
// const html = renderMarkdown('**bold** text');
|
||||
|
||||
let _lastInput = '';
|
||||
let _lastOutput = '';
|
||||
|
||||
const _escMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
||||
|
||||
function _escapeHtml(text) {
|
||||
return text.replace(/[&<>"']/g, c => _escMap[c]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown text to sanitized HTML.
|
||||
* Falls back to escaped plaintext when marked/DOMPurify unavailable.
|
||||
*
|
||||
* @param {string} text — raw markdown
|
||||
* @returns {string} sanitized HTML
|
||||
*/
|
||||
export function renderMarkdown(text) {
|
||||
if (!text) return '';
|
||||
if (text === _lastInput) return _lastOutput;
|
||||
|
||||
let result;
|
||||
if (typeof marked !== 'undefined' && typeof DOMPurify !== 'undefined') {
|
||||
result = DOMPurify.sanitize(marked.parse(text));
|
||||
} else {
|
||||
result = _escapeHtml(text);
|
||||
}
|
||||
|
||||
_lastInput = text;
|
||||
_lastOutput = result;
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user