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