Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions desktop-app/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,21 @@ async function prepareOfflineDependencies() {
dest: path.join(LIBS_DIR, "mermaid.min.js"),
hash: null
},
{
url: "https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js",
dest: path.join(LIBS_DIR, "d3.min.js"),
hash: null
},
{
url: "https://cdn.jsdelivr.net/npm/markmap-lib@0.18.12/dist/browser/index.iife.js",
dest: path.join(LIBS_DIR, "markmap-lib.iife.js"),
hash: null
},
{
url: "https://cdn.jsdelivr.net/npm/markmap-view@0.18.12/dist/browser/index.js",
dest: path.join(LIBS_DIR, "markmap-view.js"),
hash: null
},
{
url: "https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js",
dest: path.join(LIBS_DIR, "tex-mml-chtml.min.js"),
Expand Down
16 changes: 7 additions & 9 deletions desktop-app/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; base-uri 'self'; object-src 'none'; script-src 'self' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net 'sha256-DgMFO4QE+qqf2xNgeNb5gMKG6BtiiQFniYj21c88yME='; worker-src 'self'; connect-src 'self' https://api.github.com https://raw.githubusercontent.com https://cdnjs.cloudflare.com https://cdn.jsdelivr.net https://kroki.io https://www.plantuml.com https://mermaid.ink https://paulrosen.github.io; img-src 'self' data: blob: https:; style-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com https://cdn.jsdelivr.net; font-src 'self' data: https://cdnjs.cloudflare.com https://cdn.jsdelivr.net; media-src 'self' blob: data:; manifest-src 'self'; upgrade-insecure-requests">
<!-- PERF-015: Preload critical-path resources for faster discovery -->
<link rel="preload" href="/styles.css" as="style">
<link rel="preload" href="script.js" as="script">
Expand All @@ -13,13 +14,12 @@
<!-- JSON-LD Structured Data Schema for Search Rich Snippets -->


<title>Markdown Viewer</title>
<title>Markdown Viewer - Secure Online Markdown Editor and Previewer</title>
<link href="/assets/icon.jpg" rel="icon" type="image/jpg">
<!-- Updated libraries to latest versions with Subresource Integrity (SRI) -->
<link rel="stylesheet" href="/libs/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="/libs/github-markdown.min.css" integrity="sha384-hZuxRjC/Dsr4zEx1JlUhDQqkvqBPp2VLHsgXfnxPq1ULDy1eIdWCiux7nvO1RIZP" crossorigin="anonymous">
<link rel="preload" href="/libs/bootstrap-icons.min.css" as="style" integrity="sha384-XGjxtQfXaH2tnPFa9x+ruJTuLE3Aa6LhHSWRr1XeTyhezb4abCG4ccI5AkVDxqC+" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/libs/bootstrap-icons.min.css" integrity="sha384-XGjxtQfXaH2tnPFa9x+ruJTuLE3Aa6LhHSWRr1XeTyhezb4abCG4ccI5AkVDxqC+" crossorigin="anonymous"></noscript>
<link rel="stylesheet" href="/libs/bootstrap-icons.min.css" integrity="sha384-XGjxtQfXaH2tnPFa9x+ruJTuLE3Aa6LhHSWRr1XeTyhezb4abCG4ccI5AkVDxqC+" crossorigin="anonymous">
<link rel="stylesheet" href="/styles.css">

<!-- Loading order optimized - ensure libraries are loaded asynchronously using defer -->
Expand Down Expand Up @@ -973,7 +973,7 @@ <h3 class="modal-section-title">Open-source credits</h3>
</div>
</div>

<script type="text/markdown" id="default-markdown">---
<textarea id="default-markdown" hidden aria-hidden="true">---
title: Welcome to Markdown Viewer
description: A GitHub-style Markdown renderer with live preview, math, diagrams, and export support.
author: ThisIs-Developer
Expand Down Expand Up @@ -1140,12 +1140,10 @@ <h3 class="modal-section-title">Open-source credits</h3>
## 🛡️ Security Note

This is a fully client-side application. Your content never leaves your browser and stays secure on your device.
</script>
</textarea>

<script src="/libs/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
<script src="/js/neutralino.js"></script>
<script src="/js/main.js"></script>
<script src="/js/script.js"></script>
<script src="/libs/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous" defer></script>
<script src="script.js" defer></script>
<!-- Screen reader dynamic accessibility announcer -->
<div id="app-accessibility-announcer" class="visually-hidden" aria-live="polite" aria-atomic="true"></div>
</body>
Expand Down
81 changes: 76 additions & 5 deletions desktop-app/resources/js/preview-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,17 @@ function configureMarked() {
const krokiLanguages = {
'vega-lite': ['vegalite', 'Vega-Lite'],
vegalite: ['vegalite', 'Vega-Lite'],
wavedrom: ['wavedrom', 'WaveDrom'],
markmap: ['markmap', 'Markmap']
wavedrom: ['wavedrom', 'WaveDrom']
};
if (krokiLanguages[language]) {
const [engine, label] = krokiLanguages[language];
const uniqueId = `${engine}-diagram-worker-${krokiIdCounter++}`;
return renderDiagramShell(engine, 'kroki-container', 'kroki-diagram', uniqueId, code, label);
}
if (language === 'markmap') {
const uniqueId = `markmap-diagram-worker-${krokiIdCounter++}`;
return renderDiagramShell('markmap', 'markmap-container', 'markmap-diagram', uniqueId, code, 'Markmap');
}

if (language === "math") {
return `<div class="math-block">$$\n${code}\n$$</div>\n`;
Expand All @@ -400,6 +403,72 @@ function configureMarked() {
return `<h${level} id="${id}">${text}</h${level}>`;
};

function normalizeMarkmapFences(markdown) {
const lines = String(markdown || '').split(/\r?\n/);
const output = [];
let index = 0;

while (index < lines.length) {
const opening = lines[index].match(/^([ \t]{0,3})(`{3,}|~{3,})([ \t]*)(.*)$/);
const info = opening ? opening[4].trim() : '';
if (!opening || !/^markmap(?:\s|$)/i.test(info)) {
output.push(lines[index]);
index += 1;
continue;
}

const indent = opening[1];
const fence = opening[2];
const marker = fence[0];
const content = [];
let nestedFence = null;
let maxInnerFenceLength = fence.length;
let closeIndex = -1;

for (let scan = index + 1; scan < lines.length; scan += 1) {
const line = lines[scan];
const fenceMatch = line.match(/^[ \t]{0,3}(`{3,}|~{3,})([ \t]*.*)$/);
if (fenceMatch) {
const currentFence = fenceMatch[1];
const currentMarker = currentFence[0];
const tail = fenceMatch[2].trim();
if (currentMarker === marker) {
maxInnerFenceLength = Math.max(maxInnerFenceLength, currentFence.length);
}

if (nestedFence) {
if (currentMarker === nestedFence.marker && currentFence.length >= nestedFence.length && tail === '') {
nestedFence = null;
}
} else if (currentMarker === marker && currentFence.length >= fence.length && tail === '') {
closeIndex = scan;
break;
} else if (tail !== '') {
nestedFence = {
marker: currentMarker,
length: currentFence.length
};
}
}
content.push(line);
}

if (closeIndex === -1) {
output.push(lines[index]);
index += 1;
continue;
}

const normalizedFence = marker.repeat(maxInnerFenceLength + 1);
output.push(`${indent}${normalizedFence}${opening[3]}${opening[4]}`);
output.push(...content);
output.push(`${indent}${normalizedFence}`);
index = closeIndex + 1;
}

return output.join('\n');
}

marked.use({
extensions: [
blockMathExtension,
Expand All @@ -412,7 +481,8 @@ function configureMarked() {
preprocess(markdown) {
if (suppressFootnotePreprocess) return markdown;
resetExtendedMarkdownState();
const protectedMarkdown = markdown.replace(/\\\$/g, "&#36;");
const normalizedMarkdown = normalizeMarkmapFences(markdown);
const protectedMarkdown = normalizedMarkdown.replace(/\\\$/g, "&#36;");
return applyFootnotes(extractFootnoteDefinitions(protectedMarkdown));
},
},
Expand Down Expand Up @@ -510,11 +580,12 @@ function splitMarkdownBlocks(markdown) {
}

function renderSegmentedMarkdown(markdown, options) {
if (!isSegmentedPreviewSafe(markdown)) {
const normalizedMarkdown = normalizeMarkmapFences(markdown);
if (!isSegmentedPreviewSafe(normalizedMarkdown)) {
return { mode: "full-required", reason: "unsafe-markdown" };
}

const blocks = splitMarkdownBlocks(markdown);
const blocks = splitMarkdownBlocks(normalizedMarkdown);
if (blocks.length < (options.minimumBlocks || 1)) {
return { mode: "full-required", reason: "too-few-blocks" };
}
Expand Down
Loading
Loading