Merge pull request 'Fix require() of ES Module /app/node_modules/marked/lib/marked.esm.js from /app/server.js not supported.' (#16) from next into master

Reviewed-on: #16
This commit was merged in pull request #16.
This commit is contained in:
2025-07-12 06:19:11 +00:00

View File

@@ -18,7 +18,6 @@ const pdfParse = require('pdf-parse'); // For PDF files
const ExcelJS = require('exceljs'); // For Excel files
// Markdown and HTML processing
const { marked } = require('marked');
const createDOMPurify = require('dompurify');
const { JSDOM } = require('jsdom');
@@ -26,6 +25,16 @@ const { JSDOM } = require('jsdom');
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);
// Dynamic import for marked (ES module)
let marked = null;
async function initializeMarked() {
if (!marked) {
const markedModule = await import('marked');
marked = markedModule.marked;
}
return marked;
}
// Helper function to extract text from various document formats
async function extractTextFromDocument(filePath, fileExtension) {
try {
@@ -1454,7 +1463,7 @@ app.post('/api/render-revised-content', requireAuth, async (req, res) => {
case 'html':
if (isMarkdownContent || autoDetect === false) {
// Convert markdown to safe HTML
renderedContent = markdownToSafeHtml(content);
renderedContent = await markdownToSafeHtml(content);
} else {
// Just escape HTML and preserve line breaks for plain text
renderedContent = escapeHtml(content).replace(/\n/g, '<br>');
@@ -2705,10 +2714,13 @@ app.listen(PORT, () => {
});
// Helper function to convert markdown to safe HTML
function markdownToSafeHtml(markdownText) {
async function markdownToSafeHtml(markdownText) {
try {
// Initialize marked with dynamic import
const markedInstance = await initializeMarked();
// Configure marked options for better security and features
marked.setOptions({
markedInstance.setOptions({
gfm: true, // GitHub Flavored Markdown
breaks: true, // Convert line breaks to <br>
sanitize: false, // We'll use DOMPurify instead for better control
@@ -2718,7 +2730,7 @@ function markdownToSafeHtml(markdownText) {
});
// Convert markdown to HTML
const rawHtml = marked.parse(markdownText);
const rawHtml = markedInstance.parse(markdownText);
// Sanitize the HTML with DOMPurify
const cleanHtml = DOMPurify.sanitize(rawHtml, {