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
19 changes: 18 additions & 1 deletion client_ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,30 @@
const height = doc.scrollHeight - window.innerHeight;
window.scrollTo(0, Math.round(height * percent));
} else if (msg.type === "toggleTheme") {
document.documentElement.classList.toggle("mdview-dark");
document.body.classList.toggle("mdview-dark");
}
} catch (e) {
console.error("bad msg", e);
}
});

// Keyboard shortcut "t" to toggle theme
window.addEventListener("keydown", (e) => {
if ((e.key || "").toLowerCase() === "t") {
document.body.classList.toggle("mdview-dark");
}
});

// Click handler for theme toggle button
window.addEventListener("DOMContentLoaded", () => {
const btn = document.getElementById("theme-toggle-btn");
if (btn) {
btn.addEventListener("click", () => {
document.body.classList.toggle("mdview-dark");
});
}
});

// Enhanced image zoom UI with better error handling
document.addEventListener("click", (e) => {
const t = e.target;
Expand Down
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ function renderMarkdown() {
const templatePath = path.join(__dirname, "template.html");
const args = [
MARKDOWN_FILE,
"--from=gfm+tex_math_dollars+tex_math_single_backslash+pipe_tables+table_captions+task_lists+smart+emoji",
"--to=html5",
"--template=" + templatePath,
"-s",
"--mathjax", // Enable MathJax for mathematical expressions
"--standalone",
"--metadata", "title=MDView",
"--resource-path=" + path.dirname(MARKDOWN_FILE),
"-o",
"-" // output to stdout
];
Expand Down
53 changes: 3 additions & 50 deletions template.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<script>
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
inlineMath: [['$$', '$$'], ['\\(', '\\)']],
displayMath: [['$$$$', '$$$$'], ['\\[', '\\]']]
},
svg: {
fontCache: 'global'
Expand Down Expand Up @@ -124,51 +124,4 @@
$body$
</div>

<script>
(function () {
const ws = new WebSocket("ws://127.0.0.1:7070");
let isScrollingFromEditor = false;

// Theme toggle function
function toggleTheme() {
document.body.classList.toggle("mdview-dark");
}

// Handle websocket messages
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "scrollTo") {
isScrollingFromEditor = true;
window.scrollTo({top: msg.position});
setTimeout(() => {isScrollingFromEditor = false;}, 50);
} else if (msg.type === "toggleTheme") {
toggleTheme();
}
};

// Send scroll position back to server
window.addEventListener("scroll", () => {
if (!isScrollingFromEditor) {
ws.send(JSON.stringify({type: "scroll", position: window.scrollY}));
}
});

// Image zoom
document.querySelectorAll("img").forEach(img => {
img.addEventListener("click", () => {
img.classList.toggle("zoomed");
});
});

// Keyboard shortcut "t" to toggle theme
window.addEventListener("keydown", (e) => {
if (e.key.toLowerCase() === "t") {
toggleTheme();
}
});

// Button click to toggle theme
document.getElementById("theme-toggle-btn").addEventListener("click", toggleTheme);

})();
</script>
<!-- WebSocket and UI handlers are injected by client_ws.js -->
Loading