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
14 changes: 14 additions & 0 deletions desktop-app/resources/js/preview-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ function configureMarked() {
return `<pre><code class="hljs ${escapeHtmlAttribute(validLanguage)}">${highlightedCode}</code></pre>`;
};

renderer.heading = function(text, level, raw) {
let id = raw
.toLowerCase()
.trim()
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, '-')
.replace(/[^\w-]/g, '')
.replace(/-+/g, '-');
if (!id) {
id = `heading-worker-${Math.random().toString(36).substr(2, 9)}`;
}
return `<h${level} id="${id}">${text}</h${level}>`;
};

marked.use({
extensions: [
blockMathExtension,
Expand Down
90 changes: 86 additions & 4 deletions desktop-app/resources/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ document.addEventListener("DOMContentLoaded", function () {
let syncScrollingEnabled = true;
let isEditorScrolling = false;
let isPreviewScrolling = false;
let isProgrammaticScrolling = false;
let scrollSyncTimeout = null;
const SCROLL_SYNC_DELAY = 10;

Expand Down Expand Up @@ -931,6 +932,20 @@ document.addEventListener("DOMContentLoaded", function () {
return `<pre><code class="hljs ${validLanguage}">${highlightedCode}</code></pre>`;
};

renderer.heading = function (text, level, raw) {
let id = raw
.toLowerCase()
.trim()
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, '-')
.replace(/[^\w-]/g, '')
.replace(/-+/g, '-');
if (!id) {
id = 'heading-' + Math.random().toString(36).substr(2, 9);
}
return `<h${level} id="${id}">${text}</h${level}>`;
};

marked.use({
extensions: [
blockMathExtension,
Expand Down Expand Up @@ -2921,7 +2936,7 @@ document.addEventListener("DOMContentLoaded", function () {
}

function syncEditorToPreview() {
if (!syncScrollingEnabled || isPreviewScrolling) return;
if (!syncScrollingEnabled || isPreviewScrolling || isProgrammaticScrolling) return;
isEditorScrolling = true;

if (scrollSyncTimeout) cancelAnimationFrame(scrollSyncTimeout);
Expand All @@ -2944,7 +2959,7 @@ document.addEventListener("DOMContentLoaded", function () {
}

function syncPreviewToEditor() {
if (!syncScrollingEnabled || isEditorScrolling) return;
if (!syncScrollingEnabled || isEditorScrolling || isProgrammaticScrolling) return;
isPreviewScrolling = true;

if (scrollSyncTimeout) cancelAnimationFrame(scrollSyncTimeout);
Expand Down Expand Up @@ -6583,12 +6598,33 @@ document.addEventListener("DOMContentLoaded", function () {
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
max-width: 100%;
width: fit-content;
margin: 0 auto;
padding: 45px;
background-color: ${isDarkTheme ? "#0d1117" : "#ffffff"};
color: ${isDarkTheme ? "#c9d1d9" : "#24292e"};
}
.markdown-body > p,
.markdown-body > ul,
.markdown-body > ol,
.markdown-body > blockquote,
.markdown-body > h1,
.markdown-body > h2,
.markdown-body > h3,
.markdown-body > h4,
.markdown-body > h5,
.markdown-body > h6,
.markdown-body > pre,
.markdown-body > table,
.markdown-body > details,
.markdown-body > dl,
.markdown-body > hr {
max-width: 980px;
margin-left: auto !important;
margin-right: auto !important;
}


/* Syntax Highlighting */
.hljs-doctag, .hljs-keyword, .hljs-template-tag, .hljs-template-variable, .hljs-type, .hljs-variable.language_ { color: ${isDarkTheme ? "#ff7b72" : "#d73a49"}; }
Expand Down Expand Up @@ -9646,7 +9682,53 @@ document.addEventListener("DOMContentLoaded", function () {
const href = link.getAttribute('href');
if (href) {
if (href.startsWith('#')) {
return; // Allow internal anchor navigation
const targetId = decodeURIComponent(href.slice(1));
let targetEl = null;
if (targetId) {
try {
targetEl = markdownPreview.querySelector(`[id="${CSS.escape(targetId)}"]`) ||
markdownPreview.querySelector(`[name="${CSS.escape(targetId)}"]`);
} catch (err) {
targetEl = Array.from(markdownPreview.querySelectorAll('[id], [name]')).find(el => {
return el.getAttribute('id') === targetId || el.getAttribute('name') === targetId;
});
}

if (!targetEl) {
const cleanTargetId = targetId.toLowerCase().replace(/[^a-z0-9]/g, '');
if (cleanTargetId) {
targetEl = Array.from(markdownPreview.querySelectorAll('h1, h2, h3, h4, h5, h6')).find(heading => {
const cleanText = heading.textContent.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleanText === cleanTargetId;
});
}
}
}
if (targetEl) {
e.preventDefault();
isProgrammaticScrolling = true;

// Scroll preview pane to target heading
targetEl.scrollIntoView({ behavior: 'smooth', block: 'start' });

// Scroll editor pane to the matching synced position
const previewScrollRange = previewPane.scrollHeight - previewPane.clientHeight;
const targetRatio = previewScrollRange > 0 ? Math.min(1, Math.max(0, targetEl.offsetTop / previewScrollRange)) : 0;
const editorScrollPosition = targetRatio * (markdownEditor.scrollHeight - markdownEditor.clientHeight);

markdownEditor.scrollTo({
top: editorScrollPosition,
behavior: 'smooth'
});

if (window.programmaticScrollTimeout) {
clearTimeout(window.programmaticScrollTimeout);
}
window.programmaticScrollTimeout = setTimeout(() => {
isProgrammaticScrolling = false;
}, 1000);
}
return;
}

e.preventDefault();
Expand Down
14 changes: 14 additions & 0 deletions preview-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ function configureMarked() {
return `<pre><code class="hljs ${escapeHtmlAttribute(validLanguage)}">${highlightedCode}</code></pre>`;
};

renderer.heading = function(text, level, raw) {
let id = raw
.toLowerCase()
.trim()
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, '-')
.replace(/[^\w-]/g, '')
.replace(/-+/g, '-');
if (!id) {
id = `heading-worker-${Math.random().toString(36).substr(2, 9)}`;
}
return `<h${level} id="${id}">${text}</h${level}>`;
};

marked.use({
extensions: [
blockMathExtension,
Expand Down
67 changes: 64 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ document.addEventListener("DOMContentLoaded", function () {
let syncScrollingEnabled = true;
let isEditorScrolling = false;
let isPreviewScrolling = false;
let isProgrammaticScrolling = false;
let scrollSyncTimeout = null;
const SCROLL_SYNC_DELAY = 10;

Expand Down Expand Up @@ -931,6 +932,20 @@ document.addEventListener("DOMContentLoaded", function () {
return `<pre><code class="hljs ${validLanguage}">${highlightedCode}</code></pre>`;
};

renderer.heading = function (text, level, raw) {
let id = raw
.toLowerCase()
.trim()
.replace(/<[^>]*>/g, '')
.replace(/\s+/g, '-')
.replace(/[^\w-]/g, '')
.replace(/-+/g, '-');
if (!id) {
id = 'heading-' + Math.random().toString(36).substr(2, 9);
}
return `<h${level} id="${id}">${text}</h${level}>`;
};

marked.use({
extensions: [
blockMathExtension,
Expand Down Expand Up @@ -2921,7 +2936,7 @@ document.addEventListener("DOMContentLoaded", function () {
}

function syncEditorToPreview() {
if (!syncScrollingEnabled || isPreviewScrolling) return;
if (!syncScrollingEnabled || isPreviewScrolling || isProgrammaticScrolling) return;
isEditorScrolling = true;

if (scrollSyncTimeout) cancelAnimationFrame(scrollSyncTimeout);
Expand All @@ -2944,7 +2959,7 @@ document.addEventListener("DOMContentLoaded", function () {
}

function syncPreviewToEditor() {
if (!syncScrollingEnabled || isEditorScrolling) return;
if (!syncScrollingEnabled || isEditorScrolling || isProgrammaticScrolling) return;
isPreviewScrolling = true;

if (scrollSyncTimeout) cancelAnimationFrame(scrollSyncTimeout);
Expand Down Expand Up @@ -9667,7 +9682,53 @@ document.addEventListener("DOMContentLoaded", function () {
const href = link.getAttribute('href');
if (href) {
if (href.startsWith('#')) {
return; // Allow internal anchor navigation
const targetId = decodeURIComponent(href.slice(1));
let targetEl = null;
if (targetId) {
try {
targetEl = markdownPreview.querySelector(`[id="${CSS.escape(targetId)}"]`) ||
markdownPreview.querySelector(`[name="${CSS.escape(targetId)}"]`);
} catch (err) {
targetEl = Array.from(markdownPreview.querySelectorAll('[id], [name]')).find(el => {
return el.getAttribute('id') === targetId || el.getAttribute('name') === targetId;
});
}

if (!targetEl) {
const cleanTargetId = targetId.toLowerCase().replace(/[^a-z0-9]/g, '');
if (cleanTargetId) {
targetEl = Array.from(markdownPreview.querySelectorAll('h1, h2, h3, h4, h5, h6')).find(heading => {
const cleanText = heading.textContent.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleanText === cleanTargetId;
});
}
}
}
if (targetEl) {
e.preventDefault();
isProgrammaticScrolling = true;

// Scroll preview pane to target heading
targetEl.scrollIntoView({ behavior: 'smooth', block: 'start' });

// Scroll editor pane to the matching synced position
const previewScrollRange = previewPane.scrollHeight - previewPane.clientHeight;
const targetRatio = previewScrollRange > 0 ? Math.min(1, Math.max(0, targetEl.offsetTop / previewScrollRange)) : 0;
const editorScrollPosition = targetRatio * (markdownEditor.scrollHeight - markdownEditor.clientHeight);

markdownEditor.scrollTo({
top: editorScrollPosition,
behavior: 'smooth'
});

if (window.programmaticScrollTimeout) {
clearTimeout(window.programmaticScrollTimeout);
}
window.programmaticScrollTimeout = setTimeout(() => {
isProgrammaticScrolling = false;
}, 1000);
}
return;
}

e.preventDefault();
Expand Down
Loading
Loading