Yaml widget for frontmatter + Disabling KaTeX equation scrollbar until hover - #368
Open
Al3cLee wants to merge 4 commits into
Open
Yaml widget for frontmatter + Disabling KaTeX equation scrollbar until hover#368Al3cLee wants to merge 4 commits into
Al3cLee wants to merge 4 commits into
Conversation
Use markdown-it-front-matter plugin to intercept frontmatter at parse time instead of rendering it as <hr> + paragraph text. Wraps the raw YAML in a <details>/<summary> block with open/closed state preserved across live updates.
The HTML template wraps the markdown body with a leading newline, so reading .textContent gives a string starting with \n. The frontmatter plugin guards on charCodeAt(0) === '-', so the leading \n caused it to silently skip the frontmatter block on every initial page load. Live updates via WebSocket were unaffected since buffer content has no leading whitespace.
KaTeX's em-based sizing causes sub-pixel rounding that makes scrollWidth 1-2px larger than clientWidth, triggering overflow-x:auto to show a scrollbar even when the equation visually fits. Make the scrollbar thumb transparent by default and reveal it on hover, matching overlay-scrollbar behavior for both WebKit and Firefox.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes two independent improvements to the markdown preview:
▶ Document metadatawidget instead of leaking into the document as raw<hr>and paragraph elements.There is a newly introduced file
markdown-it-front-matter.min.jsof size 2.1 KB.1. YAML Frontmatter Collapsible Widget
Problem
Currently, the parser markdown-it has no front-matter awareness. Its
hrblock rule matches---on any line without astartLine === 0guard, so a YAML front-matter block at the top of a file was rendered as:This becomes ugly when one deals with, e.g.,
SKILL.mdfiles where the yaml section is quite large.Approach
The
markdown-it-front-matterplugin (v0.2.4) is bundled as a self-contained UMD module. It inserts a new block rule before thetablerule in markdown-it's block ruler chain, with an explicitstartLine !== 0guard so it fires only when---appears at the very first character of the document. The rule produces a token withhidden: true(suppressing any HTML output) and fires a synchronous callback with the raw YAML string.Files changed
static/markdown/markdown-it-front-matter.min.js(new)UMD wrapper around the plugin source, exposing
window.markdownitFrontMatter.static/markdown/main.jsmain.js:22–25: The plugin is registered with a callback that stores the captured YAML into the module-level variable_frontmatter. This variable is reset tonullat the top of everylivepreview_rendercall so stale frontmatter from a previous render cannot bleed through.main.js:27–37:buildFrontmatterHtml(yamlText, open)constructs a native HTML<details>/<summary>element. The raw YAML is HTML-escaped and placed inside a fenced<pre><code class="language-yaml">block so highlight.js applies syntax colouring automatically via the existinghljs.highlightAll()call. Theopenparameter sets theopenattribute on<details>to restore the user's expand/collapse state across re-renders.main.js:39–49:livepreview_rendernow reads theopenproperty of any existing.frontmatter-blockelement before replacinginnerHTML, then passes it tobuildFrontmatterHtml. This ensures that a live edit in Neovim does not collapse the widget if the user had expanded it.main.js:52: The initialtextContentread is now.trim()'d before being passed tolivepreview_render. The HTML template intemplate.luaplaces the markdown body after a literal\ninside the<div class="markdown-body">, sotextContentalways starts with a newline. The frontmatter plugin's first guard checksstate.src.charCodeAt(0) === '-'(char code 45); the leading\n(char code 10) caused the plugin to silently bail out on every initial page load. Live updates via WebSocket were unaffected because Neovim's buffer content (table.concat(lines, "\n")inlua/livepreview/init.lua:64) carries no leading whitespace.lua/livepreview/template.luatemplate.lua:67(themd2htmlscript string): a<script defer>tag formarkdown-it-front-matter.min.jsis inserted between the KaTeX plugin andmain.js. All markdown-it plugin scripts usedefer, so they execute in document order after DOM parsing; placing it beforemain.jsguaranteeswindow.markdownitFrontMatteris defined whenmain.jsruns.template.lua:46–52: Seven CSS rules are added to the existing inline<style>block for.frontmatter-block. The▶/▼triangle is rendered viasummary::beforecontent rather than the browser's native<details>marker (which is suppressed with::-webkit-details-marker{display:none}), giving consistent appearance across browsers. A@media(prefers-color-scheme:dark)rule adjusts the border colour from GitHub's light-mode#d0d7deto its dark-mode equivalent#30363d.2. KaTeX Display Scrollbar Fix
Problem
The pre-existing rule in
template.lua:36:caused a scrollbar to appear on every
\begin{equation}or\begin{align}environment, even when the rendered formula was narrower than the page. This is actually the default behavior that one sees atkatex.org, but in a document preview, such scroll bars are too eye-catchy; they resemble the horizontal rule---but are actually not. KaTeX usesem-based internal sizing, and sub-pixel rounding routinely makesscrollWidth1–2px larger thanclientWidth— just enough foroverflow-x:autoto activate. Code blocks do not have this problem becausewhite-space:pregives the browser an exact content width to measure against.Fix
template.lua:36–41: The.katex-displayrule is extended and five additional rules are added, all within the existing inline<style>block:overflow-x:autois intentionally kept so that a formula genuinely wider than the viewport is still scrollable. The scrollbar is made invisible by default (transparent thumb, zero-opacity track colour) and fades in on hover — matching the overlay-scrollbar behaviour that code blocks already get on macOS via the OS layer, and extending equivalent behaviour to Windows/Linux where scrollbars are always-visible by default.Files changed
static/markdown/markdown-it-front-matter.min.jsstatic/markdown/main.jslua/livepreview/template.lua