Compare commits

..
Author SHA1 Message Date
Yuri Karamian f39b301efc fix: judge network paths by their text on Windows
On Windows, a network path from note content that lies outside the vault
and outside any folder the user opened is refused before the filesystem is
asked about it, in the backend path checks and in the frontend resolvers.
Other platforms are unchanged.
2026-10-10 17:29:10 +02:00
Yuri Karamian 1ad948d4f0 fix: open only document and media files with the default application
Other file types are shown in the file manager instead of being handed to
the system opener.
2026-10-10 17:19:19 +02:00
Yuri Karamian 6341fa8c9b fix: bound frontmatter parsing
Frontmatter is parsed through a YAML engine with limits on size, aliases
and nesting. Frontmatter outside those limits is read as a note without
metadata instead of being handed to the unbounded loader.
2026-10-10 16:21:23 +02:00
Yuri Karamian 21ab7ac49e fix: keep editor state consistent when a note fails to load
A note that cannot be converted for the rich editor no longer leaves the
previous note's content in place. The editor is emptied and locked, saving
is blocked, and source mode shows the real content. Programmatic document
changes no longer leave the next user edit untracked.
2026-10-10 15:28:17 +02:00
Yuri Karamian 3b25992a15 fix: validate remote paths during sync
Paths from a WebDAV listing and from the stored sync state are now checked
before they are used as local file paths, and other spellings of the app's
internal folder are never synced.
2026-10-10 14:30:10 +02:00
Yuri Karamian 33514f28c9 fix: name the operation and file in sync errors
A local file failure during WebDAV sync surfaced as a bare OS error such as
"Operation not permitted (os error 1)", with no file and no hint that it
was local. Local failures now name themselves, the operation and the file,
and requests that get no answer name the operation and remote path.

Refs #249
2026-10-10 13:34:39 +02:00
Yuri Karamian 6e2165661c fix: keep the note title readable in a narrow editor pane
The title was the only part of the header that could shrink, down to a few
characters or nothing. It now keeps a minimum width and the toolbar buttons
move to a second row when there is no room. Unchanged when there is room,
and on mobile.

Refs #246
2026-10-10 13:34:39 +02:00
Yuri Karamian e168f4e004 fix: no indented blank line after a nested list
A nested list left an indented blank line behind when saved, which showed
as an extra empty line after the list in source view.

Refs #277
2026-10-10 13:34:39 +02:00
Yuri Karamian 2014365e4a fix: reset the zoom to the font size chosen in Appearance
Ctrl+0 reset the editor font to a fixed 14 px, because zooming overwrote
the size chosen in Appearance. That choice is now kept separately and
Ctrl+0 returns to it. For existing installs the size in use at first
launch becomes the preference.

Refs #272
2026-10-10 13:34:39 +02:00
Yuri Karamian 8df5efacb8 fix: write spaces at the edges of bold and italic outside the markers
Text formatted with a trailing or leading space was saved as **bold **rest,
which Markdown does not read as bold, so it showed as literal asterisks
after reopening. Edge spaces now go outside the markers and a mark over
only spaces is not written. Formatting around an inline code span is
dropped when a letter or digit touches it, where it would not render.

Refs #240, #276
2026-10-10 13:34:38 +02:00
Yuri Karamian f87b8af238 fix: let Backspace leave and remove a collapsible section
Backspace at the start of a collapsible section's body did nothing, so the
caret could never leave it and a section on the first line of a note could
not be removed from the keyboard. It now steps back into the summary, where
further Backspaces delete the text and unwrap the section. A section with
nothing in it is removed.

Refs #278
2026-10-10 13:34:38 +02:00
Yuri Karamian f0bf0ac5e3 fix: keep bare code fences, link titles, table alignment and formatted inline code
A round-trip audit of 1590 notes showed four things lost the first time a
note was edited. A code block without a language was saved as "text".
Link titles were dropped. Table column alignment was reset. Bold or
italic around inline code was removed.

All four now survive loading, editing and saving. Formatting that covers
only part of a code run is still dropped, since it cannot be written
inside a code span.
2026-10-10 10:19:04 +02:00
Yuri Karamian 0ecacbe1dd fix: parse lines with many unclosed links in linear time, and close a stale image toolbar
A line with thousands of unclosed [label]( starts took about 1.1 s at
56 KB, because each start read to the end of the line for its closing
parenthesis. The legacy link rule now skips starts beyond the last
closing parenthesis and bounds the failed searches per block.

The image toolbar stayed open after an undo and its buttons then changed
a different image. It now closes when its image is no longer at the
remembered position.
2026-10-09 23:28:46 +02:00
Yuri Karamian 455b059fb5 fix: group images a blank line apart into a grid, and keep blank lines on ungroup
Images separated by a blank line load with an empty paragraph between
them, so the Grid button never found two neighboring image blocks and
did nothing in an existing note. Grouping now spans a single empty
paragraph between image blocks and takes the one above the first image,
which would otherwise be saved as a marker line above the grid.

Ungroup gave each image its own paragraph but no blank line, so the
images were saved directly under the preceding paragraph and became part
of it on reload. Top-level grids now ungroup into images a blank line
apart, the reverse of grouping them.

Refs #254
2026-10-09 23:06:53 +02:00
Yuri Karamian f4c6649c0a Merge branch 'feature/side-by-side-grid-masonry-layout-for-images' into 'main' 2026-10-09 22:50:50 +02:00
Yuri Karamian 6c4d1c2e93 Merge branch 'feature/image-hover-titles' into 'main' 2026-10-09 22:45:39 +02:00
Shawn Olson 32b1e091fd fix: keep formatting when pasting from the context menu on WebKitGTK
For content copied from another app, WebKitGTK's navigator.clipboard.read() often
returns nothing, or only text/plain, while Ctrl+V still receives the HTML. The
context menu then pasted plain text and lost bold, links and other formatting.

Whatever the web clipboard leaves out is now filled in from the native clipboard.
The new read_clipboard_contents command (replacing read_clipboard_text) returns
both text and HTML through arboard. The result is replayed through the same paste
handlers as Ctrl+V. That includes the WebKitGTK image fallback for an <img>-only
clipboard. The direct native image read only runs when there is no text or HTML.
2026-10-09 12:34:54 -07:00
Shawn Olson 9aa9ca7d3e test: keep the grid title assertion independent of the image title policy 2026-10-09 09:12:25 -07:00
Shawn Olson 0b94d9ff32 feat: keep image hover titles ("title") and let users edit them
Images now keep their CommonMark title as hover text through load, edit and
save: `![alt](image.png "Hover text")`. Link titles are still dropped, as
before.

Parsing:
- helix_images no longer strips `title` from images (links unchanged).
- An image whose destination and title are valid CommonMark now uses
  markdown-it's own image rule instead of the legacy-link rule. Previously
  any space before a title sent the image down the legacy path, which can't
  handle escaped quotes (`"Say \"hi\""`) or `(title)` titles and mangled
  them into the URL.
- Genuinely legacy forms (spaces in the path, `%20"title"`) still go through
  the legacy rule, which now also keeps the image's title.

Saving: `serializeImageTitle()` writes ` "title"` with CommonMark backslash
escapes (`\"`, `\\`, and `\&` only before entity-like text). The parser
undoes these exactly, so titles survive any number of saves unchanged.

UI: a "T" button on the image toolbar opens a small dialog to set or clear
the hover text.

Tests: tests/image-titles.test.mjs covers exact round trips for tricky titles
(parentheses, &, entities, quotes, backslashes, markup), no growth over
repeated saves, the written form, titles from other apps, legacy
destinations, and links still dropping titles. The upstream assertion that
images render without `title=` is updated to the new contract.
2026-10-09 09:12:11 -07:00
Shawn Olson bd86fb8a4d feat: side-by-side image grids via a portable :::img-grid block
Implements #254. Several images can now sit side by side in a grid or masonry
layout with 2 to 4 columns. The syntax is plain Markdown images inside a
`:::` container, so other renderers show the images stacked:

    :::img-grid cols=3 masonry
    ![](a.jpg)
    ![Beach|size=small](b.jpg)
    :::

Parsing is a markdown-it block rule (`helix_img_grid` in markdownParser.ts),
not a text pre-pass:
- Grid syntax inside code blocks is never converted, because code blocks
  claim those lines first.
- A block converts only if every line is exactly one image (CommonMark image
  syntax, so titles, parentheses and <angle> destinations parse as for any
  image). Unknown options, out-of-range columns, captions, linked images,
  blank lines or a missing closer leave the whole block as ordinary Markdown.
- Each grid image is an ordinary inline image token, so it gets the same
  source resolution, `|size=` handling and title policy as other images.

Saving writes the grid as a normal block through joinMarkdownBlocks, with a
blank line before it. The images serialize with the same code as images in
paragraphs, one per line. The header is written in canonical form.

Editor (src/lib/editor/extensions/imageGrid.ts):
- `imageGrid` node (`image+`) whose renderHTML includes data-img-grid, so
  copied grids paste back as grids
- header controls: cycle columns, grid/masonry toggle, ungroup
- image toolbar: "Grid" groups the image with neighboring image-only blocks;
  arrow buttons move an image left or right inside its grid
- drag-and-drop reorder within and between grids; an emptied grid is removed
- `/image-grid` slash command: multi-file picker that inserts a grid, using
  the origin-guarded asset saving
- mobile: 3-4 column grids collapse to 2. Print: compact 2-column rows,
  controls hidden

Tests (tests/image-grid.test.mjs) cover byte-identical round trips, sizes,
code-fenced examples, no partial conversion, titles and angle-bracket
destinations, repeated saves, trailing closers, nesting, reloading next to
other blocks, renderHTML/parseHTML agreement, grouping and reorder targets.
2026-10-09 09:09:02 -07:00
Shawn Olson ec479ced8e fix: make editor context-menu paste match keyboard paste on Linux
On Linux, the context-menu Paste used its own navigator.clipboard.readText()
flow. On WebKitGTK it lost text copied from other applications. When a web
image copy also carried text, it pasted the URL instead of the bitmap. It
also skipped ProseMirror's paste handling, so things like callout paste and
the pasted-HTML style cleanup never ran.

The context menu now reads the clipboard with navigator.clipboard.read(),
classifies each format, and replays the result as a paste event on the
editor DOM. That runs the same path as Ctrl/Cmd+V: ProseMirror's paste
handler, every handlePaste prop (file, callout, link), transformPastedHTML,
the clipboard parsers and TipTap's paste rules.

Only real attachments (image/*, application/pdf) become files. Other text
formats such as text/uri-list stay text, and browser/OS metadata formats are
ignored, so a copied link no longer becomes a pasted-0.uri-list attachment.
A link offered only as a URI list pastes as its first URI.

When the web clipboard is unavailable (WebKitGTK bug #218519), Paste falls
back to native arboard reads: the image first, then text through the new
read_clipboard_text command. The text is replayed through the same paste
path. Every await re-checks the editor origin, as upstream's paste code does.

The classification lives in src/lib/utils/clipboard-paste.ts and has
regression tests.

Closes #259
2026-10-09 08:53:42 -07:00
Yuri Karamian add45ad9dd fix: clear the old caret when switching notes
Switching quickly between notes in the rich editor could leave a frozen
caret from the previous note next to the real one. The outgoing native
selection is now cleared before the note content is replaced, including
for very large notes. Reproduced on Linux and gone with the change.

Refs #281
2026-10-09 09:28:27 +02:00
Yuri Karamian 076234974a fix: parse long link destinations in linear time, and polish small mobile and history details
Strip a legacy link title with a single scan instead of a regular
expression that is quadratic on a long run of spaces. Widen the done
checkbox's tap area on phones without changing its look, and say "1
unchanged line" in Version History when only one line is folded.
2026-10-08 01:04:18 +02:00
Yuri Karamian 6d9b29343f perf: build very large notes in slices so opening them does not freeze
For notes over 100,000 characters, show the first screen at once and build
the rest of the document in growing slices. Until it is complete the
document cannot be edited: a user action, saving, or switching modes
finishes the build first, and switching notes cancels it. Smaller notes load
as before.
2026-10-08 00:34:07 +02:00
Yuri Karamian 504461bfe7 fix: Overdue tasks exclude completed ones
The Overdue filter now lists only tasks that are late and still open, even
with Hide done off, and a completed task's due date no longer uses the
overdue colour. Today and the calendar are unchanged.
2026-10-08 00:34:07 +02:00
Yuri Karamian 4aa72e4c76 fix: keep list blocks, folded callouts and opened notes intact; speed up saving
Keep a second paragraph, code block or quote inside its list item when a
note is saved, indented under the item. Import attachment links that follow
a stray backtick and images indented by four spaces. Keep a folded callout
that was revealed from the Outline open while it is edited.

Stop treating the editor's first normalization of a freshly loaded note as
an edit, so opening a note no longer saves or reformats it. Join serialized
blocks in linear time, so autosaving a large note no longer freezes the page.
2026-10-07 23:42:03 +02:00
Yuri Karamian ea4a5eae26 fix: keep dollar signs, aligned images, attachment names and code spaces intact
Write a literal $ as typed unless plain dollars could create, remove or
re-pair an inline formula. Save portable image paths when a centered,
aligned or styled block is written. Percent-encode a newly stored
attachment's path before it is inserted, and keep a Windows drive letter
literal in paths outside the vault. Keep whitespace-only code spans in
simple list items, task items and table cells.

Compare Version History in bounded time with a synchronous line diff and
an edit limit. Beyond the limit, shared lines at the start and end stay
unchanged and the rest is shown as removed and added.
2026-10-07 22:25:16 +02:00
Yuri Karamian 8d3338ef89 fix: keep callouts, numbered lists, images and blank lines intact on save
Render the first soft break of a callout's opening paragraph as a newline
so the title and body split correctly again. Only turn [ ] and [x] markers
into task items in bullet lists, not numbered ones. Join serialized blocks
in markdown.ts so an empty paragraph before an image is written as a blank
line, and lists no longer gain an extra blank line, without collapsing blank
lines inside code blocks.

In Version History, compare the current note and snapshots after ignoring
leading and trailing blank lines, so unchanged last lines and a blank line
after the frontmatter no longer show as changes.
2026-10-07 22:12:20 +02:00
Yuri Karamian b2c306d69f fix: scan vaults whose own folder name starts with a dot
A vault kept in a dot-named folder such as ~/.notes was treated as hidden
by seven vault walkers, so All Notes, note titles for links, Graph View,
Tasks and rename link updates found no notes. Exempt the vault root from
the hidden-folder check, as the tag scan and search already do. Hidden
subfolders, .helixnotes and the existing hidden-name rules are unchanged.
2026-10-07 21:03:49 +02:00
Yuri KaramianandClaude Sonnet 5.5 ac2c0aef94 fix: read full frontmatter for note metadata and speed up title and graph scans
Note lists, mobile listings and tag counts read a fixed 1-2 KB prefix, so notes
with longer frontmatter lost their title and tags. Read the complete frontmatter
instead, stopping after the closing delimiter (256 KB cap if unterminated).

Scan note titles in parallel from the frontmatter only, and build graph nodes and
links in one parallel read per note without keeping note bodies in memory. The
Ctrl+K link dialog reuses the loaded wiki-link title cache.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-07 20:39:21 +02:00
Yuri Karamian f07241dd3a fix: harden note saves, attachment imports, and Markdown handling
- Fix new-note save conflicts and scoped external Markdown access
- Preserve attachment URIs and links across saves, imports, and moves
- Ignore unused and overridden attachment reference definitions
- Improve Markdown parsing, outline handling, and editor performance
- Optimize graph, task filtering, tags, WebDAV, and Obsidian imports
- Fix note-list metadata wrapping and add regression coverage
- Document saving and close-to-tray behavior
2026-10-06 06:09:14 +02:00
Yuri Karamian 14b2a3e8a2 feat: improve history review and note workflows
- Add draft-aware diffs, rendered snapshots and restore confirmation
- Show saved YAML frontmatter in Note Info
- Add Today/Overdue task filters and Ubuntu font families
- Honor timestamp preferences
- Improve vault labels and full-path tooltips

Refs: #234, #244, #257, #262, #265, #268
2026-10-04 00:46:43 +02:00
Yuri Karamian 505ed2b890 docs: update README downloads to v1.3.6 2026-09-30 22:13:48 +02:00
Yuri Karamian b03d7fd12c fix: simplify update download label 2026-09-30 22:09:20 +02:00
72 changed files with 9388 additions and 910 deletions

No files matched your search

+33 -13
View File
@@ -1,7 +1,7 @@
# HelixNotes
[![License: AGPL-3.0](https://img.shields.io/badge/License-AGPL--3.0-blue.svg)](https://gitlab.com/ArkHost/HelixNotes/-/blob/main/LICENSE)
[![Latest Release](https://img.shields.io/badge/release-v1.3.5-green)](https://gitlab.com/ArkHost/HelixNotes/-/releases/v1.3.5)
[![Latest Release](https://img.shields.io/badge/release-v1.3.6-green)](https://gitlab.com/ArkHost/HelixNotes/-/releases/v1.3.6)
[![Website](https://img.shields.io/badge/web-helixnotes.com-purple)](https://helixnotes.com)
[![Platform](https://img.shields.io/badge/platform-Linux%20%7C%20Windows%20%7C%20macOS%20%7C%20Android-lightgrey)]()
@@ -10,7 +10,7 @@ A local markdown note-taking app built with Tauri, SvelteKit, and Rust.
Your notes are stored as standard Markdown files on your local filesystem.
No cloud, no lock-in.
## Download (v1.3.5)
## Download (v1.3.6)
### Linux
@@ -18,7 +18,7 @@ No cloud, no lock-in.
The AppImage works only on Fedora 43+, Arch Linux, and openSUSE Tumbleweed (x86_64).
[Download AppImage](https://download.helixnotes.com/releases/v1.3.5/HelixNotes_1.3.5_amd64.AppImage)
[Download AppImage](https://download.helixnotes.com/releases/v1.3.6/HelixNotes_1.3.6_amd64.AppImage)
#### Distro-specific packages
@@ -108,18 +108,18 @@ sudo eopkg it helixnotes
#### Manual package downloads
- [.deb](https://download.helixnotes.com/releases/v1.3.5/HelixNotes_1.3.5_amd64.deb) (Ubuntu 22.04+)
- [.rpm](https://download.helixnotes.com/releases/v1.3.5/HelixNotes-1.3.5-1.x86_64.rpm)
- [.deb](https://download.helixnotes.com/releases/v1.3.6/HelixNotes_1.3.6_amd64.deb) (Ubuntu 22.04+)
- [.rpm](https://download.helixnotes.com/releases/v1.3.6/HelixNotes-1.3.6-1.x86_64.rpm)
### Windows
[Download Installer](https://download.helixnotes.com/releases/v1.3.5/HelixNotes_1.3.5_x64-setup.exe) (Windows 10/11)
[Download Installer](https://download.helixnotes.com/releases/v1.3.6/HelixNotes_1.3.6_x64-setup.exe) (Windows 10/11)
### macOS
[Download .dmg (Apple Silicon)](https://download.helixnotes.com/releases/v1.3.5/HelixNotes_1.3.5_aarch64.dmg) (M-series Macs)
[Download .dmg (Apple Silicon)](https://download.helixnotes.com/releases/v1.3.6/HelixNotes_1.3.6_aarch64.dmg) (M-series Macs)
[Download .dmg (Intel)](https://download.helixnotes.com/releases/v1.3.5/HelixNotes_1.3.5_x64.dmg)
[Download .dmg (Intel)](https://download.helixnotes.com/releases/v1.3.6/HelixNotes_1.3.6_x64.dmg)
The Apple Silicon build is signed and notarized by Apple. The Intel build may still trigger a Gatekeeper warning. If macOS reports that the Intel build is damaged, run this once after installing or updating it:
@@ -129,7 +129,7 @@ xattr -cr /Applications/HelixNotes.app
### Android
[Download APK](https://download.helixnotes.com/releases/v1.3.5/HelixNotes_1.3.5_android.apk)
[Download APK](https://download.helixnotes.com/releases/v1.3.6/HelixNotes_1.3.6_android.apk)
---
@@ -138,24 +138,44 @@ All releases: [gitlab.com/ArkHost/HelixNotes/-/releases](https://gitlab.com/ArkH
## Features
- Markdown editor with toolbar, slash commands, source mode, code highlighting
- **Tasks view**: aggregate `- [ ]` checklists from across all notes, set priority and due dates, work in a list or a calendar (drag a task to reschedule)
- **Tasks view**: aggregate `- [ ]` checklists from across all notes, set priority and due dates, filter Today or Overdue tasks, work in a list or a calendar (drag a task to reschedule)
- `[[Wiki-links]]` and graph view
- Full-text search (Tantivy), CJK-aware for Chinese, Japanese, and Korean
- Outline panel, daily notes with calendar view, tags with autocomplete, drag-and-drop
- Live KaTeX math editor (`/math`, `/imath`) with modal preview, double-click to edit
- Mermaid diagrams (opt-in render, copy as PNG, save as PNG/SVG)
- Encrypted secret blocks (`/secret`) stored as portable `helix-secret` markdown fences
- Insert date/time (`/date`, `/time`, `/now`), color swatches (`/color`), configurable week start
- Insert date/time (`/date`, `/time`, `/now`) with 12-hour or 24-hour timestamps, color swatches (`/color`), configurable week start
- Manual notebook sorting (drag to reorder above, into, or below)
- External `.md` viewer mode with import-to-vault flow
- PDF preview, Obsidian import, "Show in File Manager"
- AI writing tools (Ollama / OpenAI-compatible / Anthropic / OpenAI)
- **Optional WebDAV sync** to your own server (Nextcloud, ownCloud, a NAS): manual or automatic, with keep-both conflict copies
- Version history with diffs, automatic backups
- Version history with main-area diffs and rendered read-only snapshots, automatic backups
- Multi-window, file associations, focus mode, view mode
- Themes (light, dark, and 14 palettes), accent colors, fonts, 80-200% interface scale
- Themes (light, dark, and 14 palettes), accent colors, fonts including bundled Ubuntu and Ubuntu Mono, 80-200% interface scale
- Local plain-text files, no company cloud
### Version history
Changes compares the selected snapshot with the current editor contents, including unsaved edits. Added and removed lines show what restoring that version would change; long unchanged sections can be expanded.
Snapshot displays the stored note read-only, with View source available for its Markdown body. Metadata is separate and is not restored. Snapshot links are inactive, secrets stay locked, and diagrams render only when requested.
Restore replaces the current note's body and asks for confirmation if it would replace an unsaved draft. Inspecting versions does not replace the live editor contents or reset their undo history.
### Note Info
Open **Note info** from the editor toolbar. Expand **Frontmatter** to inspect the original YAML header, including custom keys, without editing it. This shows the last loaded or saved metadata; unsaved changes are not included, and the display updates after saving.
### Saving notes
Notes autosave after three seconds without edits. During continuous typing, a save is queued at least every ten seconds. Ctrl+S (Cmd+S on macOS), switching notes, and closing an editor flush pending changes immediately. A failed save leaves the note marked **Unsaved** and blocks leaving that editor.
Note writes run off the UI thread and replace the file atomically. Unchanged notes are not rewritten. Ordinary saves update only the affected note in the list, wiki-link cache, and Tasks view; search-index updates are batched in the background. External file changes are still picked up by the watcher.
With **Close to tray** enabled and a tray icon available, closing the main window hides it after saving. With the setting off, closing the main window closes the application. The close-to-tray setting takes effect immediately; enabling the tray icon itself requires a restart. Explicit **Quit** exits rather than hiding to the tray.
Full documentation: [helixnotes.com/docs](https://helixnotes.com/docs.html)
## Tech Stack
+2
View File
@@ -69,6 +69,8 @@
"@tiptap/starter-kit": "^3.19.0",
"@types/katex": "^0.16.8",
"@types/markdown-it": "^14.1.2",
"diff": "^9.0.0",
"dompurify": "^3.4.13",
"highlight.js": "^11.11.1",
"katex": "^0.16.28",
"lowlight": "^3.3.0",
+12
View File
@@ -110,6 +110,12 @@ importers:
'@types/markdown-it':
specifier: ^14.1.2
version: 14.1.2
diff:
specifier: ^9.0.0
version: 9.0.0
dompurify:
specifier: ^3.4.13
version: 3.4.13
highlight.js:
specifier: ^11.11.1
version: 11.11.1
@@ -1373,6 +1379,10 @@ packages:
devlop@1.1.0:
resolution: {integrity: sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==}
diff@9.0.0:
resolution: {integrity: sha512-svtcdpS8CgJyqAjEQIXdb3OjhFVVYjzGAPO8WGCmRbrml64SPw/jJD4GoE98aR7r25A0XcgrK3F02yw9R/vhQw==}
engines: {node: '>=0.3.1'}
dompurify@3.4.13:
resolution: {integrity: sha512-2vmYIoqjze2d+kakP8S/nS5shfsl587kzwEjcGlTdiksUVgFHnFCsLYDVj/JNqJVOQZGSYBTmuycv0PodwmnMQ==}
@@ -2946,6 +2956,8 @@ snapshots:
dependencies:
dequal: 2.0.3
diff@9.0.0: {}
dompurify@3.4.13:
optionalDependencies:
'@types/trusted-types': 2.0.7
+145 -1
View File
@@ -1,5 +1,38 @@
use std::path::Path;
use tauri::{AppHandle, Manager, Runtime};
use tauri_plugin_fs::FsExt;
/// Only configured user-selected vaults may provide an external note's vault-relative root.
pub fn external_note_root<R: Runtime>(
app: &AppHandle<R>,
note_path: &Path,
) -> Result<std::path::PathBuf, String> {
let note = std::fs::canonicalize(note_path).map_err(|error| error.to_string())?;
if let Some(state) = app.try_state::<crate::state::AppState>() {
let (active, mut roots) = {
let config = state.config.lock().map_err(|error| error.to_string())?;
(
config.active_vault.clone(),
config
.vaults
.iter()
.map(|vault| vault.path.clone())
.collect::<Vec<_>>(),
)
};
roots.sort_by_key(|root| std::cmp::Reverse(root.len()));
for root in active.into_iter().chain(roots) {
if let Ok(root) = std::fs::canonicalize(root) {
if note.starts_with(&root) {
return Ok(root);
}
}
}
}
note.parent()
.map(Path::to_path_buf)
.ok_or_else(|| "External note has no parent directory".into())
}
/// Grants asset-protocol access only to a vault selected by the user.
/// `.helixnotes` is added explicitly because Unix glob matching excludes hidden paths.
@@ -37,7 +70,23 @@ pub fn allow_external_note_assets<R: Runtime>(
.ok_or_else(|| "External note has no parent directory".to_string())?;
app.asset_protocol_scope()
.allow_directory(parent, true)
.map_err(|error| error.to_string())
.map_err(|error| error.to_string())?;
let root = external_note_root(app, &note)?;
let attachments = root.join(".helixnotes/attachments");
// Never follow a hidden attachment-directory symlink when extending either scope.
if std::fs::canonicalize(&attachments)
.is_ok_and(|canonical| canonical == attachments && canonical.is_dir())
{
app.asset_protocol_scope()
.allow_directory(&attachments, true)
.map_err(|error| error.to_string())?;
if let Some(scope) = app.try_fs_scope() {
scope
.allow_directory(&attachments, true)
.map_err(|error| error.to_string())?;
}
}
Ok(())
}
#[cfg(test)]
@@ -85,6 +134,101 @@ mod tests {
fs::remove_dir_all(root).unwrap();
}
#[test]
fn external_note_scopes_include_its_attachments_but_not_hidden_private_metadata() {
use tauri_plugin_fs::FsExt;
let root = tempfile::tempdir().unwrap();
let parent = root.path().join("external");
let attachments = parent.join(".helixnotes/attachments");
fs::create_dir_all(&attachments).unwrap();
fs::create_dir_all(parent.join(".helixnotes/history")).unwrap();
fs::create_dir_all(parent.join(".ssh")).unwrap();
let note = parent.join("Note.md");
let image = attachments.join("image.png");
let history = parent.join(".helixnotes/history/snapshot.md");
let private = parent.join(".ssh/private.bin");
for file in [&note, &image, &history, &private] {
fs::write(file, b"fixture").unwrap();
}
let app = tauri::test::mock_builder()
.plugin(tauri_plugin_fs::init())
.build(tauri::test::mock_context(tauri::test::noop_assets()))
.unwrap();
app.fs_scope().allow_file(&note).unwrap();
app.fs_scope().allow_directory(&parent, true).unwrap();
let before_private = [
app.fs_scope().is_allowed(&history),
app.fs_scope().is_allowed(&private),
];
allow_external_note_assets(app.handle(), &note).unwrap();
assert!(
app.fs_scope().is_allowed(&image),
"import must be able to read its managed attachments"
);
assert!(
app.asset_protocol_scope().is_allowed(&image),
"the viewer must display managed attachments"
);
for (file, previous) in [&history, &private].into_iter().zip(before_private) {
assert_eq!(
app.fs_scope().is_allowed(file),
previous,
"attachment grants must not widen private metadata access"
);
assert!(!app.asset_protocol_scope().is_allowed(file));
}
}
#[test]
fn external_notebook_uses_its_registered_vault_root_for_attachment_scopes() {
use tauri_plugin_fs::FsExt;
let root = tempfile::tempdir().unwrap();
let vault = root.path().join("source");
fs::create_dir_all(vault.join("Notebook")).unwrap();
fs::create_dir_all(vault.join(".helixnotes/attachments")).unwrap();
let note = vault.join("Notebook/Note.md");
let image = vault.join(".helixnotes/attachments/image.png");
fs::write(&note, b"Note").unwrap();
fs::write(&image, b"Image").unwrap();
let app = tauri::test::mock_builder()
.plugin(tauri_plugin_fs::init())
.manage(crate::state::AppState::new(crate::types::AppConfig {
vaults: vec![crate::types::VaultConfig {
path: vault.to_string_lossy().into_owned(),
..Default::default()
}],
..Default::default()
}))
.build(tauri::test::mock_context(tauri::test::noop_assets()))
.unwrap();
allow_external_note_assets(app.handle(), &note).unwrap();
assert!(app.asset_protocol_scope().is_allowed(&image));
assert!(app.fs_scope().is_allowed(&image));
}
#[cfg(unix)]
#[test]
fn external_attachment_scope_does_not_follow_hidden_directory_symlinks() {
use tauri_plugin_fs::FsExt;
let root = tempfile::tempdir().unwrap();
let parent = root.path().join("external");
let private = root.path().join("private");
fs::create_dir_all(parent.join(".helixnotes")).unwrap();
fs::create_dir(&private).unwrap();
let note = parent.join("Note.md");
let file = private.join("private.bin");
fs::write(&note, b"Note").unwrap();
fs::write(&file, b"private fixture").unwrap();
std::os::unix::fs::symlink(&private, parent.join(".helixnotes/attachments")).unwrap();
let app = tauri::test::mock_builder()
.plugin(tauri_plugin_fs::init())
.build(tauri::test::mock_context(tauri::test::noop_assets()))
.unwrap();
allow_external_note_assets(app.handle(), &note).unwrap();
assert!(!app.fs_scope().is_allowed(&file));
assert!(!app.asset_protocol_scope().is_allowed(&file));
}
#[test]
fn external_note_scope_allows_relative_assets_only_below_its_directory() {
let root = test_directory("external-note-assets");
+816 -133
View File
File diff suppressed because it is too large. Load diff
+2
View File
@@ -187,6 +187,7 @@ pub fn run() {
commands::get_notes,
commands::read_note,
commands::save_note,
commands::import_note,
commands::create_note,
commands::duplicate_note,
commands::create_daily_note,
@@ -211,6 +212,7 @@ pub fn run() {
commands::load_vault_state,
commands::save_vault_state,
commands::copy_text_to_clipboard,
commands::read_clipboard_contents,
commands::read_clipboard_image,
commands::copy_image_to_clipboard,
commands::save_image,
+460 -24
View File
@@ -60,6 +60,65 @@ fn manifest_path(vault: &str) -> PathBuf {
helixnotes_dir(vault).join("sync_state.json")
}
/// A local filesystem failure, named as such with the operation and the file. The bare OS error
/// ("Operation not permitted (os error 1)") names no file and reads like a server problem.
fn local_error(operation: &str, path: impl AsRef<Path>, error: impl std::fmt::Display) -> String {
format!(
"Local file error: cannot {operation} \"{}\": {error}",
path.as_ref().display()
)
}
/// A request that never got an answer from the server (connection, TLS, timeout).
fn webdav_error(operation: &str, relpath: &str, error: impl std::fmt::Display) -> String {
format!("WebDAV error: cannot {operation} \"{relpath}\": {error}")
}
/// The only relative paths sync will touch: plain file and folder names separated by `/`.
/// A remote listing is untrusted input, and so is a stored manifest: a `..` segment, an absolute
/// or drive-prefixed path, or a backslash must never be joined to the vault folder.
fn validate_relpath(rel: &str) -> Result<(), String> {
validate_relpath_for(rel, cfg!(windows))
}
fn validate_relpath_for(rel: &str, windows: bool) -> Result<(), String> {
let reject = |reason: &str| {
Err(format!(
"Sync stopped: \"{}\" is not a valid file path ({reason}). No file was written for it.",
rel.escape_debug()
))
};
if rel.is_empty() {
return reject("empty path");
}
for segment in rel.split('/') {
if segment.is_empty() {
return reject("empty path segment");
}
if segment == "." || segment == ".." {
return reject("relative path segment");
}
if segment.contains('\\') {
return reject("backslash");
}
if segment.chars().any(char::is_control) {
return reject("control character");
}
// Windows reads `D:x` as a drive path and `x:y` as a file stream, and drops trailing dots
// and spaces, which can turn a segment into `..`.
if windows && (segment.contains(':') || segment.ends_with('.') || segment.ends_with(' ')) {
return reject("not a valid Windows file name");
}
}
Ok(())
}
/// The local file for a synced path. Every join of a sync key to the vault goes through here.
fn local_path(vault: &Path, relpath: &str) -> Result<PathBuf, String> {
validate_relpath(relpath)?;
Ok(vault.join(relpath))
}
fn load_manifest(vault: &str) -> Result<SyncManifest, String> {
match fs::read(manifest_path(vault)) {
Ok(bytes) => serde_json::from_slice(&bytes)
@@ -72,8 +131,10 @@ fn load_manifest(vault: &str) -> Result<SyncManifest, String> {
fn save_manifest(vault: &str, m: &SyncManifest) -> Result<(), String> {
let bytes = serde_json::to_vec(m).map_err(|e| e.to_string())?;
let target = manifest_path(vault);
fs::create_dir_all(target.parent().unwrap()).map_err(|e| e.to_string())?;
let folder = target.parent().unwrap();
fs::create_dir_all(folder).map_err(|e| local_error("create folder", folder, e))?;
crate::vault::operations::replace_file_atomically(&target, &bytes)
.map_err(|e| local_error("save sync state", &target, e))
}
fn baseline_for_endpoint(manifest: SyncManifest, identity: &str) -> SyncManifest {
@@ -149,12 +210,12 @@ fn write_local(
bytes: &[u8],
changes: &mut HashMap<PathBuf, &'static str>,
) -> Result<(), String> {
let target = vault.join(relpath);
let target = local_path(vault, relpath)?;
if fs::read(&target).is_ok_and(|current| current == bytes) {
return Ok(());
}
if let Some(parent) = target.parent() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
fs::create_dir_all(parent).map_err(|e| local_error("create folder for", relpath, e))?;
}
if matches!(
relpath,
@@ -166,7 +227,8 @@ fn write_local(
content,
)?;
}
crate::vault::operations::replace_file_atomically(&target, bytes)?;
crate::vault::operations::replace_file_atomically(&target, bytes)
.map_err(|e| local_error("write", relpath, e))?;
changes.insert(target, "modify");
Ok(())
}
@@ -175,10 +237,10 @@ fn require_unchanged_local(path: &Path, expected_hash: Option<&str>) -> Result<(
let actual = match fs::File::open(path) {
Ok(mut file) => Some(
sha256_reader(&mut file, &mut [0; HASH_BUFFER_SIZE])
.map_err(|error| error.to_string())?,
.map_err(|error| local_error("read", path, error))?,
),
Err(error) if error.kind() == io::ErrorKind::NotFound => None,
Err(error) => return Err(error.to_string()),
Err(error) => return Err(local_error("read", path, error)),
};
if actual.as_deref() != expected_hash {
return Err(format!(
@@ -198,7 +260,7 @@ fn write_local_if_unchanged(
changes: &mut HashMap<PathBuf, &'static str>,
) -> Result<(), String> {
let _guard = write_guard.blocking_lock();
require_unchanged_local(&vault.join(relpath), expected_hash)?;
require_unchanged_local(&local_path(vault, relpath)?, expected_hash)?;
write_local(vault, relpath, bytes, changes)
}
@@ -211,6 +273,14 @@ struct LocalFile {
/// `.helixnotes/attachments/`, plus notebook icon and tag style mappings. Applied to BOTH local
/// and remote so pointing at a folder with unrelated files never imports them.
fn is_synced_relpath(rel: &str) -> bool {
// On a case-insensitive disk `.HelixNotes/history/x.md` lands in the app's own folder, so any
// other spelling of it is never synced, whatever lies below.
let first = rel.split('/').next().unwrap_or_default();
if first != ".helixnotes"
&& (first.to_lowercase() == ".helixnotes" || first.to_uppercase() == ".HELIXNOTES")
{
return false;
}
if rel.starts_with(".helixnotes/") {
rel.starts_with(".helixnotes/attachments/")
|| rel == ".helixnotes/notebook_icons.json"
@@ -385,7 +455,9 @@ impl WebdavClient {
if !expected_etag.is_empty() {
request = request.header("If-Match", expected_etag);
}
let resp = request.send().map_err(|e| e.to_string())?;
let resp = request
.send()
.map_err(|e| webdav_error("download", relpath, e))?;
if resp.status().as_u16() == 412 {
return Err(format!(
"Remote file {relpath} changed during sync; retry sync"
@@ -409,7 +481,7 @@ impl WebdavClient {
"Remote file {relpath} changed during sync; retry sync"
));
}
let bytes = resp.bytes().map_err(|e| e.to_string())?.to_vec();
let bytes: Vec<u8> = resp.bytes().map_err(|e| e.to_string())?.into();
if !expected_etag.is_empty() && self.etag_for(relpath)? != expected_etag {
return Err(format!(
"Remote file {relpath} changed during sync; retry sync"
@@ -433,7 +505,9 @@ impl WebdavClient {
}
None => request.header("If-None-Match", "*"),
};
let resp = request.send().map_err(|e| e.to_string())?;
let resp = request
.send()
.map_err(|e| webdav_error("upload", relpath, e))?;
if resp.status().as_u16() == 412 {
return Err(format!(
"Remote file {relpath} changed during sync; retry sync"
@@ -460,7 +534,7 @@ impl WebdavClient {
.basic_auth(&self.user, Some(&self.pass))
.header("If-Match", etag)
.send()
.map_err(|e| e.to_string())?;
.map_err(|e| webdav_error("delete", relpath, e))?;
if resp.status().as_u16() == 412 || resp.status().as_u16() == 404 {
return Err(format!(
"Remote file {relpath} changed during sync; retry sync"
@@ -483,7 +557,7 @@ impl WebdavClient {
.request(method, self.url_for(relpath))
.basic_auth(&self.user, Some(&self.pass))
.send()
.map_err(|e| e.to_string())?;
.map_err(|e| webdav_error("create folder", relpath, e))?;
let s = resp.status().as_u16();
if resp.status().is_success() || s == 405 || s == 301 || s == 403 {
Ok(())
@@ -565,8 +639,11 @@ fn href_to_relpath(href: &str, base_path: &str) -> Result<Option<String>, String
.map_err(|e| format!("bad percent-encoding in WebDAV base path '{base_path}': {e}"))?
.into_owned();
let rest = match path.strip_prefix(&base) {
Some(r) => r.trim_start_matches('/'),
None => {
// `/dav-other/x` is not inside `/dav`: the folder must end at a path boundary.
Some(r) if r.is_empty() || r.starts_with('/') || base.is_empty() => {
r.trim_start_matches('/')
}
_ => {
return Err(format!(
"remote path '{path}' is not under the configured WebDAV folder '{base}'. \
Aborting before deleting anything locally - please report this URL."
@@ -576,6 +653,7 @@ fn href_to_relpath(href: &str, base_path: &str) -> Result<Option<String>, String
if rest.is_empty() {
return Ok(None); // the base collection itself
}
validate_relpath(rest)?;
Ok(Some(rest.to_string()))
}
@@ -624,10 +702,34 @@ fn parse_multistatus(xml: &str, base_path: &str) -> Result<Vec<RemoteEntry>, Str
}
Ok(Event::Text(e)) => {
if cap != Cap::None {
if let Ok(decoded) = e.decode() {
if let Ok(text) = quick_xml::escape::unescape(&decoded) {
buf.push_str(&text);
}
let text = e
.decode()
.map_err(|e| format!("XML text decode error: {e}"))?;
buf.push_str(&text);
}
}
Ok(Event::CData(e)) => {
if cap != Cap::None {
let text = e
.decode()
.map_err(|e| format!("XML CDATA decode error: {e}"))?;
buf.push_str(&text);
}
}
Ok(Event::GeneralRef(e)) => {
if cap != Cap::None {
if let Some(ch) = e
.resolve_char_ref()
.map_err(|e| format!("XML reference error: {e}"))?
{
buf.push(ch);
} else {
let name = e
.decode()
.map_err(|e| format!("XML reference decode error: {e}"))?;
let text = quick_xml::escape::resolve_xml_entity(&name)
.ok_or_else(|| format!("Unknown XML entity: &{name};"))?;
buf.push_str(text);
}
}
}
@@ -768,7 +870,7 @@ fn apply_changes(
let ckey = conflict_relpath(key, &stamp);
let lbytes = {
let _guard = write_guard.blocking_lock();
require_unchanged_local(&vault_path.join(&ckey), None)?;
require_unchanged_local(&local_path(vault_path, &ckey)?, None)?;
let lbytes = read_scanned_local(lf)?;
write_local(vault_path, &ckey, &lbytes, changes)?;
write_local(vault_path, key, &bytes, changes)?;
@@ -803,7 +905,7 @@ fn apply_changes(
Some(_) => {
let _guard = write_guard.blocking_lock();
require_unchanged_local(&lf.path, Some(&lf.hash))?;
fs::remove_file(&lf.path).map_err(|e| e.to_string())?;
fs::remove_file(&lf.path).map_err(|e| local_error("delete", &lf.path, e))?;
changes.insert(lf.path.clone(), "remove");
sum.deleted_local += 1;
}
@@ -1319,19 +1421,147 @@ mod tests {
)
.unwrap();
fs::write(vault.path().join("blocked"), b"not a directory").unwrap();
assert!(write_local(
let error = write_local(
vault.path(),
"blocked/note.md",
b"cannot write",
&mut changes
&mut changes,
)
.is_err());
.unwrap_err();
// The failure names itself as local, the operation and the file (issue #249).
assert!(
error.starts_with("Local file error: cannot create folder for \"blocked/note.md\": "),
"{error}"
);
assert_eq!(changes, HashMap::from([(path.clone(), "modify")]));
let event = changed_entry(path, "modify", vault.path());
assert_eq!(event.note.unwrap().preview, "remote content");
}
#[test]
fn remote_paths_cannot_leave_the_vault() {
// Literal and percent-encoded traversal, as a hostile server could list it.
for href in [
"/dav/.helixnotes/attachments/..%2F..%2F..%2Fescaped.md",
"/dav/.helixnotes/attachments/../../../escaped.md",
"/dav/..%2Fescaped.md",
"/dav/Notes/./x.md",
"/dav/Notes//x.md",
"/dav/Notes%5C..%5Cx.md",
"/dav/Notes/x%00.md",
] {
let error = href_to_relpath(href, "/dav").unwrap_err();
assert!(error.starts_with("Sync stopped: "), "{href}: {error}");
}
// The folder must end at a path boundary.
assert!(href_to_relpath("/dav-other/x.md", "/dav").is_err());
assert_eq!(
href_to_relpath("/dav/Notes/x.md", "/dav")
.unwrap()
.as_deref(),
Some("Notes/x.md")
);
assert_eq!(
href_to_relpath("/x.md", "/").unwrap().as_deref(),
Some("x.md")
);
// Ordinary names stay valid, including ones only some platforms dislike.
for rel in [
"Notes/a b.md",
"Notes/é ü.md",
"Notes/A: B.md",
"Notes/v1.2.md",
".helixnotes/attachments/x.png",
] {
assert!(validate_relpath_for(rel, false).is_ok(), "{rel}");
}
// Windows: drive prefixes, streams and names it would silently rewrite.
for rel in [
"D:x.md",
"D: draft.md",
"Notes/x.md:stream",
"Notes/.. /x.md",
"Notes/x.md.",
] {
assert!(validate_relpath_for(rel, true).is_err(), "{rel}");
}
assert!(validate_relpath_for("Notes/a b.md", true).is_ok());
// Even a key that slipped past the listing never reaches the disk.
let vault = tempfile::tempdir().unwrap();
let root = vault.path().join("vault");
fs::create_dir_all(&root).unwrap();
let mut changes = HashMap::new();
for rel in [
"../escaped.md",
".helixnotes/attachments/../../../escaped.md",
"/abs.md",
] {
assert!(
write_local(&root, rel, b"x", &mut changes).is_err(),
"{rel}"
);
assert!(write_local_if_unchanged(
&tokio::sync::Mutex::new(()),
&root,
rel,
b"x",
None,
&mut changes
)
.is_err());
}
assert!(changes.is_empty());
assert!(!vault.path().join("escaped.md").exists());
}
#[test]
fn a_listing_with_a_path_outside_the_folder_stops_the_sync() {
let listing = |href: &str| {
format!(
r#"<?xml version="1.0"?><d:multistatus xmlns:d="DAV:"><d:response><d:href>/dav/ok.md</d:href><d:propstat><d:prop><d:getetag>"a"</d:getetag></d:prop></d:propstat></d:response><d:response><d:href>{href}</d:href><d:propstat><d:prop><d:getetag>"b"</d:getetag></d:prop></d:propstat></d:response></d:multistatus>"#
)
};
// One hostile entry fails the whole listing, so nothing from it is acted on.
for href in [
"/dav/.helixnotes/attachments/..%2F..%2F..%2FLibrary%2Fx.plist",
"/dav/../x.md",
"/elsewhere/x.md",
] {
assert!(parse_multistatus(&listing(href), "/dav").is_err(), "{href}");
}
let entries = parse_multistatus(&listing("/dav/Notes/fine.md"), "/dav").unwrap();
assert_eq!(entries.len(), 2);
}
#[test]
fn other_spellings_of_the_internal_folder_are_never_synced() {
for rel in [
".HelixNotes/history/id/zz-00.md",
".HELIXNOTES/trash/x.md",
".helixNotes/attachments/x.png",
".helixnote\u{17f}/history/x.md",
] {
assert!(!is_synced_relpath(rel), "{rel}");
}
assert!(is_synced_relpath(".helixnotes/attachments/x.png"));
assert!(!is_synced_relpath(".helixnotes/history/id/x.md"));
assert!(is_synced_relpath("Notes/.HelixNotes.md"));
assert!(is_synced_relpath("Sub/.HelixNotes/x.md"));
}
fn webdav_test_client(
responses: Vec<(&'static str, &'static str)>,
) -> (WebdavClient, std::thread::JoinHandle<Vec<String>>) {
webdav_test_client_bytes(
responses
.into_iter()
.map(|(status, body)| (status, body.as_bytes().to_vec()))
.collect(),
)
}
fn webdav_test_client_bytes(
responses: Vec<(&'static str, Vec<u8>)>,
) -> (WebdavClient, std::thread::JoinHandle<Vec<String>>) {
use std::io::Write;
use std::net::TcpListener;
@@ -1370,10 +1600,11 @@ mod tests {
}
requests.push(headers);
let response = format!(
"HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
"HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);
stream.write_all(response.as_bytes()).unwrap();
stream.write_all(&body).unwrap();
}
requests
});
@@ -1561,4 +1792,209 @@ mod tests {
.contains("no strong ETag"));
assert!(requests.join().unwrap().is_empty());
}
#[test]
fn multistatus_preserves_named_and_numeric_references() {
let xml = r#"<d:multistatus xmlns:d="DAV:"><d:response>
<d:href>/vault/a&amp;b&apos;&lt;&gt;&quot;-&#233;-&#x96EA;.md</d:href>
<d:propstat><d:prop>
<d:getetag>&quot;v&amp;&apos;&lt;&gt;&#x96EA;&#233;&quot;</d:getetag>
</d:prop></d:propstat>
</d:response></d:multistatus>"#;
let entries = parse_multistatus(xml, "/vault").unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].relpath, "a&b'<>\"-é-雪.md");
assert_eq!(entries[0].etag, "\"v&'<>雪é\"");
assert!(!entries[0].is_dir);
}
#[test]
fn multistatus_preserves_mixed_cdata_and_strong_etag_policy() {
let xml = r#"<multistatus xmlns="DAV:">
<response>
<href>/vault/<![CDATA[café&amp;]]>&#x96EA;.md</href>
<propstat><prop><getetag>&#34;<![CDATA[v&amp;]]>&#x96EA;&#x22;</getetag></prop></propstat>
</response>
<response>
<href><![CDATA[/vault/folder/]]></href>
<propstat><prop><getetag><![CDATA[W/"weak"]]></getetag>
<resourcetype><collection/></resourcetype></prop></propstat>
</response>
</multistatus>"#;
let entries = parse_multistatus(xml, "/vault").unwrap();
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].relpath, "café&amp;雪.md");
assert_eq!(entries[0].etag, "\"v&amp;雪\"");
assert_eq!(entries[1].relpath, "folder");
assert!(entries[1].etag.is_empty());
assert!(entries[1].is_dir);
}
#[test]
fn multistatus_rejects_unknown_and_malformed_captured_references() {
for reference in [
"&unknown;",
"&;",
"&#;",
"&#xZZ;",
"&#xD800;",
"&#x110000;",
"&#0;",
"&amp",
"&#123",
] {
for field in ["href", "getetag"] {
let (href, etag) = if field == "href" {
(format!("/vault/bad{reference}.md"), "\"ok\"".to_string())
} else {
("/vault/bad.md".to_string(), format!("\"bad{reference}\""))
};
let xml = format!(
"<multistatus><response><href>/vault/good.md</href>\
<getetag>\"good\"</getetag></response><response>\
<href>{href}</href><getetag>{etag}</getetag>\
</response></multistatus>"
);
assert!(
parse_multistatus(&xml, "/vault").is_err(),
"accepted {reference:?} in {field}"
);
}
}
}
#[test]
fn multistatus_does_not_expand_custom_or_external_dtd_entities() {
for declaration in [
r#"<!ENTITY custom "expanded">"#,
r#"<!ENTITY custom SYSTEM "file:///nonexistent-helixnotes-entity">"#,
] {
let xml = format!(
"<!DOCTYPE multistatus [{declaration}]><multistatus><response>\
<href>/vault/&custom;.md</href><getetag>\"etag\"</getetag>\
</response></multistatus>"
);
assert!(parse_multistatus(&xml, "/vault")
.err()
.unwrap()
.contains("Unknown XML entity"));
}
}
#[test]
fn get_preserves_binary_download_and_rechecks_strong_etag() {
let bytes: Vec<u8> = (0..HASH_BUFFER_SIZE * 2 + 137)
.map(|i| (i % 256) as u8)
.collect();
let listing = br#"<d:multistatus xmlns:d="DAV:"><d:response>
<d:href>/vault/file.bin</d:href>
<d:propstat><d:prop><d:getetag>&quot;stable&quot;</d:getetag></d:prop></d:propstat>
</d:response></d:multistatus>"#;
let (client, requests) = webdav_test_client_bytes(vec![
("200 OK\r\nETag: \"stable\"", bytes.clone()),
("207 Multi-Status", listing.to_vec()),
]);
assert_eq!(client.get("file.bin", "\"stable\"").unwrap(), bytes);
let requests = requests.join().unwrap();
assert_eq!(requests.len(), 2);
assert!(requests[0].starts_with("GET /vault/file.bin "));
assert!(requests[0]
.to_ascii_lowercase()
.contains("if-match: \"stable\""));
assert!(requests[1].starts_with("PROPFIND /vault/file.bin "));
assert!(requests[1].to_ascii_lowercase().contains("depth: 0"));
}
#[test]
fn get_rejects_etag_changed_after_download_and_http_failures() {
let listing = r#"<multistatus><response><href>/vault/note.md</href>
<getetag>&quot;new&quot;</getetag></response></multistatus>"#;
let (client, requests) = webdav_test_client(vec![
("200 OK\r\nETag: \"old\"", "snapshot"),
("207 Multi-Status", listing),
]);
assert!(client
.get("note.md", "\"old\"")
.unwrap_err()
.contains("retry"));
assert_eq!(requests.join().unwrap().len(), 2);
for (status, expected_error) in [
("412 Precondition Failed", "retry"),
("404 Not Found", "HTTP 404"),
] {
let (client, requests) = webdav_test_client(vec![(status, "ignored")]);
assert!(client
.get("note.md", "\"old\"")
.unwrap_err()
.contains(expected_error));
assert_eq!(requests.join().unwrap().len(), 1);
}
}
#[test]
fn get_enforces_whole_body_timeout_for_progressing_chunked_download() {
use std::io::Write;
use std::net::TcpListener;
use std::time::Duration;
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/vault", listener.local_addr().unwrap());
let server = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(2)))
.unwrap();
stream
.set_write_timeout(Some(Duration::from_secs(2)))
.unwrap();
let mut request = Vec::new();
let mut buffer = [0; 4096];
while !request.windows(4).any(|part| part == b"\r\n\r\n") {
let count = stream.read(&mut buffer).unwrap();
assert!(count > 0);
request.extend_from_slice(&buffer[..count]);
}
assert!(request.starts_with(b"GET /vault/file.bin "));
stream
.write_all(
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\
Connection: close\r\n\r\n1\r\nx\r\n",
)
.unwrap();
let mut chunks_sent = 1;
// Each gap is below the 400ms timeout, but the full body takes 1s.
for _ in 0..20 {
std::thread::sleep(Duration::from_millis(50));
if stream.write_all(b"1\r\nx\r\n").is_err() {
return chunks_sent;
}
chunks_sent += 1;
}
let _ = stream.write_all(b"0\r\n\r\n");
chunks_sent
});
let mut client = WebdavClient::new(WebdavConfig {
url,
username: String::new(),
password: String::new(),
})
.unwrap();
client.client = reqwest::blocking::Client::builder()
.no_proxy()
.timeout(Duration::from_millis(400))
.build()
.unwrap();
let result = client.get("file.bin", "");
let chunks_sent = server.join().unwrap();
assert!(
chunks_sent > 1,
"server must make progress during the download"
);
assert!(
result.is_err(),
"accepted a body exceeding the total timeout"
);
}
}
+5
View File
@@ -163,6 +163,10 @@ pub struct AppConfig {
pub accent_color: Option<String>,
#[serde(default)]
pub font_size: Option<u32>,
/// The size chosen in Appearance. Zooming changes `font_size` only, and resetting the zoom
/// returns to this.
#[serde(default)]
pub preferred_font_size: Option<u32>,
#[serde(default = "default_true")]
pub scroll_to_change_font_size: bool,
#[serde(default)]
@@ -337,6 +341,7 @@ impl Default for AppConfig {
system_dark_theme: default_system_dark_theme(),
accent_color: None,
font_size: None,
preferred_font_size: None,
scroll_to_change_font_size: true,
toolbar_layout: None,
font_family: None,
+158 -5
View File
@@ -1,9 +1,70 @@
use crate::types::NoteMeta;
use chrono::{NaiveDate, NaiveDateTime, Utc};
use gray_matter::engine::YAML;
use gray_matter::Matter;
use gray_matter::engine::{Engine, YAML as UnboundedYaml};
use gray_matter::{Matter, Pod};
use serde::{Deserialize, Serialize};
const MAX_FRONTMATTER_BYTES: usize = 64 * 1024;
const MAX_FRONTMATTER_ALIASES: usize = 16;
const MAX_FRONTMATTER_INDENT: usize = 200;
const MAX_FRONTMATTER_INDICATORS: usize = 32;
/// YAML frontmatter engine for notes, which are untrusted input (sync, import, shared vaults).
/// The underlying loader expands every alias by copying and recurses once per nesting level, so a
/// note of a few hundred bytes could take gigabytes or overflow the stack and take the app down
/// at every start. Frontmatter outside these bounds is treated as carrying no metadata.
pub struct BoundedYaml;
impl Engine for BoundedYaml {
fn parse(content: &str) -> Pod {
if frontmatter_within_bounds(content) {
UnboundedYaml::parse(content)
} else {
Pod::Null
}
}
}
fn frontmatter_within_bounds(content: &str) -> bool {
if content.len() > MAX_FRONTMATTER_BYTES {
return false;
}
// Aliases multiply: with few of them the expansion stays small whatever they point at.
let bytes = content.as_bytes();
let aliases = bytes
.iter()
.enumerate()
.filter(|&(index, &byte)| {
byte == b'*'
&& (index == 0
|| matches!(
bytes[index - 1],
b' ' | b'\t' | b'\n' | b'[' | b',' | b':' | b'-' | b'{'
))
&& bytes
.get(index + 1)
.is_some_and(|next| next.is_ascii_alphanumeric() || matches!(next, b'_' | b'-'))
})
.count();
if aliases > MAX_FRONTMATTER_ALIASES {
return false;
}
// Block nesting deepens by indentation, or by indicators chained on one line (`- - - x`).
content.lines().all(|line| {
let indent = line.len() - line.trim_start_matches(' ').len();
let mut rest = &line.as_bytes()[indent..];
let mut indicators = 0;
while let [b'-' | b'?' | b':', b' ' | b'\t', tail @ ..] = rest {
indicators += 1;
rest = tail;
while let [b' ' | b'\t', tail @ ..] = rest {
rest = tail;
}
}
indent <= MAX_FRONTMATTER_INDENT && indicators <= MAX_FRONTMATTER_INDICATORS
})
}
#[derive(Debug, Serialize, Deserialize, Default)]
struct RawFrontmatter {
id: Option<String>,
@@ -41,7 +102,7 @@ pub fn parse_date_flexible(s: &str) -> Option<chrono::DateTime<Utc>> {
}
pub fn parse_note(raw: &str, filename: &str) -> (NoteMeta, String) {
let matter = Matter::<YAML>::new();
let matter = Matter::<BoundedYaml>::new();
let result = matter.parse(raw);
let fm: RawFrontmatter = result
@@ -134,7 +195,7 @@ pub fn update_note_raw(meta: &NoteMeta, body: &str) -> String {
/// Merge NoteMeta fields into existing frontmatter, preserving unknown YAML keys.
/// Falls back to serialize_frontmatter() if the original has no frontmatter.
pub fn merge_frontmatter(original_raw: &str, meta: &NoteMeta, body: &str) -> String {
let matter = Matter::<YAML>::new();
let matter = Matter::<BoundedYaml>::new();
let parsed = matter.parse(original_raw);
// If original had no frontmatter, use the simple serializer
@@ -214,7 +275,7 @@ pub fn filename_to_title(filename: &str) -> String {
/// Quick title extraction from raw note content (frontmatter only, no full parse)
pub fn extract_title(raw: &str) -> Option<String> {
let matter = Matter::<YAML>::new();
let matter = Matter::<BoundedYaml>::new();
let result = matter.parse(raw);
let fm: RawFrontmatter = result
.data
@@ -422,4 +483,96 @@ mod tests {
eprintln!("Stripped: {:?}", stripped);
assert!(stripped.contains("Telefoonnummer"));
}
fn alias_bomb(levels: usize, width: usize) -> String {
let mut note = format!(
"---\ntitle: Bomb\na0: &a0 [{}]\n",
vec!["1"; width].join(",")
);
for level in 1..levels {
let alias = format!("*a{}", level - 1);
note.push_str(&format!(
"a{level}: &a{level} [{}]\n",
vec![alias.as_str(); width].join(",")
));
}
note.push_str("---\nbody\n");
note
}
/// Runs on a stack the size of the app's worker threads, where the unbounded loader overflowed.
fn on_worker_stack<T: Send + 'static>(work: impl FnOnce() -> T + Send + 'static) -> T {
std::thread::Builder::new()
.stack_size(2 * 1024 * 1024)
.spawn(work)
.unwrap()
.join()
.unwrap()
}
#[test]
fn hostile_frontmatter_is_read_as_a_note_without_metadata() {
let nested_on_one_line = format!("---\nt: {}x\n---\nbody\n", "- ".repeat(2400));
let mut nested_by_indent = String::from("---\nt:\n");
for depth in 0..400 {
note_line(&mut nested_by_indent, depth);
}
nested_by_indent.push_str("---\nbody\n");
let oversized = format!(
"---\ntitle: Big\nfiller: \"{}\"\n---\nbody\n",
"x".repeat(MAX_FRONTMATTER_BYTES)
);
for note in [
alias_bomb(10, 8),
alias_bomb(40, 2),
nested_on_one_line,
nested_by_indent,
oversized,
] {
let started = std::time::Instant::now();
let (title, content, extracted, merged) = on_worker_stack(move || {
let (meta, content) = parse_note(&note, "Fallback Name.md");
let merged = merge_frontmatter(&note, &meta, &content);
(meta.title, content, extract_title(&note), merged)
});
assert!(started.elapsed() < std::time::Duration::from_secs(2));
// No metadata is taken from it, and the body is still split off as usual.
assert_eq!(title, "Fallback Name");
assert_eq!(content, "body");
assert_eq!(extracted, None);
assert!(
merged.ends_with("body"),
"{}",
&merged[merged.len().saturating_sub(40)..]
);
}
}
fn note_line(note: &mut String, depth: usize) {
note.push_str(&" ".repeat(depth));
note.push_str("-\n");
}
#[test]
fn ordinary_frontmatter_is_unaffected_by_the_bounds() {
let (meta, content) = parse_note(
"---\nid: abc\ntitle: \"*Starred* title: 5*3\"\ntags:\n - one\n - two\npinned: true\n---\n# Body\n",
"x.md",
);
assert_eq!(meta.id, "abc");
assert_eq!(meta.title, "*Starred* title: 5*3");
assert_eq!(meta.tags, vec!["one", "two"]);
assert!(meta.pinned);
assert_eq!(content, "# Body");
// A few aliases and modest nesting are ordinary YAML.
let (meta, _) = parse_note(
"---\ntitle: Shared\nbase: &base [a, b]\ntags: *base\nextra:\n - - nested\n - list\n---\nbody\n",
"x.md",
);
assert_eq!(meta.title, "Shared");
assert_eq!(meta.tags, vec!["a", "b"]);
assert!(frontmatter_within_bounds(&format!(
"title: {}",
"x".repeat(MAX_FRONTMATTER_BYTES - 16)
)));
}
}
+70 -21
View File
@@ -3,8 +3,8 @@ use std::fs::OpenOptions;
use std::io;
use std::path::{Path, PathBuf};
use crate::vault::frontmatter::BoundedYaml;
use chrono::Utc;
use gray_matter::engine::YAML;
use gray_matter::Matter;
use regex::Regex;
use uuid::Uuid;
@@ -259,7 +259,7 @@ fn build_file_index(vault: &Path) -> Result<HashMap<String, String>, String> {
}
fn rename_deprecated_properties(raw: &str) -> String {
let matter = Matter::<YAML>::new();
let matter = Matter::<BoundedYaml>::new();
let result = matter.parse(raw);
if result.matter.trim().is_empty() {
@@ -298,7 +298,7 @@ fn rename_deprecated_properties(raw: &str) -> String {
}
fn normalize_frontmatter(raw: &str, path: &Path) -> (NoteMeta, String) {
let matter = Matter::<YAML>::new();
let matter = Matter::<BoundedYaml>::new();
let result = matter.parse(raw);
let yaml_str = result.matter.trim();
@@ -883,30 +883,79 @@ fn pathdiff(target: &str, base: &str) -> Result<String, ()> {
}
fn percent_decode(s: &str) -> String {
let mut result = String::with_capacity(s.len());
let mut chars = s.bytes();
while let Some(b) = chars.next() {
if b == b'%' {
let hi = chars.next();
let lo = chars.next();
if let (Some(h), Some(l)) = (hi, lo) {
if let Ok(byte) = u8::from_str_radix(&format!("{}{}", h as char, l as char), 16) {
result.push(byte as char);
continue;
}
}
result.push('%');
} else {
result.push(b as char);
}
}
result
// Keep the complete original reference if escaped bytes are not valid UTF-8.
urlencoding::decode(s)
.map(|decoded| decoded.into_owned())
.unwrap_or_else(|_| s.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn percent_decode_preserves_unicode_plus_and_malformed_escapes() {
for (input, expected) in [
("café 雪+.png", "café 雪+.png"),
("caf%C3%A9%20%E9%9B%AA+.png", "café 雪+.png"),
("bad%GG%20name.png", "bad%GG name.png"),
("tail%", "tail%"),
("tail%2", "tail%2"),
("雪%FF%20+%GG", "雪%FF%20+%GG"),
("%E9%9B", "%E9%9B"),
] {
assert_eq!(percent_decode(input), expected, "input {input:?}");
}
}
#[test]
fn import_rewrites_unicode_encoded_and_malformed_attachment_references() {
let vault = tempfile::tempdir().unwrap();
let root = vault.path();
let note_dir = root.join("笔记");
std::fs::create_dir(&note_dir).unwrap();
let attachments = [
"café 雪+.png",
"bad%GG.png",
"tail%.png",
"tail%2.png",
"bytes%FF%20.png",
];
for name in attachments {
std::fs::write(note_dir.join(name), name.as_bytes()).unwrap();
}
let note_path = note_dir.join("Entry.md");
std::fs::write(
&note_path,
"# Entry\n\n![raw](café%20雪+.png)\n\
[encoded](caf%C3%A9%20%E9%9B%AA+.png#preview)\n\
![malformed](bad%GG.png)\n![percent](tail%.png)\n\
[short](tail%2.png)\n[invalid UTF-8](bytes%FF%20.png)\n",
)
.unwrap();
let result = import(root.to_str().unwrap()).unwrap();
assert_eq!(result.attachments_moved, attachments.len() as u64);
let note = std::fs::read_to_string(&note_path).unwrap();
for reference in [
"![raw](../.helixnotes/attachments/笔记/café%20雪+.png)",
"[encoded](../.helixnotes/attachments/笔记/café%20雪+.png#preview)",
"![malformed](../.helixnotes/attachments/笔记/bad%GG.png)",
"![percent](../.helixnotes/attachments/笔记/tail%.png)",
"[short](../.helixnotes/attachments/笔记/tail%2.png)",
"[invalid UTF-8](../.helixnotes/attachments/笔记/bytes%FF%20.png)",
] {
assert!(note.contains(reference), "missing {reference:?} in {note}");
}
for name in attachments {
assert!(!note_dir.join(name).exists());
assert_eq!(
std::fs::read(root.join(".helixnotes/attachments/笔记").join(name)).unwrap(),
name.as_bytes()
);
}
}
#[test]
fn test_normalize_tags_comma_string() {
let mapping: serde_yaml::Mapping = serde_yaml::from_str("tags: \"a, b, c\"").unwrap();
+3
View File
@@ -2,3 +2,6 @@ pub mod frontmatter;
pub mod import;
pub mod operations;
pub mod watcher;
#[cfg(test)]
mod note_import_tests;
+493
View File
@@ -0,0 +1,493 @@
use super::operations::{
ensure_vault_structure, import_note as import_from_root, ExternalNoteReference,
};
fn import_note(
vault: &str,
source: &str,
notebook: Option<&str>,
raw: &str,
references: Vec<ExternalNoteReference>,
authorize: impl Fn(&Path) -> Result<(), String>,
) -> Result<crate::types::NoteContent, String> {
import_from_root(
vault,
source,
Path::new(source).parent().unwrap(),
notebook,
raw,
references,
authorize,
)
}
use std::fs;
use std::path::{Path, PathBuf};
fn reference(raw: &str, original: &str, href: &str, html: bool) -> ExternalNoteReference {
let from = raw.find(original).unwrap();
ExternalNoteReference {
from,
to: from + original.len(),
href: href.into(),
html,
pdf: false,
}
}
fn fixture(raw: &str) -> (tempfile::TempDir, String, PathBuf) {
let root = tempfile::tempdir().unwrap();
let vault = root.path().join("vault").to_string_lossy().into_owned();
ensure_vault_structure(&vault).unwrap();
let external = root.path().join("external");
fs::create_dir_all(external.join("images")).unwrap();
let source = external.join("External.md");
fs::write(&source, raw).unwrap();
(root, vault, source)
}
fn attachments(vault: &str) -> Vec<PathBuf> {
fs::read_dir(Path::new(vault).join(".helixnotes/attachments"))
.unwrap()
.map(|entry| entry.unwrap().path())
.collect()
}
#[test]
fn imports_relative_files_once_preserving_originals_frontmatter_and_literal_content() {
let raw = "---\r\ntitle: Portable\r\ncustom: 雪 📝\r\n---\r\n![One](images/café%20雪.png)\r\n![Two](images/./café%20雪.png)\r\n<a href=\"images/report.pdf#page=2&amp;mode=fit\">PDF</a>\r\n\r\n~~~md\r\n![Example](missing.png)\r\n~~~\r\n";
for notebook in [None, Some("Projects/Nested")] {
let (_root, vault, source) = fixture(raw);
let parent = source.parent().unwrap();
fs::write(parent.join("images/café 雪.png"), b"image bytes").unwrap();
fs::write(parent.join("images/report.pdf"), b"pdf bytes").unwrap();
fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap();
let references = vec![
reference(raw, "images/café%20雪.png", "images/café%20雪.png", false),
reference(
raw,
"images/./café%20雪.png",
"images/./café%20雪.png",
false,
),
reference(
raw,
"images/report.pdf#page=2&amp;mode=fit",
"images/report.pdf#page=2&mode=fit",
true,
),
];
let note = import_note(
&vault,
source.to_str().unwrap(),
notebook,
raw,
references,
|path| {
if path.starts_with(parent) {
Ok(())
} else {
Err("Not selected".into())
}
},
)
.unwrap();
assert!(note
.raw
.starts_with("---\r\ntitle: Portable\r\ncustom: 雪 📝\r\n---\r\n"));
assert!(note.raw.contains("![Example](missing.png)"));
assert!(note.raw.contains("#page=2&amp;mode=fit"));
assert_eq!(fs::read_to_string(&source).unwrap(), raw);
assert_eq!(fs::read_to_string(&note.path).unwrap(), note.raw);
let files = attachments(&vault);
assert_eq!(
files.len(),
2,
"aliases of one file share one portable copy"
);
for file in files {
let filename = urlencoding::encode(file.file_name().unwrap().to_str().unwrap());
let relative = format!(".helixnotes/attachments/{filename}");
assert!(note.raw.contains(&relative));
assert!(Path::new(&vault)
.join(urlencoding::decode(&relative).unwrap().as_ref())
.is_file());
}
assert_eq!(
fs::read(parent.join("images/café 雪.png")).unwrap(),
b"image bytes"
);
assert_eq!(
fs::read(parent.join("images/report.pdf")).unwrap(),
b"pdf bytes"
);
}
}
#[test]
fn imported_pdf_embeds_keep_the_vault_root_path_convention_in_nested_notebooks() {
let raw = "<div data-pdf-src=\"images/report.pdf\" data-pdf-name=\"Report\"></div>";
let (_root, vault, source) = fixture(raw);
fs::write(source.parent().unwrap().join("images/report.pdf"), b"pdf").unwrap();
fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap();
let mut pdf = reference(raw, "images/report.pdf", "images/report.pdf", true);
pdf.pdf = true;
let note = import_note(
&vault,
source.to_str().unwrap(),
Some("Projects/Nested"),
raw,
vec![pdf],
|_| Ok(()),
)
.unwrap();
assert!(note.raw.contains("data-pdf-src=\".helixnotes/attachments/"));
assert!(note.raw.contains("data-pdf-name=\"Report\""));
assert_eq!(attachments(&vault).len(), 1);
}
#[test]
fn imported_document_links_remain_managed_and_stable_after_note_and_notebook_moves() {
let raw = "[Report](images/report.pdf#page=2)";
let (_root, vault, source) = fixture(raw);
fs::write(source.parent().unwrap().join("images/report.pdf"), b"pdf").unwrap();
fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap();
fs::create_dir_all(Path::new(&vault).join("Archive")).unwrap();
let note = import_note(
&vault,
source.to_str().unwrap(),
Some("Projects/Nested"),
raw,
vec![reference(
raw,
"images/report.pdf#page=2",
"images/report.pdf#page=2",
false,
)],
|_| Ok(()),
)
.unwrap();
assert!(
note.raw.starts_with("[Report](.helixnotes/attachments/"),
"managed links must not depend on notebook depth"
);
let href = note
.raw
.strip_prefix("[Report](")
.unwrap()
.strip_suffix(')')
.unwrap();
let attachment = Path::new(&vault).join(
urlencoding::decode(href.split('#').next().unwrap())
.unwrap()
.as_ref(),
);
assert_eq!(fs::read(attachment).unwrap(), b"pdf");
let moved = super::operations::move_note(&vault, &note.path, &vault).unwrap();
assert_eq!(fs::read_to_string(moved).unwrap(), note.raw);
// Imported managed paths remain stable when their containing notebook is moved too.
let second = import_note(
&vault,
source.to_str().unwrap(),
Some("Projects/Nested"),
raw,
vec![reference(
raw,
"images/report.pdf#page=2",
"images/report.pdf#page=2",
false,
)],
|_| Ok(()),
)
.unwrap();
super::operations::move_notebook(
&vault,
Path::new(&vault).join("Projects").to_str().unwrap(),
Path::new(&vault).join("Archive").to_str().unwrap(),
)
.unwrap();
assert_eq!(
fs::read_to_string(Path::new(&vault).join("Archive/Projects/Nested/External.md")).unwrap(),
second.raw
);
}
#[test]
fn import_does_not_replace_existing_note_or_same_named_attachment() {
let raw = "![Image](images/photo.png)";
let (_root, vault, source) = fixture(raw);
fs::write(
source.parent().unwrap().join("images/photo.png"),
b"new image",
)
.unwrap();
fs::write(Path::new(&vault).join("External.md"), b"keep note").unwrap();
fs::write(
Path::new(&vault).join(".helixnotes/attachments/photo.png"),
b"keep image",
)
.unwrap();
let note = import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![reference(
raw,
"images/photo.png",
"images/photo.png",
false,
)],
|_| Ok(()),
)
.unwrap();
assert!(note.path.ends_with("External (2).md"));
assert_eq!(
fs::read(Path::new(&vault).join("External.md")).unwrap(),
b"keep note"
);
assert_eq!(
fs::read(Path::new(&vault).join(".helixnotes/attachments/photo.png")).unwrap(),
b"keep image"
);
assert_eq!(attachments(&vault).len(), 2);
}
#[test]
fn stale_sources_missing_files_and_denied_attachments_publish_nothing() {
let raw = "![Image](images/photo.png)";
let (_root, vault, source) = fixture(raw);
let plan = || {
vec![reference(
raw,
"images/photo.png",
"images/photo.png",
false,
)]
};
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
"stale",
plan(),
|_| Ok(())
)
.is_err());
assert!(
import_note(&vault, source.to_str().unwrap(), None, raw, plan(), |_| Ok(
()
))
.is_err()
);
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
let denied = import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
plan(),
|path| {
if path == source {
Ok(())
} else {
Err("Attachment not selected".into())
}
},
);
assert!(denied.unwrap_err().contains("not selected"));
assert!(attachments(&vault).is_empty());
assert!(!Path::new(&vault).join("External.md").exists());
assert_eq!(fs::read_to_string(&source).unwrap(), raw);
}
#[test]
fn invalid_destinations_ranges_and_absolute_references_are_rejected() {
let raw = "雪 ![Image](images/photo.png)";
let (_root, vault, source) = fixture(raw);
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
assert!(import_note(
&vault,
source.to_str().unwrap(),
Some("../escape"),
raw,
vec![],
|_| Ok(())
)
.is_err());
for href in [
"/private.png",
"%2Fprivate.png",
"C:/private.png",
"https://example.com/file.png",
"Related.md",
] {
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![reference(raw, "images/photo.png", href, false)],
|_| Ok(())
)
.is_err());
}
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![ExternalNoteReference {
from: 1,
to: 3,
href: "images/photo.png".into(),
html: false,
pdf: false,
}],
|_| Ok(())
)
.is_err());
let plan = vec![
reference(raw, "images/photo.png", "images/photo.png", false),
reference(raw, "images/photo.png", "images/photo.png", false),
];
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
plan,
|_| Ok(())
)
.is_err());
assert!(attachments(&vault).is_empty());
}
#[test]
fn failed_note_publication_removes_only_its_new_attachments() {
let raw = "![Image](images/photo.png)";
let (_root, vault, source) = fixture(raw);
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
fs::write(Path::new(&vault).join("External.md"), b"keep").unwrap();
for number in 2..=100 {
fs::write(
Path::new(&vault).join(format!("External ({number}).md")),
b"keep",
)
.unwrap();
}
let original_asset = Path::new(&vault).join(".helixnotes/attachments/original.png");
fs::write(&original_asset, b"keep attachment").unwrap();
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![reference(
raw,
"images/photo.png",
"images/photo.png",
false
)],
|_| Ok(())
)
.is_err());
assert_eq!(attachments(&vault), vec![original_asset]);
assert_eq!(
fs::read(Path::new(&vault).join("External.md")).unwrap(),
b"keep"
);
assert_eq!(fs::read_to_string(&source).unwrap(), raw);
assert!(!fs::read_dir(&vault).unwrap().any(|entry| entry
.unwrap()
.file_name()
.to_string_lossy()
.starts_with(".helixnotes-import-")));
}
#[test]
fn note_content_cannot_import_parent_files_or_hidden_secrets_from_a_broad_scope() {
let raw = "[File](asset.bin)";
let (root, vault, source) = fixture(raw);
let parent = source.parent().unwrap();
for name in [
".ssh/key.bin",
".helixnotes/history/private.bin",
".helixnotes/attachments/photo.bin",
] {
let file = parent.join(name);
fs::create_dir_all(file.parent().unwrap()).unwrap();
fs::write(file, b"private fixture").unwrap();
}
fs::write(root.path().join("private.bin"), b"private outside fixture").unwrap();
for href in [
"../private.bin",
".ssh/key.bin",
".helixnotes/history/private.bin",
] {
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![reference(raw, "asset.bin", href, false)],
|_| Ok(())
)
.is_err());
assert!(attachments(&vault).is_empty());
assert!(!Path::new(&vault).join("External.md").exists());
}
let note = import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![reference(
raw,
"asset.bin",
".helixnotes/attachments/photo.bin",
false,
)],
|_| Ok(()),
)
.unwrap();
assert!(note.raw.contains(".helixnotes/attachments/"));
assert_eq!(attachments(&vault).len(), 1);
}
#[cfg(unix)]
#[test]
fn import_rejects_attachment_and_notebook_destinations_that_follow_symlinks() {
let raw = "![Image](images/photo.png)";
let (root, vault, source) = fixture(raw);
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
let outside = root.path().join("outside");
fs::create_dir(&outside).unwrap();
std::os::unix::fs::symlink(&outside, Path::new(&vault).join("Notebook")).unwrap();
assert!(import_note(
&vault,
source.to_str().unwrap(),
Some("Notebook"),
raw,
vec![],
|_| Ok(())
)
.is_err());
let directory = Path::new(&vault).join(".helixnotes/attachments");
fs::remove_dir(&directory).unwrap();
std::os::unix::fs::symlink(&outside, &directory).unwrap();
assert!(import_note(
&vault,
source.to_str().unwrap(),
None,
raw,
vec![reference(
raw,
"images/photo.png",
"images/photo.png",
false
)],
|_| Ok(())
)
.is_err());
assert!(fs::read_dir(outside).unwrap().next().is_none());
}
+637 -28
View File
@@ -21,11 +21,81 @@ fn canonicalize_path(path: &Path, label: &str) -> Result<PathBuf, String> {
fs::canonicalize(path).map_err(|error| format!("Invalid {label}: {error}"))
}
/// Whether Windows would resolve this path over the network: `\\host\share\..`, `//host/share/..`
/// or the verbatim `\\?\UNC\host\..`. Merely examining such a path (does it exist, what is its
/// real path) makes Windows contact the host and offer it the user's login, so a path taken from
/// note content must be judged by its text before the filesystem is asked anything.
pub(crate) fn is_network_path(path: &str) -> bool {
let normalized = path.replace('\\', "/");
match normalized
.strip_prefix("//?/")
.or_else(|| normalized.strip_prefix("//./"))
{
Some(rest) => rest
.get(..4)
.is_some_and(|prefix| prefix.eq_ignore_ascii_case("unc/")),
None => normalized.starts_with("//"),
}
}
/// `//host/share/folder` in one spelling: forward slashes, no verbatim prefix, lower case.
fn network_path_key(path: &str) -> String {
let normalized = path.replace('\\', "/");
let rest = match normalized.get(..8) {
Some(prefix)
if prefix.eq_ignore_ascii_case("//?/unc/")
|| prefix.eq_ignore_ascii_case("//./unc/") =>
{
format!("//{}", &normalized[8..])
}
_ => normalized,
};
rest.trim_end_matches('/').to_lowercase()
}
/// Whether a network path lies, by its text alone, inside one of the folders the user already
/// works in (a vault on a share, or a folder they opened a note from).
pub(crate) fn network_path_within(path: &str, roots: &[String]) -> bool {
let key = network_path_key(path);
if key.split('/').any(|segment| segment == "..") {
return false;
}
roots
.iter()
.filter(|root| is_network_path(root))
.any(|root| {
let root = network_path_key(root);
key == root
|| key
.strip_prefix(&root)
.is_some_and(|rest| rest.starts_with('/'))
})
}
/// Refuses a network path outside the known folders before any filesystem call. Only Windows
/// reads these spellings as network paths; elsewhere `//x/y` is an ordinary local path.
pub(crate) fn refuse_foreign_network_path(path: &Path, roots: &[String]) -> Result<(), String> {
refuse_foreign_network_path_on(cfg!(windows), path, roots)
}
fn refuse_foreign_network_path_on(
windows: bool,
path: &Path,
roots: &[String],
) -> Result<(), String> {
let text = path.to_string_lossy();
if windows && is_network_path(&text) && !network_path_within(&text, roots) {
return Err("Network paths outside the vault are not opened".to_string());
}
Ok(())
}
fn ensure_vault_content_path(
vault_path: &str,
requested_path: &Path,
allow_root: bool,
) -> Result<PathBuf, String> {
refuse_foreign_network_path(requested_path, &[vault_path.to_string()])?;
let vault = canonicalize_path(Path::new(vault_path), "vault path")?;
let requested = canonicalize_path(requested_path, "vault item path")?;
let metadata = vault.join(".helixnotes");
@@ -87,6 +157,7 @@ fn ensure_notebook_path(vault_path: &str, requested_path: &Path) -> Result<PathB
}
fn ensure_trash_entry(vault_path: &str, requested_path: &Path) -> Result<PathBuf, String> {
refuse_foreign_network_path(requested_path, &[vault_path.to_string()])?;
let trash = canonicalize_path(&helixnotes_dir(vault_path).join("trash"), "trash path")?;
let requested = canonicalize_path(requested_path, "trash item path")?;
if requested == trash || !requested.starts_with(&trash) {
@@ -549,7 +620,9 @@ pub fn scan_notes(vault_path: &str, notebook_path: Option<&str>) -> Result<Vec<N
WalkDir::new(root)
.into_iter()
// filter_entry skips descending into hidden dirs (unlike filter)
.filter_entry(|e| !is_hidden(e.path()) && !e.path().starts_with(&hn_dir))
.filter_entry(|e| {
e.depth() == 0 || (!is_hidden(e.path()) && !e.path().starts_with(&hn_dir))
})
.filter_map(|e| e.ok())
.map(|e| e.path().to_path_buf())
.filter(|p| p.is_file() && p.extension().and_then(|x| x.to_str()) == Some("md"))
@@ -579,7 +652,9 @@ pub fn scan_notes(vault_path: &str, notebook_path: Option<&str>) -> Result<Vec<N
WalkDir::new(root)
.into_iter()
.filter_entry(|e| {
!is_hidden(e.path()) && !e.path().starts_with(helixnotes_dir(vault_path))
e.depth() == 0
|| (!is_hidden(e.path())
&& !e.path().starts_with(helixnotes_dir(vault_path)))
})
.filter_map(|e| e.ok())
.map(|e| e.path().to_path_buf())
@@ -606,12 +681,7 @@ fn read_note_entry(path: &Path, vault_root: &Path) -> Result<NoteEntry, String>
/// Persisted creation dates survive atomic file replacement.
#[cfg(any(mobile, test))]
fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result<NoteEntry, String> {
// Read first 2KB - enough for frontmatter with tags, title, pinned
let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
let mut buf = vec![0u8; 2048];
let bytes_read = file.read(&mut buf).map_err(|e| e.to_string())?;
buf.truncate(bytes_read);
let raw = String::from_utf8_lossy(&buf);
let raw = read_note_head(path)?;
let filename = path
.file_name()
@@ -649,13 +719,54 @@ fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result<NoteE
})
}
/// Fast version: reads only the first ~2KB of the file (enough for frontmatter + preview).
pub(crate) fn read_note_entry_fast(path: &Path, vault_root: &Path) -> Result<NoteEntry, String> {
const NOTE_HEAD_BLOCK: usize = 2048;
const NOTE_HEAD_LIMIT: usize = 256 * 1024;
/// Where the YAML frontmatter stands within a partially read note: `None` when the note has
/// none, `Some(None)` while the closing delimiter is still unread, else the offset after it.
fn frontmatter_state(head: &[u8]) -> Option<Option<usize>> {
let rest = head.strip_prefix(b"---")?;
if !matches!(rest.first(), None | Some(b'\n' | b'\r')) {
return None;
}
let mut offset = 3;
for line in head[3..].split_inclusive(|byte| *byte == b'\n') {
offset += line.len();
if line.ends_with(b"\n") && line.trim_ascii_end() == b"---" {
return Some(Some(offset));
}
}
Some(None)
}
/// Reads the start of a note: its whole frontmatter plus a block of body for previews, instead
/// of a fixed prefix that truncates long frontmatter and loses its title and tags.
pub(crate) fn read_note_head(path: &Path) -> Result<String, String> {
let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
let mut buf = vec![0u8; 2048];
let bytes_read = file.read(&mut buf).map_err(|e| e.to_string())?;
buf.truncate(bytes_read);
let raw = String::from_utf8_lossy(&buf);
let mut head = Vec::with_capacity(NOTE_HEAD_BLOCK);
let mut target = NOTE_HEAD_BLOCK;
loop {
let wanted = target - head.len();
(&mut file)
.take(wanted as u64)
.read_to_end(&mut head)
.map_err(|e| e.to_string())?;
if head.len() < target || head.len() >= NOTE_HEAD_LIMIT {
break;
}
target = match frontmatter_state(&head) {
None => break,
Some(None) => (head.len() * 2).min(NOTE_HEAD_LIMIT),
Some(Some(end)) if head.len() >= end + NOTE_HEAD_BLOCK => break,
Some(Some(end)) => end + NOTE_HEAD_BLOCK,
};
}
Ok(String::from_utf8_lossy(&head).into_owned())
}
/// Fast version: reads the frontmatter and the start of the body (enough for a preview).
pub(crate) fn read_note_entry_fast(path: &Path, vault_root: &Path) -> Result<NoteEntry, String> {
let raw = read_note_head(path)?;
read_note_entry_from_str(&raw, path, vault_root)
}
@@ -714,7 +825,10 @@ pub fn read_vault_note(vault_path: &str, path: &str) -> Result<NoteContent, Stri
read_note_content(&validated, path)
}
fn read_note_content(validated: &Path, reported_path: &str) -> Result<NoteContent, String> {
pub(crate) fn read_note_content(
validated: &Path,
reported_path: &str,
) -> Result<NoteContent, String> {
let p = validated;
let raw = fs::read_to_string(p).map_err(|e| e.to_string())?;
let filename = p
@@ -1317,7 +1431,9 @@ fn update_wikilinks_after_rename(
// vs. the other note with the same title.
let title_is_unique = !WalkDir::new(vault)
.into_iter()
.filter_entry(|e| !is_hidden(e.path()) && !e.path().starts_with(&hn_dir))
.filter_entry(|e| {
e.depth() == 0 || (!is_hidden(e.path()) && !e.path().starts_with(&hn_dir))
})
.filter_map(|e| e.ok())
.any(|e| {
let p = e.path();
@@ -1336,7 +1452,9 @@ fn update_wikilinks_after_rename(
for entry in WalkDir::new(vault)
.into_iter()
.filter_entry(|e| !is_hidden(e.path()) && !e.path().starts_with(&hn_dir))
.filter_entry(|e| {
e.depth() == 0 || (!is_hidden(e.path()) && !e.path().starts_with(&hn_dir))
})
.filter_map(|e| e.ok())
{
let path = entry.path();
@@ -1663,26 +1781,20 @@ pub fn get_all_tags(vault_path: &str) -> Result<Vec<(String, usize)>, String> {
let hn_dir = helixnotes_dir(vault_path);
let md_files: Vec<PathBuf> = WalkDir::new(vault_path)
.into_iter()
.filter_entry(|entry| entry.depth() == 0 || entry.path() != hn_dir.as_path())
.filter_map(|e| e.ok())
.filter(|e| {
let p = e.path();
p.is_file()
&& p.extension().and_then(|x| x.to_str()) == Some("md")
&& !is_hidden(p)
&& !p.starts_with(&hn_dir)
p.is_file() && p.extension().and_then(|x| x.to_str()) == Some("md") && !is_hidden(p)
})
.map(|e| e.path().to_path_buf())
.collect();
// Read frontmatter in parallel (only need tags, so partial read is fine)
// Read frontmatter in parallel (only need tags, so the body is not read)
let all_tags: Vec<Vec<String>> = md_files
.par_iter()
.filter_map(|path| {
let mut file = fs::File::open(path).ok()?;
let mut buf = vec![0u8; 1024]; // Tags are in frontmatter, 1KB is plenty
let n = file.read(&mut buf).ok()?;
buf.truncate(n);
let raw = String::from_utf8_lossy(&buf);
let raw = read_note_head(path).ok()?;
let filename = path.file_name()?.to_string_lossy().to_string();
let (meta, _) = frontmatter::parse_note(&raw, &filename);
Some(meta.tags)
@@ -1734,6 +1846,256 @@ pub fn save_attachment(vault_path: &str, name: &str, data: &[u8]) -> Result<Stri
}
}
#[derive(Debug, serde::Deserialize)]
pub struct ExternalNoteReference {
pub from: usize,
pub to: usize,
pub href: String,
pub html: bool,
#[serde(default)]
pub pdf: bool,
}
/// Import an accepted Markdown snapshot and its scoped relative attachments without overwriting files.
pub fn import_note(
vault_path: &str,
source_path: &str,
source_root: &Path,
notebook_relative: Option<&str>,
expected_raw: &str,
mut references: Vec<ExternalNoteReference>,
authorize: impl Fn(&Path) -> Result<(), String>,
) -> Result<NoteContent, String> {
let source = canonicalize_path(Path::new(source_path), "source note")?;
authorize(&source)?;
if !source.is_file() || source.extension().and_then(|ext| ext.to_str()) != Some("md") {
return Err("Import source must be a Markdown file".into());
}
let source_root = canonicalize_path(source_root, "source attachment root")?;
if !source.starts_with(&source_root) {
return Err("Source attachment root does not own the note".into());
}
let source_attachments = source_root.join(".helixnotes/attachments");
let raw = fs::read_to_string(&source).map_err(|error| error.to_string())?;
if raw != expected_raw {
return Err("The source note changed on disk. Reopen it before importing.".into());
}
let relative = notebook_relative.map(safe_relative_path).transpose()?;
let directory = Path::new(vault_path).join(relative.unwrap_or(Path::new("")));
ensure_vault_content_dir(vault_path, &directory)?;
let vault = canonicalize_path(Path::new(vault_path), "vault path")?;
let canonical_directory = canonicalize_path(&directory, "import destination")?;
if canonical_directory != vault.join(relative.unwrap_or(Path::new(""))) {
return Err("Import destination must not use symbolic links".into());
}
let attachments = helixnotes_dir(vault_path).join("attachments");
references.sort_by_key(|reference| reference.from);
let mut previous = 0;
let mut targets = Vec::new();
// Validate every range and permission before publishing any attachment.
for reference in &references {
if reference.from < previous
|| reference.from >= reference.to
|| raw.get(reference.from..reference.to).is_none()
{
return Err("Invalid attachment reference range".into());
}
previous = reference.to;
let (path, suffix) = reference
.href
.find(['#', '?'])
.map(|index| reference.href.split_at(index))
.unwrap_or((&reference.href, ""));
let decoded = urlencoding::decode(path)
.map_err(|error| error.to_string())?
.replace('\\', "/");
let path = Path::new(&decoded);
if decoded.is_empty()
|| decoded.contains(':')
|| path.is_absolute()
|| path
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| ext.eq_ignore_ascii_case("md"))
{
return Err("Import attachments must be relative non-Markdown files".into());
}
let base = if decoded.starts_with(".helixnotes/attachments/") || reference.pdf {
source_root.as_path()
} else {
source.parent().ok_or("Source note has no parent")?
};
let target = canonicalize_path(&base.join(path), "attachment")?;
authorize(&target)?;
let relative_target = if let Ok(relative) = target.strip_prefix(&source_attachments) {
relative
} else {
target.strip_prefix(source.parent().ok_or("Source note has no parent")?)
.map_err(|_| "Attachments must stay inside the source note's folder or managed attachment root")?
};
let components: Vec<_> = relative_target.components().collect();
let helix_attachment = components
.first()
.is_some_and(|part| part.as_os_str() == ".helixnotes")
&& components
.get(1)
.is_some_and(|part| part.as_os_str() == "attachments");
if components.iter().enumerate().any(|(index, part)| {
part.as_os_str().to_string_lossy().starts_with('.') && !(helix_attachment && index == 0)
}) {
return Err("Hidden metadata is not imported as an attachment".into());
}
if !target.is_file() {
return Err("Attachment reference must point to a file".into());
}
targets.push((target, suffix.to_string()));
}
if !references.is_empty() {
fs::create_dir_all(&attachments).map_err(|error| error.to_string())?;
if canonicalize_path(&attachments, "attachment destination")?
!= vault.join(".helixnotes/attachments")
{
return Err("Attachment destination must not use symbolic links".into());
}
}
let mut created = Vec::new();
let result = (|| {
let mut copied = std::collections::HashMap::<PathBuf, String>::new();
let mut rewritten = String::with_capacity(raw.len());
let mut previous = 0;
for (reference, (target, suffix)) in references.iter().zip(targets) {
let relative = if let Some(relative) = copied.get(&target) {
relative.clone()
} else {
let name = sanitize_filename(
&target
.file_name()
.ok_or("Attachment has no filename")?
.to_string_lossy(),
);
let mut temporary = tempfile::Builder::new()
.prefix(".helixnotes-import-")
.tempfile_in(&attachments)
.map_err(|error| error.to_string())?;
let mut file = fs::File::open(&target).map_err(|error| error.to_string())?;
let before = file.metadata().map_err(|error| error.to_string())?;
let bytes = std::io::copy(&mut file, temporary.as_file_mut())
.map_err(|error| error.to_string())?;
let after = file.metadata().map_err(|error| error.to_string())?;
if bytes != before.len()
|| before.len() != after.len()
|| before.modified().ok() != after.modified().ok()
{
return Err("An attachment changed while importing. Retry the import.".into());
}
temporary
.as_file()
.sync_all()
.map_err(|error| error.to_string())?;
let filename = loop {
let filename = format!("{}_{}", Uuid::new_v4(), name);
let destination = attachments.join(&filename);
match temporary.persist_noclobber(&destination) {
Ok(_) => {
created.push(destination);
break filename;
}
Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => {
temporary = error.file
}
Err(error) => return Err(error.error.to_string()),
}
};
let relative =
format!(".helixnotes/attachments/{}", urlencoding::encode(&filename));
copied.insert(target, relative.clone());
relative
};
// Use the same stable vault-root convention as normal attachment insertion/saving.
let mut href = format!("{relative}{suffix}");
if reference.html {
href = href
.replace('&', "&amp;")
.replace('"', "&quot;")
.replace('\'', "&#39;")
.replace('<', "&lt;")
.replace('>', "&gt;");
} else {
href = href
.replace(' ', "%20")
.replace('"', "%22")
.replace('<', "%3C")
.replace('>', "%3E");
}
rewritten.push_str(&raw[previous..reference.from]);
rewritten.push_str(&href);
previous = reference.to;
}
rewritten.push_str(&raw[previous..]);
let filename = source.file_name().ok_or("Source note has no filename")?;
let mut temporary = tempfile::Builder::new()
.prefix(".helixnotes-import-")
.tempfile_in(&directory)
.map_err(|error| error.to_string())?;
temporary
.write_all(rewritten.as_bytes())
.map_err(|error| error.to_string())?;
temporary
.as_file()
.sync_all()
.map_err(|error| error.to_string())?;
let mut destination = directory.join(filename);
let mut suffix = 2;
loop {
match temporary.persist_noclobber(&destination) {
Ok(_) => break,
Err(error)
if error.error.kind() == std::io::ErrorKind::AlreadyExists && suffix <= 100 =>
{
temporary = error.file;
destination = directory.join(format!(
"{} ({suffix}).md",
source.file_stem().unwrap_or_default().to_string_lossy()
));
suffix += 1;
}
Err(error) => return Err(error.error.to_string()),
}
}
let (meta, content) = frontmatter::parse_note(
&rewritten,
&destination
.file_name()
.unwrap_or_default()
.to_string_lossy(),
);
Ok(NoteContent {
path: destination.to_string_lossy().into_owned(),
meta,
content,
raw: rewritten,
})
})();
if let Err(error) = result {
let mut failed_cleanup = Vec::new();
for path in created {
if let Err(cleanup) = fs::remove_file(&path) {
if cleanup.kind() != std::io::ErrorKind::NotFound {
failed_cleanup.push(format!("{}: {cleanup}", path.display()));
}
}
}
if !failed_cleanup.is_empty() {
return Err(format!(
"{error}; could not remove unused attachments: {}",
failed_cleanup.join("; ")
));
}
return Err(error);
}
result
}
pub(crate) fn normalize_notebook_icon_key(path: &str) -> String {
path.replace('\\', "/")
}
@@ -2188,6 +2550,79 @@ pub fn sanitize_filename(name: &str) -> String {
.to_string()
}
#[cfg(test)]
mod network_path_tests {
use super::*;
#[test]
fn network_paths_are_recognized_by_their_text() {
for path in [
r"\\host\share\r.pdf",
"//host/share/r.pdf",
r"\\?\UNC\host\share\r.pdf",
r"\\?\unc\host\share",
"//?/UNC/host/share",
r"\/host\share",
] {
assert!(is_network_path(path), "{path}");
}
for path in [
r"C:\Users\me\note.md",
"C:/Users/me/note.md",
r"\\?\C:\Users\me\note.md",
r"\\.\C:\x",
"/home/me/note.md",
"relative/note.md",
r"folder\note.md",
"",
] {
assert!(!is_network_path(path), "{path}");
}
}
#[test]
fn only_network_paths_inside_known_folders_pass_on_windows() {
let roots = vec![
r"\\nas\notes\Vault".to_string(),
r"\\?\UNC\files\home\me\opened".to_string(),
r"C:\Users\me\Vault".to_string(),
];
let check = |path: &str| refuse_foreign_network_path_on(true, Path::new(path), &roots);
// Inside a vault or an opened folder, in any spelling.
for path in [
r"\\nas\notes\Vault\note.md",
"//NAS/notes/vault/sub/note.md",
r"\\?\UNC\nas\notes\Vault\.helixnotes\attachments\a.png",
r"\\files\home\me\opened\note.md",
r"\\nas\notes\Vault",
] {
assert!(check(path).is_ok(), "{path}");
}
// Another host, another share, a sibling folder, or a way back out.
for path in [
r"\\evil\share\r.pdf",
"//evil/share/r.pdf",
r"\\nas\other\x.md",
r"\\nas\notes\Vault2\x.md",
r"\\nas\notes\Vault\..\..\other\x.md",
r"\\?\UNC\evil\share\x.md",
] {
assert!(check(path).is_err(), "{path}");
}
// Local paths are not this check's business, and a local vault grants no network folder.
assert!(check(r"C:\Windows\system.ini").is_ok());
assert!(check("relative/x.md").is_ok());
assert!(refuse_foreign_network_path_on(
true,
Path::new(r"\\nas\x"),
&[r"C:\nas".to_string()]
)
.is_err());
// Elsewhere `//x/y` is an ordinary local path and nothing is refused.
assert!(refuse_foreign_network_path_on(false, Path::new("//home/me/x.md"), &[]).is_ok());
}
}
#[cfg(test)]
mod tests {
use super::{
@@ -2200,6 +2635,71 @@ mod tests {
use std::fs;
use std::io::Read;
#[test]
fn all_tags_excludes_only_root_metadata_subtree_and_hidden_filenames() {
let parent = tempfile::tempdir().unwrap();
let root = parent.path().join(".helixnotes");
let fixtures = [
("Root.md", "root"),
(".helixnotes/history/Stored.md", "excluded"),
("Notebook/.helixnotes/Nested.md", "nested"),
(".helixnotes-other/Visible.md", "similar"),
(".obsidian/Visible.md", "obsidian"),
("assets/Visible.md", "assets"),
(".other/Visible.md", "other"),
("Notebook/.Hidden.md", "hidden"),
("Notebook/Upper.MD", "uppercase"),
("Notebook/other.txt", "text"),
];
for (relative, tag) in fixtures {
let path = root.join(relative);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, format!("---\ntags: [{tag}]\n---\n# Note")).unwrap();
}
assert_eq!(
super::get_all_tags(root.to_str().unwrap()).unwrap(),
vec![
("assets".to_string(), 1),
("nested".to_string(), 1),
("obsidian".to_string(), 1),
("other".to_string(), 1),
("root".to_string(), 1),
("similar".to_string(), 1),
]
);
}
#[cfg(unix)]
#[test]
fn all_tags_keeps_file_symlinks_without_following_directory_symlinks() {
use std::os::unix::fs::symlink;
let vault = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
fs::write(
outside.path().join("External.md"),
"---\ntags: [linked]\n---\n# Note",
)
.unwrap();
symlink(
outside.path().join("External.md"),
vault.path().join("Linked.md"),
)
.unwrap();
symlink(outside.path(), vault.path().join("Directory")).unwrap();
fs::create_dir(vault.path().join(".helixnotes")).unwrap();
symlink(
outside.path().join("External.md"),
vault.path().join(".helixnotes/Excluded.md"),
)
.unwrap();
assert_eq!(
super::get_all_tags(vault.path().to_str().unwrap()).unwrap(),
vec![("linked".to_string(), 1)]
);
}
#[test]
fn same_named_attachment_writes_never_replace_each_other() {
let vault = tempfile::tempdir().unwrap();
@@ -2815,7 +3315,8 @@ mod tests {
assert_eq!(saved.meta.id, first.id);
assert_eq!(saved.meta.created, meta.created);
assert!(saved.content.contains("Second change."));
let parsed = gray_matter::Matter::<gray_matter::engine::YAML>::new().parse(&saved.raw);
let parsed =
gray_matter::Matter::<crate::vault::frontmatter::BoundedYaml>::new().parse(&saved.raw);
let fields: serde_yaml::Value = parsed.data.unwrap().deserialize().unwrap();
assert_eq!(fields["aliases"][0].as_str(), Some("Legacy"));
}
@@ -3248,6 +3749,114 @@ mod tests {
assert!(!outside.join("Stashed").exists());
}
#[test]
fn header_reads_keep_metadata_when_frontmatter_exceeds_the_first_block() {
let vault = tempfile::tempdir().unwrap();
let tags: Vec<String> = (0..120)
.map(|index| format!("long-tag-name-{index}"))
.collect();
let raw = format!(
"---\nid: existing\ntitle: Long Frontmatter\ntags: [{}]\n---\n# Long Frontmatter\n\nBody.\n",
tags.join(", ")
);
assert!(raw.find("\n---\n#").unwrap() > 2048);
fs::write(vault.path().join("Long.md"), raw).unwrap();
let entry =
super::read_note_entry_fast(&vault.path().join("Long.md"), vault.path()).unwrap();
assert_eq!(entry.meta.title, "Long Frontmatter");
assert_eq!(entry.meta.tags, tags);
let counted = super::get_all_tags(vault.path().to_str().unwrap()).unwrap();
assert_eq!(counted.len(), tags.len());
}
#[test]
fn note_head_stops_after_frontmatter_and_one_body_block() {
let vault = tempfile::tempdir().unwrap();
let path = vault.path().join("Note.md");
let tags = (0..400)
.map(|i| format!("tag-{i}"))
.collect::<Vec<_>>()
.join(", ");
for newline in ["\n", "\r\n"] {
let body = "x".repeat(100_000);
let raw = format!("---{newline}tags: [{tags}]{newline}---{newline}{body}");
fs::write(&path, &raw).unwrap();
let head = super::read_note_head(&path).unwrap();
let end = raw.find(&format!("{newline}---{newline}")).unwrap() + newline.len() * 2 + 3;
assert!(head.len() >= end + 2048 && head.len() < end + 2 * 2048);
assert!(raw.starts_with(&head));
}
}
#[test]
fn note_head_without_frontmatter_reads_one_block_and_unterminated_is_capped() {
let vault = tempfile::tempdir().unwrap();
let path = vault.path().join("Note.md");
fs::write(&path, "# Title\n".to_string() + &"y".repeat(50_000)).unwrap();
assert_eq!(super::read_note_head(&path).unwrap().len(), 2048);
fs::write(
&path,
"---\ntitle: Open\n".to_string() + &"z\n".repeat(300_000),
)
.unwrap();
assert_eq!(super::read_note_head(&path).unwrap().len(), 256 * 1024);
fs::write(
&path,
"---not frontmatter\n".to_string() + &"y".repeat(50_000),
)
.unwrap();
assert_eq!(super::read_note_head(&path).unwrap().len(), 2048);
fs::write(&path, "---\n---\nShort").unwrap();
assert_eq!(super::read_note_head(&path).unwrap(), "---\n---\nShort");
}
#[test]
fn dot_named_vault_folder_is_still_scanned_and_renames_update_links() {
// tempdir() names its folder ".tmpXXXX", like a vault kept in ~/.notes.
let vault = tempfile::tempdir().unwrap();
assert!(vault
.path()
.file_name()
.unwrap()
.to_string_lossy()
.starts_with('.'));
let vault_path = vault.path().to_str().unwrap();
fs::write(vault.path().join("Old.md"), "---\ntitle: Old\n---\n# Old\n").unwrap();
let other = vault.path().join("Other.md");
fs::write(&other, "---\ntitle: Other\n---\nSee [[Old]].\n").unwrap();
assert_eq!(super::scan_notes(vault_path, None).unwrap().len(), 2);
fs::create_dir(vault.path().join("Notebook")).unwrap();
fs::write(
vault.path().join("Notebook/Tagged.md"),
"---\ntags: [work]\n---\n# Tagged\n",
)
.unwrap();
assert_eq!(super::scan_notebooks(vault_path).unwrap().len(), 1);
assert_eq!(
super::get_all_tags(vault_path).unwrap(),
[("work".to_string(), 1)]
);
assert_eq!(
super::scan_notes(
vault_path,
Some(vault.path().join("Notebook").to_str().unwrap())
)
.unwrap()
.len(),
1
);
super::rename_note(
vault.path().join("Old.md").to_str().unwrap(),
"Renamed",
vault_path,
)
.unwrap();
assert!(fs::read_to_string(other).unwrap().contains("Renamed"));
}
#[test]
fn vault_state_save_replaces_file_without_truncating_open_reader() {
let vault = tempfile::tempdir().unwrap();
+78
View File
@@ -152,6 +152,65 @@
src: url("/fonts/open-sans/OpenSans-BoldItalic.woff2") format("woff2");
}
/* Ubuntu families - unmodified upstream fonts, Ubuntu Font Licence 1.0.
Copyright and licence notices are bundled beside each family's local assets. */
@font-face {
font-family: "Ubuntu";
font-style: normal;
font-weight: 400;
font-display: swap;
src: url("/fonts/ubuntu/Ubuntu-R.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu";
font-style: normal;
font-weight: 700;
font-display: swap;
src: url("/fonts/ubuntu/Ubuntu-B.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu";
font-style: italic;
font-weight: 400;
font-display: swap;
src: url("/fonts/ubuntu/Ubuntu-RI.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu";
font-style: italic;
font-weight: 700;
font-display: swap;
src: url("/fonts/ubuntu/Ubuntu-BI.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu Mono";
font-style: normal;
font-weight: 400;
font-display: swap;
src: url("/fonts/ubuntu-mono/UbuntuMono-R.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu Mono";
font-style: normal;
font-weight: 700;
font-display: swap;
src: url("/fonts/ubuntu-mono/UbuntuMono-B.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu Mono";
font-style: italic;
font-weight: 400;
font-display: swap;
src: url("/fonts/ubuntu-mono/UbuntuMono-RI.ttf") format("truetype");
}
@font-face {
font-family: "Ubuntu Mono";
font-style: italic;
font-weight: 700;
font-display: swap;
src: url("/fonts/ubuntu-mono/UbuntuMono-BI.ttf") format("truetype");
}
:root {
/* Light theme */
--bg-primary: #ffffff;
@@ -1232,6 +1291,25 @@ body.resizing {
page-break-inside: avoid;
}
/* Image grids print as compact 2-column rows without their editing controls. */
.ProseMirror .img-grid-header {
display: none !important;
}
.ProseMirror .img-grid,
.ProseMirror .img-grid[data-layout='masonry'] {
display: grid !important;
grid-template-columns: repeat(2, 1fr) !important;
columns: auto !important;
gap: 8px !important;
break-inside: avoid;
}
.ProseMirror .img-grid img {
margin: 0 !important;
width: 100% !important;
max-width: 100% !important;
break-inside: avoid;
}
/* Avoid breaking inside these elements */
h1,
h2,
+23 -2
View File
@@ -1,4 +1,5 @@
import { invoke } from "@tauri-apps/api/core";
import type { ExternalNoteReference } from "./editor/externalNote";
import type {
AppConfig,
CustomTheme,
@@ -79,8 +80,9 @@ export async function importCustomThemes(path: string): Promise<CustomTheme[]> {
return invoke("import_custom_themes", { path });
}
export async function setFontSize(size: number): Promise<void> {
return invoke("set_font_size", { size });
/** `preferred` marks a choice made in Appearance, which is what resetting the zoom returns to. */
export async function setFontSize(size: number, preferred = false): Promise<void> {
return invoke("set_font_size", { size, preferred });
}
export async function setScrollToChangeFontSize(enabled: boolean): Promise<void> {
@@ -163,6 +165,16 @@ export async function saveNote(
return invoke("save_note", { vaultPath, path, meta, body, expectedRaw });
}
export async function importNote(
vaultPath: string,
sourcePath: string,
notebookRelative: string | null,
expectedRaw: string,
references: ExternalNoteReference[],
): Promise<NoteContent> {
return invoke("import_note", { vaultPath, sourcePath, notebookRelative, expectedRaw, references });
}
export async function createNote(
vaultPath: string,
notebookRelative: string | null,
@@ -269,6 +281,15 @@ export async function readClipboardImage(): Promise<number[]> {
return invoke("read_clipboard_image");
}
export interface ClipboardContents {
text: string | null;
html: string | null;
}
export async function readClipboardContents(): Promise<ClipboardContents> {
return invoke("read_clipboard_contents");
}
export async function copyImageToClipboard(path: string): Promise<void> {
return invoke("copy_image_to_clipboard", { path });
}
+13 -3
View File
@@ -89,6 +89,13 @@
let sidebar = $state<Sidebar>();
let noteList = $state<NoteList>();
let editor = $state<Editor>();
let outlineOpen = $state(false);
let outlinePanelId = $state('');
function handleOutlineChange(open: boolean, panelId: string) {
outlineOpen = open;
outlinePanelId = panelId;
}
let pendingSearchReveal = $state<{ path: string; token: number } | null>(null);
let unlistenFileChange: (() => void) | null = null;
let unlistenClose: (() => void) | null = null;
@@ -979,7 +986,9 @@
if (!ownerIsCurrent()) return;
$sidebarWidth = state.sidebar_width;
$notelistWidth = state.notelist_width;
if (typeof state.outline_width === 'number') $outlineWidth = state.outline_width;
if (typeof state.outline_width === 'number' && Number.isFinite(state.outline_width)) {
$outlineWidth = Math.max(160, Math.min(500, state.outline_width));
}
$sidebarCollapsed = state.sidebar_collapsed;
$notelistCollapsed = state.notelist_collapsed ?? false;
$collapsedNotebooks = state.collapsed_notebooks ?? [];
@@ -1190,7 +1199,8 @@
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
</svg>
</button>
<button class="mobile-header-btn" onclick={() => editor?.toggleOutlinePanel()} title="Outline">
<button class="mobile-header-btn" class:active={outlineOpen} onclick={() => editor?.toggleOutlinePanel()}
title="Outline" aria-label="Outline" aria-expanded={outlineOpen} aria-controls={outlinePanelId || undefined}>
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="4" y1="6" x2="20" y2="6"/><line x1="8" y1="12" x2="20" y2="12"/><line x1="8" y1="18" x2="20" y2="18"/><circle cx="4" cy="12" r="1" fill="currentColor"/><circle cx="4" cy="18" r="1" fill="currentColor"/>
</svg>
@@ -1268,7 +1278,7 @@
<NoteList bind:this={noteList} onNoteSelected={handleNoteSelected} onBeforeNoteSwitch={() => editor?.flushSave() ?? Promise.resolve(true)} onBeforeNoteDuplicate={() => editor?.forceSave() ?? Promise.resolve(true)} onBeforePathMutation={() => editor?.beginPathMutation() ?? Promise.resolve(() => {})} onPathsMoved={handlePathsMoved} onUpdateActiveNote={updateActiveNoteMetadata} onTagsChanged={handleTagsChanged} onRefreshTags={() => { if (!isMobile) void sidebar?.refreshTags(); }} onNoteMoved={() => sidebar?.refresh()} onNoteCreated={(token) => { editor?.focusTitle(token); }} onToggleTask={toggleTask} onSetTaskPriority={changeTaskPriority} onSetTaskDue={changeTaskDue} />
</div>
<div class="mobile-panel" class:active={$mobileView === 'editor'}>
<Editor bind:this={editor} onMoveToTrash={trashOpenNote} />
<Editor bind:this={editor} onMoveToTrash={trashOpenNote} onOutlineChange={handleOutlineChange} />
</div>
</div>
File diff suppressed because it is too large. Load diff
+44 -60
View File
@@ -4,6 +4,7 @@
import { activeNotePath, appConfig } from '$lib/stores/app';
import { isMobile } from '$lib/platform';
import { applyGraphRepulsion } from '$lib/utils/graph-layout';
import { buildGraphTopology, getGraphNeighborhood } from '$lib/utils/graph-topology';
let { onclose, onnavigate }: {
onclose: () => void;
@@ -26,6 +27,7 @@
let nodeIndexMap: Map<string, number> = new Map();
let connectedSet: Set<number> = new Set();
let nodeDegree: number[] = [];
let incidentEdges = $state.raw<number[][]>([]);
let searchMatchSet = $state.raw<Set<number> | null>(null);
let folderColorMap = new Map<string, string>();
@@ -41,6 +43,8 @@
let hoveredNeighborSet: Set<number> = new Set();
let hoveredEdgeSet: Set<number> = new Set();
let activeNodeIdx = $state(-1);
// Svelte caches this until the active node or graph topology changes.
let localNeighborhood = $derived(getGraphNeighborhood(activeNodeIdx, edges, incidentEdges));
let navigatedFromGraph = false;
// Physics runs in short yielded batches; the completed graph is static.
@@ -49,6 +53,7 @@
const layoutChannel = new MessageChannel();
let layoutScheduled = false;
let disposed = false;
let drawFrame: number | null = null;
// DPI fix
let dpr = 1;
@@ -115,35 +120,9 @@
function updateHoveredState(node: GraphNode | null) {
hoveredNode = node;
hoveredIdx = node ? nodes.indexOf(node) : -1;
hoveredNeighborSet = new Set();
hoveredEdgeSet = new Set();
if (hoveredIdx < 0) return;
hoveredNeighborSet.add(hoveredIdx);
for (let i = 0; i < edges.length; i++) {
const e = edges[i];
if (e.sourceIdx === hoveredIdx || e.targetIdx === hoveredIdx) {
hoveredEdgeSet.add(i);
hoveredNeighborSet.add(e.sourceIdx);
hoveredNeighborSet.add(e.targetIdx);
}
}
}
// Nodes/edges visible in local mode (active note + direct neighbours)
function computeLocalSets(): { nodeSet: Set<number>; edgeSet: Set<number> } {
const nodeSet = new Set<number>();
const edgeSet = new Set<number>();
if (activeNodeIdx < 0) return { nodeSet, edgeSet };
nodeSet.add(activeNodeIdx);
for (let i = 0; i < edges.length; i++) {
const e = edges[i];
if (e.sourceIdx === activeNodeIdx || e.targetIdx === activeNodeIdx) {
edgeSet.add(i);
nodeSet.add(e.sourceIdx);
nodeSet.add(e.targetIdx);
}
}
return { nodeSet, edgeSet };
const neighborhood = getGraphNeighborhood(hoveredIdx, edges, incidentEdges);
hoveredNeighborSet = neighborhood.nodeSet;
hoveredEdgeSet = neighborhood.edgeSet;
}
function updateSearchMatch() {
@@ -175,15 +154,13 @@
};
});
connectedSet = new Set();
nodeDegree = new Array(nodes.length).fill(0);
edges = data.edges.map(e => {
connectedSet.add(e.source);
connectedSet.add(e.target);
nodeDegree[e.source] = (nodeDegree[e.source] || 0) + 1;
nodeDegree[e.target] = (nodeDegree[e.target] || 0) + 1;
return { sourceIdx: e.source, targetIdx: e.target, bidirectional: e.bidirectional };
});
edges = data.edges.map(e => ({
sourceIdx: e.source, targetIdx: e.target, bidirectional: e.bidirectional,
}));
const topology = buildGraphTopology(nodes.length, edges);
connectedSet = topology.connectedSet;
nodeDegree = topology.nodeDegree;
incidentEdges = topology.incidentEdges;
buildFolderColors();
updateSearchMatch();
@@ -196,15 +173,11 @@
function centerOnActiveNote() {
if (!canvas || nodes.length === 0 || activeNodeIdx < 0) return;
const activeNode = nodes[activeNodeIdx];
const neighborhood: GraphNode[] = [activeNode];
for (const edge of edges) {
if (edge.sourceIdx === activeNodeIdx) neighborhood.push(nodes[edge.targetIdx]);
else if (edge.targetIdx === activeNodeIdx) neighborhood.push(nodes[edge.sourceIdx]);
}
const neighborhood = localNeighborhood.nodeSet;
const w = canvasW, h = canvasH, padding = 100;
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
for (const n of neighborhood) {
for (const i of neighborhood) {
const n = nodes[i];
if (n.x < minX) minX = n.x; if (n.y < minY) minY = n.y;
if (n.x > maxX) maxX = n.x; if (n.y > maxY) maxY = n.y;
}
@@ -230,7 +203,7 @@
function focusActive() {
if (activeNodeIdx >= 0) centerOnActiveNote(); else fitToView();
draw();
requestDraw();
}
function startSimulation() {
@@ -239,7 +212,7 @@
physicsStep = null;
if (physicsRemaining === 0) {
loading = false;
draw();
requestDraw();
} else {
startLayoutLoop();
}
@@ -264,7 +237,7 @@
if (physicsRemaining === 0) {
loading = false;
if (activeNodeIdx >= 0) centerOnActiveNote(); else fitToView();
draw();
requestDraw();
} else {
layoutScheduled = true;
layoutChannel.port2.postMessage(null);
@@ -311,8 +284,16 @@
}
}
function requestDraw() {
if (disposed || drawFrame !== null) return;
drawFrame = requestAnimationFrame(() => {
drawFrame = null;
draw();
});
}
function draw() {
if (!canvas || loading) return;
if (disposed || !canvas || loading) return;
const ctx = canvas.getContext('2d');
if (!ctx) return;
@@ -328,7 +309,8 @@
const isSearching = searchMatchSet !== null;
// Local mode: restrict which nodes/edges are visible
const { nodeSet: localNodeSet, edgeSet: localEdgeSet } = localMode ? computeLocalSets() : { nodeSet: null as Set<number> | null, edgeSet: null as Set<number> | null };
const localNodeSet = localMode ? localNeighborhood.nodeSet : null;
const localEdgeSet = localMode ? localNeighborhood.edgeSet : null;
// ── Edges ──────────────────────────────────────────────────────────────
if (isHovering) {
@@ -594,17 +576,17 @@
dragging.x = (e.clientX - rect.left - pan.x) / zoom;
dragging.y = (e.clientY - rect.top - pan.y) / zoom;
dragging.vx = 0; dragging.vy = 0;
draw();
requestDraw();
} else if (panning) {
pan.x = e.clientX - panStart.x;
pan.y = e.clientY - panStart.y;
draw();
requestDraw();
} else if (!dragging) {
const node = getNodeAt(e.clientX, e.clientY);
if (node !== hoveredNode) {
updateHoveredState(node);
if (canvas) canvas.style.cursor = node ? 'pointer' : 'grab';
if (!layoutScheduled) draw();
if (!layoutScheduled) requestDraw();
}
}
}
@@ -629,7 +611,7 @@
zoom = Math.max(0.2, Math.min(5, zoom * (e.deltaY > 0 ? 0.9 : 1.1)));
pan.x = mx - (mx - pan.x) * (zoom / oldZoom);
pan.y = my - (my - pan.y) * (zoom / oldZoom);
draw();
requestDraw();
}
function handleKeydown(e: KeyboardEvent) {
@@ -668,11 +650,11 @@
dragging.x = (t.clientX - rect.left - pan.x) / zoom;
dragging.y = (t.clientY - rect.top - pan.y) / zoom;
dragging.vx = 0; dragging.vy = 0;
draw();
requestDraw();
} else if (panning) {
pan.x = t.clientX - panStart.x;
pan.y = t.clientY - panStart.y;
draw();
requestDraw();
}
} else if (e.touches.length === 2) {
const dx = e.touches[0].clientX - e.touches[1].clientX;
@@ -687,7 +669,7 @@
zoom = Math.max(0.2, Math.min(5, zoom * (dist / lastTouchDist)));
pan.x = mx - (mx - pan.x) * (zoom / oldZoom);
pan.y = my - (my - pan.y) * (zoom / oldZoom);
draw();
requestDraw();
}
lastTouchDist = dist;
}
@@ -730,18 +712,20 @@
navigatedFromGraph = false;
centerOnActiveNote();
}
if (!layoutScheduled && canvas) untrack(draw);
if (!layoutScheduled && canvas) untrack(requestDraw);
});
// React to search query changes
$effect(() => {
const _q = searchQuery; // track
updateSearchMatch();
if (canvas && !layoutScheduled) untrack(draw);
if (canvas && !layoutScheduled) untrack(requestDraw);
});
onDestroy(() => {
disposed = true;
if (drawFrame !== null) cancelAnimationFrame(drawFrame);
drawFrame = null;
layoutChannel.port1.onmessage = null;
layoutChannel.port1.close();
layoutChannel.port2.close();
@@ -775,7 +759,7 @@
<div class="graph-stats">
{#if !loading}
{#if localMode && activeNodeIdx >= 0}
{computeLocalSets().nodeSet.size} notes · {computeLocalSets().edgeSet.size} links
{localNeighborhood.nodeSet.size} notes · {localNeighborhood.edgeSet.size} links
{:else}
{nodes.length} notes · {edges.length} links
{/if}
+189
View File
@@ -0,0 +1,189 @@
<script lang="ts">
import { tick } from 'svelte';
import SnapshotDocument from './SnapshotDocument.svelte';
import { comparableBody, compareHistory, splitSnapshot, type HistoryComparison, type HistoryRow } from '$lib/utils/history-comparison';
let { raw, current, selected, loading, error, canRestore, onRetry, onRestore, renderSnapshot }: {
raw: string | null;
current: string;
selected: string | null;
loading: boolean;
error: string | null;
canRestore: boolean;
onRetry: () => void;
onRestore: () => void;
renderSnapshot: (body: string) => string;
} = $props();
let view = $state<'changes' | 'snapshot'>('changes');
let source = $state(false);
let comparison = $state<HistoryComparison | null>(null);
let comparing = $state(false);
let comparisonError = $state<string | null>(null);
let request = 0;
const snapshot = $derived(splitSnapshot(raw ?? ''));
const rendered = $derived.by(() => {
if (view !== 'snapshot' || source || raw === null) return { html: '', error: '' };
try {
return { html: renderSnapshot(snapshot.body), error: '' };
} catch (cause) {
return { html: '', error: `Could not render this snapshot: ${String(cause)}` };
}
});
$effect(() => {
const content = raw;
const working = current;
const body = snapshot.body;
const generation = ++request;
comparison = null;
comparisonError = null;
comparing = content !== null;
if (content !== null) {
compareHistory(comparableBody(working), comparableBody(body)).then((result) => {
if (generation !== request) return;
comparison = result;
}).catch((cause) => {
if (generation === request) comparisonError = `Could not compare this version: ${String(cause)}`;
}).finally(() => {
if (generation === request) comparing = false;
});
}
return () => { request++; };
});
async function navigateTabs(event: KeyboardEvent) {
if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
event.preventDefault();
view = event.key === 'Home' ? 'changes' : event.key === 'End' ? 'snapshot' : view === 'changes' ? 'snapshot' : 'changes';
await tick();
document.getElementById(`history-tab-${view}`)?.focus();
}
</script>
{#snippet lines(rows: HistoryRow[])}
{#each rows as row}
<div class="diff-line {row.kind}">
<span class="line-number" aria-label="Current note line">{row.currentLine ?? ''}</span>
<span class="line-number" aria-label="Snapshot line">{row.snapshotLine ?? ''}</span>
<span class="line-sign" aria-label={row.kind === 'added' ? 'Added by restoring' : row.kind === 'removed' ? 'Removed by restoring' : 'Unchanged'}>{row.kind === 'added' ? '+' : row.kind === 'removed' ? '−' : ' '}</span>
<span class="line-text">{row.text || ' '}{#if !row.newline}<span class="line-ending">No final newline</span>{/if}</span>
</div>
{/each}
{/snippet}
<section class="history-review" aria-label="Version review">
<div class="review-header">
<div class="review-heading">
<strong>{selected ?? 'Version History'}</strong>
<span>Read-only. Your current note stays unchanged.</span>
</div>
<div class="review-tabs" role="tablist" aria-label="Version view" tabindex="-1" onkeydown={navigateTabs}>
<button id="history-tab-changes" role="tab" aria-selected={view === 'changes'} aria-controls="history-changes" tabindex={view === 'changes' ? 0 : -1} onclick={() => view = 'changes'}>Changes</button>
<button id="history-tab-snapshot" role="tab" aria-selected={view === 'snapshot'} aria-controls="history-snapshot" tabindex={view === 'snapshot' ? 0 : -1} onclick={() => view = 'snapshot'}>Snapshot</button>
</div>
</div>
{#if loading}
<p class="review-status" role="status">Loading version…</p>
{:else if error}
<div class="review-status" role="alert"><p>{error}</p><button class="review-button" onclick={onRetry}>Retry</button></div>
{:else if raw === null}
<p class="review-status">Select a version to review.</p>
{:else if view === 'changes'}
<div id="history-changes" class="review-panel" role="tabpanel" aria-labelledby="history-tab-changes" tabindex="0">
<div class="comparison-heading">
<span>Current note → selected version</span>
{#if comparison}<span class="comparison-count"><span class="added-count">+{comparison.added}</span> <span class="removed-count">−{comparison.removed}</span> lines</span>{/if}
</div>
{#if comparing}
<p class="review-status" role="status">Comparing…</p>
{:else if comparisonError}
<p class="review-status" role="alert">{comparisonError}</p>
{:else if comparison && comparison.added === 0 && comparison.removed === 0}
<p class="review-status">No content changes. Metadata is not restored.</p>
{:else if comparison}
<div class="history-diff">
{#key raw}
{#each comparison.chunks as chunk}
{#if chunk.kind === 'unchanged'}
<details class="unchanged-lines"><summary>{chunk.rows.length} unchanged {chunk.rows.length === 1 ? 'line' : 'lines'}</summary>{@render lines(chunk.rows)}</details>
{:else}
{@render lines(chunk.rows)}
{/if}
{/each}
{/key}
</div>
{/if}
</div>
{:else}
<div id="history-snapshot" class="review-panel" role="tabpanel" aria-labelledby="history-tab-snapshot" tabindex="0">
<div class="snapshot-heading"><span>Selected snapshot</span><button class="source-toggle" aria-pressed={source} onclick={() => source = !source}>{source ? 'Rendered view' : 'View source'}</button></div>
{#if snapshot.body === ''}
<p class="review-status">This snapshot is empty.</p>
{:else if source}
<textarea class="snapshot-source" readonly aria-label="Snapshot Markdown, read-only" value={snapshot.body}></textarea>
{:else if rendered.error}
<p class="review-status" role="alert">{rendered.error} View source to inspect the stored body.</p>
{:else}
<SnapshotDocument html={rendered.html} />
{/if}
</div>
{/if}
{#if raw !== null && snapshot.metadata}
<details class="history-metadata"><summary>Metadata (not restored)</summary><pre>{snapshot.metadata}</pre></details>
{/if}
{#if selected}
<div class="review-actions"><span>Restore replaces the current note’s content only.</span><button class="history-restore-btn" disabled={!canRestore} onclick={onRestore}>Restore this version</button></div>
{/if}
</section>
<style>
.history-review { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; background: var(--bg-primary); }
.review-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border-light); flex-shrink: 0; flex-wrap: wrap; }
.review-heading { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.review-heading strong { font-size: 13px; overflow-wrap: anywhere; }
.review-heading > span { font-size: 12px; color: var(--text-secondary); }
.review-tabs { display: flex; gap: 2px; padding: 2px; border: 1px solid var(--border-color); border-radius: 6px; }
.review-tabs button, .source-toggle, .review-button { padding: 6px 10px; border: none; border-radius: 4px; background: transparent; color: var(--text-secondary); font-size: 12px; cursor: pointer; }
.review-tabs button[aria-selected="true"] { background: var(--accent-light); color: var(--accent); }
.review-tabs button:focus-visible, .source-toggle:focus-visible, .review-button:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.review-panel { flex: 1; min-height: 0; overflow: auto; outline-offset: -2px; }
.review-panel:focus-visible { outline: 2px solid var(--accent); }
.comparison-heading, .snapshot-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 16px; font-size: 12px; color: var(--text-secondary); border-bottom: 1px solid var(--border-light); flex-wrap: wrap; }
.comparison-count { font-family: var(--font-mono, monospace); }
.added-count { color: var(--success); } .removed-count { color: var(--danger, #dc2626); }
.review-status { margin: 0; padding: 20px 16px; color: var(--text-secondary); font-size: 13px; line-height: 1.6; overflow-wrap: anywhere; }
.review-button { margin-top: 8px; border: 1px solid var(--border-color); background: var(--bg-secondary); color: var(--text-primary); }
.history-diff { padding: 8px 0; font-family: var(--font-mono, monospace); font-size: 12px; line-height: 1.6; }
.diff-line { display: grid; grid-template-columns: 3.5em 3.5em 1.6em minmax(0, 1fr); align-items: start; padding: 1px 12px 1px 4px; }
.diff-line.added { background: color-mix(in srgb, var(--success) 12%, var(--bg-primary)); }
.diff-line.removed { background: color-mix(in srgb, var(--danger, #dc2626) 12%, var(--bg-primary)); }
.line-number { padding-right: 8px; text-align: right; color: var(--text-tertiary); user-select: none; font-size: 10px; }
.line-sign { color: var(--text-secondary); user-select: none; }
.added .line-sign { color: var(--success); } .removed .line-sign { color: var(--danger, #dc2626); }
.line-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.line-ending { display: block; color: var(--text-secondary); font-style: italic; font-size: 10px; }
.unchanged-lines > summary { padding: 7px 16px; color: var(--text-secondary); background: var(--bg-secondary); cursor: pointer; font-family: var(--font-sans, inherit); font-size: 12px; }
.snapshot-source { display: block; width: 100%; min-height: 100%; margin: 0; padding: 16px; resize: none; border: none; background: var(--bg-primary); color: var(--text-primary); font-family: var(--font-mono, monospace); font-size: 13px; line-height: 1.6; }
.snapshot-source:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.history-metadata { flex-shrink: 0; border-top: 1px solid var(--border-light); font-size: 12px; color: var(--text-secondary); max-height: 30%; overflow: auto; }
.history-metadata > summary { cursor: pointer; padding: 8px 16px; }
.history-metadata pre { margin: 0; padding: 0 16px 12px; font-family: var(--font-mono, monospace); font-size: 11px; white-space: pre-wrap; overflow-wrap: anywhere; }
.review-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--border-light); background: var(--bg-secondary); flex-shrink: 0; flex-wrap: wrap; }
.review-actions > span { font-size: 11px; color: var(--text-secondary); }
.history-restore-btn { padding: 8px 14px; border: 1px solid var(--accent); border-radius: 6px; color: var(--accent); background: var(--accent-light); font-size: 12px; font-weight: 500; cursor: pointer; }
.history-restore-btn:hover:not(:disabled) { background: var(--accent); color: var(--bg-primary); }
.history-restore-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.history-restore-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (max-width: 600px) {
.review-header { padding: 10px 12px; }
.review-tabs { width: 100%; }
.review-tabs button { flex: 1; min-height: 44px; }
.source-toggle, .review-button, .history-restore-btn { min-height: 44px; }
.diff-line { grid-template-columns: 2.8em 2.8em 1.4em minmax(0, 1fr); padding-right: 8px; }
.review-actions { padding: 10px 12px; padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
.review-actions > span, .history-restore-btn { width: 100%; }
}
</style>
+32 -9
View File
@@ -631,12 +631,15 @@
$notes = [entry, ...$notes];
onNoteMoved();
if (!navigationIsCurrent(token)) return;
// The first save must use the exact file created on disk, not wait for a watcher echo.
const content = await readNote(ownerVault!, entry.path);
if (!navigationIsCurrent(token)) return;
if (!(await onBeforeNoteSwitch())) return;
if (!navigationIsCurrent(token)) return;
$activeNote = { path: entry.path, meta: entry.meta, content: '\n', raw: '' };
$activeNote = content;
$activeNotePath = entry.path;
$editorDirty = false;
onNoteSelected(entry.path, '\n');
onNoteSelected(entry.path, content.content);
onNoteCreated(token);
} catch (e) {
console.error('Failed to create note:', e);
@@ -1466,6 +1469,7 @@
class:qa-drag-below={$viewMode === 'quickaccess' && qaDragOver === noteIndex && qaDragFrom !== null && qaDragHalf === 'bottom'}
class:note-drag-above={canCustomReorder && noteDragOver === noteIndex && noteDragFrom !== null && noteDragHalf === 'top'}
class:note-drag-below={canCustomReorder && noteDragOver === noteIndex && noteDragFrom !== null && noteDragHalf === 'bottom'}
title={$viewMode === 'quickaccess' ? note.path : (getNotebookPath(note) ? `${getNotebookPath(note)}/${note.meta.title}` : note.meta.title)}
onclick={(e) => handleNoteClick(e, note)}
onkeydown={(e) => {
if (e.key === 'F2') {
@@ -1542,7 +1546,7 @@
ondragend={() => { qaDragFrom = null; qaDragOver = null; resetNoteDrag(); }}
>
{#if compact}
<div class="note-compact-row" title={getNotebookPath(note) ? `${getNotebookPath(note)}/${note.meta.title}` : note.meta.title}>
<div class="note-compact-row">
<span class="note-title">
{#if note.meta.pinned}
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="pin-icon">
@@ -1562,7 +1566,7 @@
{#if showDates}<span class="note-date-compact" title={`Created ${formatDate(note.meta.created)}\nModified ${formatDate(note.meta.modified)}`}>{formatRelativeTime($sortMode === 'created' ? note.meta.created : note.meta.modified)}</span>{/if}
</div>
{:else}
<div class="note-title" title={getNotebookPath(note) ? `${getNotebookPath(note)}/${note.meta.title}` : note.meta.title}>
<div class="note-title">
{#if note.meta.pinned}
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="pin-icon">
<path d="M12 17v5"/><path d="M9 2h6l-1 7h4l-2 4H8l-2-4h4L9 2z"/>
@@ -1577,11 +1581,15 @@
</div>
<div class="note-preview">{note.preview}</div>
<div class="note-meta">
{#if getNotebookPath(note)}
<span class="note-notebook">{getNotebookPath(note)}</span>
{#if showDates}<span class="note-meta-sep">&middot;</span>{/if}
{#if getNotebookPath(note) || showDates}
<span class="note-context">
{#if getNotebookPath(note)}
<span class="note-notebook">{getNotebookPath(note)}</span>
{#if showDates}<span class="note-meta-sep">&middot;</span>{/if}
{/if}
{#if showDates}<span class="note-date" title={`Created ${formatDate(note.meta.created)}\nModified ${formatDate(note.meta.modified)}`}>{formatRelativeTime($sortMode === 'created' ? note.meta.created : note.meta.modified)}</span>{/if}
</span>
{/if}
{#if showDates}<span class="note-date" title={`Created ${formatDate(note.meta.created)}\nModified ${formatDate(note.meta.modified)}`}>{formatRelativeTime($sortMode === 'created' ? note.meta.created : note.meta.modified)}</span>{/if}
{#if note.meta.tags.length > 0}
<span class="note-tags">
{#each note.meta.tags.slice(0, 3) as tag, i (tagIterationKey(tag, i))}
@@ -2198,9 +2206,18 @@
.note-meta {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 4px 8px;
margin-top: 4px;
}
.note-context {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 4px;
min-width: 0;
max-width: 100%;
}
.note-notebook {
@@ -2215,6 +2232,7 @@
.note-meta-sep {
color: var(--text-tertiary);
font-size: 0.78em;
flex-shrink: 0;
}
.note-compact-row .note-notebook {
@@ -2226,10 +2244,15 @@
.note-date {
font-size: 0.78em;
color: var(--text-tertiary);
flex-shrink: 0;
white-space: nowrap;
}
.note-tags {
display: flex;
flex-wrap: wrap;
min-width: 0;
max-width: 100%;
gap: 4px;
}
+1 -1
View File
@@ -253,7 +253,7 @@
<section class="switcher-section" aria-labelledby={`note-switcher-${section.label.replace(' ', '-').toLowerCase()}`}>
<h2 id={`note-switcher-${section.label.replace(' ', '-').toLowerCase()}`}>{section.label}</h2>
{#each section.rows as row (row.path)}
<div class="note-row" class:current-row={row.current}>
<div class="note-row" class:current-row={row.current} title={row.path}>
{#if row.current}<span class="current-marker" aria-hidden="true"></span>{/if}
<button
class="note-primary"
+31 -6
View File
@@ -1,5 +1,14 @@
<script lang="ts">
let { onResize }: { onResize: (delta: number) => void } = $props();
import { onDestroy } from 'svelte';
let { onResize, label, value, min, max, controls }: {
onResize: (delta: number) => void;
label?: string;
value?: number;
min?: number;
max?: number;
controls?: string;
} = $props();
let active = $state(false);
let startX = 0;
let rafId = 0;
@@ -26,15 +35,31 @@
}
}
function onMouseUp() {
function stopResize(flush: boolean) {
if (active) document.body.classList.remove('resizing');
active = false;
document.body.classList.remove('resizing');
if (rafId) { cancelAnimationFrame(rafId); rafId = 0; }
if (pendingDelta) { onResize(pendingDelta); pendingDelta = 0; }
if (flush && pendingDelta) onResize(pendingDelta);
pendingDelta = 0;
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', onMouseUp);
}
function onMouseUp() { stopResize(true); }
function onKeyDown(event: KeyboardEvent) {
if (value === undefined || (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight')) return;
event.preventDefault();
onResize((event.key === 'ArrowLeft' ? -1 : 1) * (event.shiftKey ? 32 : 16));
}
onDestroy(() => stopResize(false));
</script>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div class="resize-handle" class:active onmousedown={onMouseDown}></div>
<!-- A separator with a supplied range is an interactive resize control. -->
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div class="resize-handle" class:active onmousedown={onMouseDown} onkeydown={onKeyDown}
role={value === undefined ? 'presentation' : 'separator'} tabindex={value === undefined ? undefined : 0}
aria-label={label} aria-orientation={value === undefined ? undefined : 'vertical'}
aria-valuenow={value} aria-valuemin={min} aria-valuemax={max} aria-controls={controls}
title={value === undefined ? undefined : 'Use Left and Right arrow keys to resize'}></div>
+9 -4
View File
@@ -667,6 +667,8 @@
{ name: 'Open Sans', value: 'opensans', stack: '"Open Sans", -apple-system, sans-serif' },
{ name: 'Literata', value: 'literata', stack: '"Literata", Georgia, serif' },
{ name: 'Mono', value: 'mono', stack: '"JetBrains Mono", "Fira Code", "Cascadia Code", monospace' },
{ name: 'Ubuntu', value: 'ubuntu', stack: '"Ubuntu", -apple-system, sans-serif' },
{ name: 'Ubuntu Mono', value: 'ubuntu-mono', stack: '"Ubuntu Mono", monospace' },
];
const lineHeightPresets = [
@@ -1097,8 +1099,11 @@
function selectFontSize(size: number) {
activeFontSize = size;
applyFontSize(size);
if ($appConfig) $appConfig.font_size = size;
setFontSize(size).catch((e) => console.error('Failed to save font size:', e));
if ($appConfig) {
$appConfig.font_size = size;
$appConfig.preferred_font_size = size;
}
setFontSize(size, true).catch((e) => console.error('Failed to save font size:', e));
}
function toggleScrollToChangeFontSize() {
@@ -1496,7 +1501,7 @@
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z"/>
</svg>
Switch Vault
Manage Vaults
</button>
</div>
{/if}
@@ -2691,7 +2696,7 @@
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/>
</svg>
Download from Codeberg
Download
</a>
{/if}
</div>
+446
View File
@@ -0,0 +1,446 @@
<script lang="ts" module>
let diagramId = 0;
// Mermaid has a shared configuration; serialize snapshot renders and restore it afterward.
let diagramQueue = Promise.resolve();
</script>
<script lang="ts">
import createDOMPurify from 'dompurify';
import katex from 'katex';
import 'katex/dist/katex.min.css';
import { calloutGroup, calloutIcon, calloutLabel } from '$lib/editor/callouts';
import { readSecretTitle } from '$lib/utils/secrets';
let { html, label = 'Snapshot preview' }: { html: string; label?: string } = $props();
let root = $state<HTMLElement>();
let purifier: ReturnType<typeof createDOMPurify>;
const documentTags = [
'a', 'abbr', 'b', 'blockquote', 'br', 'caption', 'code', 'col', 'colgroup',
'dd', 'del', 'details', 'div', 'dl', 'dt', 'em', 'figcaption', 'figure',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'kbd', 'li',
'mark', 'ol', 'p', 'pre', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'small',
'span', 'strong', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'tfoot',
'th', 'thead', 'tr', 'u', 'ul', 'var', 'wbr',
];
const documentAttributes = [
'alt', 'class', 'colspan', 'dir', 'height', 'href', 'lang', 'open', 'reversed',
'rowspan', 'scope', 'span', 'src', 'start', 'style', 'title', 'value', 'width',
'data-type', 'data-checked', 'data-size', 'data-language', 'data-secret-block',
'data-math-inline', 'data-math-block', 'data-callout', 'data-callout-title',
'data-callout-foldable', 'data-callout-folded',
'data-wiki-link', 'data-path', 'data-title', 'data-aliased', 'data-bg-color',
'data-pdf-src', 'data-pdf-name', 'data-page-break',
];
// Custom asset protocols are accepted only here, then checked specifically on images.
const documentUris = /^(?:https?:|mailto:|tel:|asset:|imgproxy:|blob:|data:image\/(?:png|jpeg|gif|webp|avif|bmp);|#)/i;
const formattingProperties = [
'color', 'background-color', 'font-weight', 'font-style', 'font-size',
'text-align', 'text-decoration-line', 'text-decoration-color', 'vertical-align',
];
function message(text: string): HTMLParagraphElement {
const node = document.createElement('p');
node.className = 'snapshot-message';
node.textContent = text;
return node;
}
function imageSourceAllowed(source: string): boolean {
if (/[\u0000-\u0020\u007f]/.test(source)) return false;
if (/^(?:asset|imgproxy):\/\//i.test(source)) return true;
if (/^data:image\/(?:png|jpeg|gif|webp|avif|bmp);base64,[a-z\d+/=]+$/i.test(source)) return true;
try {
const url = new URL(source);
if (url.protocol === 'blob:') return new URL(url.pathname).origin === window.location.origin;
return ['http:', 'https:'].includes(url.protocol)
&& ['asset.localhost', 'imgproxy.localhost'].includes(url.hostname)
&& !url.username && !url.password && !url.port;
} catch {
return false;
}
}
function keepFormatting(element: HTMLElement): void {
const style = element.style;
const values = formattingProperties.map((property) => [property, style.getPropertyValue(property)]);
element.removeAttribute('style');
for (const [property, value] of values) {
// Only presentation properties; no URL-bearing CSS, variables, escapes or overlays.
if (value && !/url\s*\(|var\s*\(|expression\s*\(|[\\@]/i.test(value)) {
element.style.setProperty(property, value);
}
}
}
function prepareDocument(fragment: DocumentFragment): void {
for (const element of fragment.querySelectorAll<HTMLElement>('*')) {
keepFormatting(element);
const language = element.tagName === 'CODE'
? Array.from(element.classList).find((name) => /^language-[\w+-]+$/.test(name))
: undefined;
element.removeAttribute('class');
if (language) element.className = language;
if (element.hasAttribute('data-wiki-link')) {
element.className = 'snapshot-wiki-link';
element.title = `Wiki link to ${element.getAttribute('data-title') || element.textContent || ''} (inactive in snapshot preview)`;
element.setAttribute('aria-disabled', 'true');
}
if (element.hasAttribute('data-page-break')) element.className = 'snapshot-page-break';
// Snapshot links are readable but inert: inspection cannot navigate the live note.
if (element.tagName === 'A') {
const href = element.getAttribute('href');
if (href) element.title = `${element.title ? element.title + ' — ' : ''}${href} (inactive in snapshot preview)`;
element.removeAttribute('href');
element.setAttribute('aria-disabled', 'true');
}
}
for (const attachment of fragment.querySelectorAll<HTMLElement>('[data-pdf-src]')) {
const name = attachment.getAttribute('data-pdf-name') || 'PDF attachment';
const source = attachment.getAttribute('data-pdf-src') || '';
attachment.prepend(message(`${name} — PDF attachment (inline PDF rendering is unavailable in snapshot previews). ${source}`));
}
for (const image of fragment.querySelectorAll<HTMLImageElement>('img')) {
const source = image.getAttribute('src') || '';
if (!imageSourceAllowed(source)) {
image.replaceWith(message(`Image unavailable or blocked${image.alt ? ': ' + image.alt : '.'}`));
continue;
}
image.crossOrigin = 'anonymous';
image.loading = 'lazy';
image.decoding = 'async';
image.addEventListener('error', () => {
image.replaceWith(message(`Image could not be loaded: ${image.alt || source}`));
}, { once: true });
}
for (const item of fragment.querySelectorAll<HTMLElement>('li[data-type="taskItem"]')) {
const body = document.createElement('div');
while (item.firstChild) body.append(item.firstChild);
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = item.getAttribute('data-checked') === 'true';
checkbox.disabled = true;
checkbox.setAttribute('aria-label', checkbox.checked ? 'Completed task' : 'Incomplete task');
item.className = 'snapshot-task';
item.append(checkbox, body);
}
for (const table of fragment.querySelectorAll('table')) {
const scroll = document.createElement('div');
scroll.className = 'snapshot-table';
scroll.tabIndex = 0;
scroll.setAttribute('role', 'region');
scroll.setAttribute('aria-label', 'Snapshot table');
table.replaceWith(scroll);
scroll.append(table);
}
}
function renderSecrets(fragment: DocumentFragment): void {
for (const node of fragment.querySelectorAll<HTMLElement>('[data-secret-block]')) {
const stored = node.getAttribute('data-secret-block') || '';
node.replaceChildren();
node.className = 'snapshot-secret';
const title = document.createElement('strong');
let payload = stored;
let error = '';
try {
payload = decodeURIComponent(stored);
JSON.parse(payload);
} catch {
error = 'The stored secret payload is malformed; it cannot be previewed.';
}
title.textContent = readSecretTitle(payload);
node.append(title, message('Locked encrypted content. Secrets are not unlocked in history previews.'));
if (error) {
const details = document.createElement('details');
const summary = document.createElement('summary');
summary.textContent = 'Stored secret payload';
const code = document.createElement('pre');
code.textContent = payload;
details.append(summary, code);
node.append(message(error), details);
}
// Do not retain ciphertext in the rendered DOM unless needed for a readable error.
node.removeAttribute('data-secret-block');
}
}
function renderCallouts(fragment: DocumentFragment): void {
// Innermost first so nested callout bodies keep their independently rendered headers.
for (const node of Array.from(fragment.querySelectorAll<HTMLElement>('[data-callout]')).reverse()) {
const type = node.getAttribute('data-callout') || 'note';
const title = node.getAttribute('data-callout-title') || calloutLabel(type);
const foldable = node.getAttribute('data-callout-foldable') === 'true';
const wrapper = document.createElement(foldable ? 'details' : 'div');
wrapper.className = 'snapshot-callout';
const group = calloutGroup(type);
wrapper.style.setProperty('--snapshot-callout-color', `var(--callout-${typeof group === 'string' ? group : 'custom'})`);
if (wrapper instanceof HTMLDetailsElement) wrapper.open = node.getAttribute('data-callout-folded') !== 'true';
const header = document.createElement(foldable ? 'summary' : 'div');
header.className = 'snapshot-callout-header';
const icon = document.createElement('span');
icon.setAttribute('aria-hidden', 'true');
icon.append(purifier.sanitize(calloutIcon(type), { USE_PROFILES: { svg: true }, RETURN_DOM_FRAGMENT: true }));
const heading = document.createElement('strong');
heading.textContent = title;
header.append(icon, heading);
if (title !== calloutLabel(type)) {
const kind = document.createElement('small');
kind.textContent = calloutLabel(type);
header.append(kind);
}
const body = document.createElement('div');
body.className = 'snapshot-callout-body';
while (node.firstChild) body.append(node.firstChild);
wrapper.append(header, body);
node.replaceWith(wrapper);
}
}
function renderMath(fragment: DocumentFragment): void {
for (const node of fragment.querySelectorAll<HTMLElement>('[data-math-inline], [data-math-block]')) {
const block = node.hasAttribute('data-math-block');
const stored = node.getAttribute(block ? 'data-math-block' : 'data-math-inline') || '';
let tex = stored;
node.className = block ? 'snapshot-math-block' : 'snapshot-math-inline';
try {
tex = decodeURIComponent(stored);
const rendered = katex.renderToString(tex, { displayMode: block, trust: false, throwOnError: true });
node.replaceChildren(purifier.sanitize(rendered, {
USE_PROFILES: { html: true, mathMl: true, svg: true },
FORBID_TAGS: ['style', 'a', 'img', 'image', 'foreignobject'],
FORBID_ATTR: ['href', 'xlink:href', 'src'],
RETURN_DOM_FRAGMENT: true,
}));
} catch (error) {
const code = document.createElement('code');
code.textContent = tex;
const status = document.createElement('span');
status.className = 'snapshot-message';
status.textContent = ` Math could not be rendered: ${error instanceof Error ? error.message : String(error)}`;
node.replaceChildren(code, status);
}
node.removeAttribute('data-math-inline');
node.removeAttribute('data-math-block');
}
}
function prepareDiagrams(fragment: DocumentFragment, isCurrent: () => boolean): void {
for (const code of fragment.querySelectorAll<HTMLElement>('pre > code.language-mermaid, pre[data-language="mermaid"] > code')) {
const source = code.textContent || '';
const controls = document.createElement('div');
controls.className = 'snapshot-diagram';
const button = document.createElement('button');
button.type = 'button';
button.textContent = '▶ Render diagram';
const status = document.createElement('span');
status.className = 'snapshot-message';
status.setAttribute('role', 'status');
const output = document.createElement('div');
output.className = 'snapshot-diagram-output';
controls.append(button, status, output);
code.parentElement!.after(controls);
button.addEventListener('click', async () => {
button.disabled = true;
status.textContent = 'Rendering diagram…';
let staging: HTMLDivElement | undefined;
let restoreConfig: (() => void) | undefined;
let release!: () => void;
const previousRender = diagramQueue;
diagramQueue = new Promise<void>((resolve) => { release = resolve; });
await previousRender;
try {
const { default: mermaid } = await import('mermaid');
if (!isCurrent() || !controls.isConnected) return;
const previousConfig = mermaid.mermaidAPI.getConfig();
restoreConfig = () => mermaid.initialize(previousConfig);
mermaid.initialize({
startOnLoad: false,
securityLevel: 'strict',
suppressErrorRendering: true,
theme: document.documentElement.classList.contains('dark') ? 'dark' : 'default',
fontFamily: 'inherit',
htmlLabels: false,
flowchart: { htmlLabels: false },
secure: [
...(mermaid.mermaidAPI.defaultConfig.secure || []),
'htmlLabels', 'flowchart', 'themeCSS', 'themeVariables', 'fontFamily', 'altFontFamily', 'dompurifyConfig',
],
});
const parsed = await mermaid.mermaidAPI.getDiagramFromText(source);
// Strict Mermaid still fetches image-shape URLs during layout, before SVG sanitization.
// Inspect its parsed layout data rather than trying to rewrite the fence's source.
if ('getData' in parsed.db && typeof parsed.db.getData === 'function') {
const data: unknown = parsed.db.getData();
if (data && typeof data === 'object' && 'nodes' in data && Array.isArray(data.nodes)
&& data.nodes.some((node: unknown) => node && typeof node === 'object' && 'img' in node && node.img)) {
throw new Error('Embedded Mermaid images are not loaded in snapshot previews.');
}
}
if (!isCurrent() || !controls.isConnected) return;
staging = document.createElement('div');
staging.className = 'snapshot-diagram-staging';
staging.setAttribute('aria-hidden', 'true');
staging.inert = true;
controls.append(staging);
const { svg } = await mermaid.render(`snapshot-diagram-${++diagramId}`, source, staging);
if (!isCurrent() || !controls.isConnected) return;
const clean = purifier.sanitize(svg, {
USE_PROFILES: { svg: true },
FORBID_TAGS: ['style', 'foreignobject', 'image', 'use', 'a'],
FORBID_ATTR: ['style', 'href', 'xlink:href'],
RETURN_DOM_FRAGMENT: true,
});
const blockedContent = purifier.removed.some((entry) => 'element' in entry
&& ['foreignobject', 'image', 'use'].includes(entry.element.nodeName.toLowerCase()));
for (const element of clean.querySelectorAll('*')) {
element.removeAttribute('class');
for (const attribute of Array.from(element.attributes)) {
if (/url\s*\(/i.test(attribute.value) && !/^url\(\s*#[\w:.-]+\s*\)$/i.test(attribute.value)) {
element.removeAttribute(attribute.name);
}
}
}
const diagram = clean.querySelector('svg');
if (!diagram) throw new Error('No safe diagram output was produced.');
diagram.setAttribute('role', 'img');
diagram.setAttribute('aria-label', 'Rendered snapshot diagram; source is shown above');
output.replaceChildren(clean);
status.textContent = blockedContent
? 'Rendered with unsupported embedded content blocked. Original source remains above.'
: 'Rendered. Original source remains above.';
button.textContent = '↻ Render again';
} catch (error) {
if (isCurrent() && controls.isConnected) {
status.textContent = `Diagram could not be rendered: ${error instanceof Error ? error.message : String(error)} Original source remains above.`;
button.textContent = '↻ Retry';
}
} finally {
staging?.remove();
try {
restoreConfig?.();
} finally {
release();
button.disabled = false;
}
}
});
}
}
$effect(() => {
const source = html;
const target = root;
if (!target) return;
let active = true;
try {
purifier ??= createDOMPurify(window);
if (!purifier.isSupported) throw new Error('Safe HTML rendering is unavailable in this browser.');
const fragment = purifier.sanitize(source, {
ALLOWED_TAGS: documentTags,
ALLOWED_ATTR: documentAttributes,
ADD_URI_SAFE_ATTR: documentAttributes.filter((attribute) => attribute.startsWith('data-')),
ALLOWED_NAMESPACES: ['http://www.w3.org/1999/xhtml'],
ALLOW_DATA_ATTR: false,
ALLOW_ARIA_ATTR: false,
ALLOWED_URI_REGEXP: documentUris,
RETURN_DOM_FRAGMENT: true,
});
const blockedMarkup = purifier.removed.some((entry) => 'element' in entry
&& ['script', 'style', 'iframe', 'object', 'embed', 'form', 'input', 'textarea', 'select', 'video', 'audio', 'svg', 'math']
.includes(entry.element.nodeName.toLowerCase()));
prepareDocument(fragment);
renderSecrets(fragment);
renderCallouts(fragment);
renderMath(fragment);
prepareDiagrams(fragment, () => active && target.isConnected);
if (blockedMarkup) {
const details = document.createElement('details');
const summary = document.createElement('summary');
summary.textContent = 'Active or unsupported embedded content was blocked. View supplied HTML as text.';
const code = document.createElement('pre');
code.textContent = source;
details.append(summary, code);
fragment.prepend(details);
}
target.replaceChildren(source.trim() ? fragment : message('This snapshot has no body content.'));
} catch (error) {
const code = document.createElement('pre');
code.textContent = source;
target.replaceChildren(message(`Snapshot could not be rendered: ${error instanceof Error ? error.message : String(error)} Showing supplied HTML as text.`), code);
}
return () => { active = false; };
});
</script>
<section class="snapshot-document" aria-label={label}>
<div class="snapshot-body" bind:this={root}></div>
</section>
<style>
.snapshot-document {
min-width: 0;
max-width: 100%;
padding: 16px;
color: var(--text-primary);
font-family: var(--editor-font-family, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif);
font-size: var(--editor-font-size, 14px);
line-height: var(--editor-line-height, 1.65);
user-select: text;
overflow-wrap: anywhere;
}
.snapshot-body :global(p) { margin: 0 0 0.4em; }
.snapshot-body :global(h1) { font-size: 1.75em; margin: 1.5em 0 0.5em; }
.snapshot-body :global(h2) { font-size: 1.4em; margin: 1.25em 0 0.5em; }
.snapshot-body :global(h3) { font-size: 1.2em; margin: 1em 0 0.5em; }
.snapshot-body :global(h4), .snapshot-body :global(h5), .snapshot-body :global(h6) { margin: 0.9em 0 0.4em; }
.snapshot-body :global(h1), .snapshot-body :global(h2), .snapshot-body :global(h3) { line-height: 1.3; }
.snapshot-body > :global(:first-child) { margin-top: 0; }
.snapshot-body :global(strong) { font-weight: 600; }
.snapshot-body :global(a) { color: var(--text-accent); text-decoration: underline; }
.snapshot-body :global(blockquote) { border-left: 3px solid var(--accent); padding-left: 16px; margin: 1em 0; color: var(--text-secondary); }
.snapshot-body :global(code), .snapshot-body :global(kbd), .snapshot-body :global(pre) { font-family: var(--font-mono, monospace); }
.snapshot-body :global(code) { background: color-mix(in srgb, var(--accent) 8%, var(--bg-tertiary)); padding: 2px 6px; border-radius: 4px; font-size: 0.9em; }
.snapshot-body :global(pre) { overflow-x: auto; max-width: 100%; padding: 16px; margin: 1em 0; border-radius: 8px; background: color-mix(in srgb, var(--accent) 8%, var(--bg-tertiary)); white-space: pre; }
.snapshot-body :global(pre code) { padding: 0; background: none; font-size: 0.93em; line-height: 1.5; }
.snapshot-body :global(img) { max-width: 100%; height: auto; object-fit: contain; }
.snapshot-body :global(img[data-size="small"]) { max-width: 33%; }
.snapshot-body :global(img[data-size="medium"]) { max-width: 65%; }
.snapshot-body :global(ul), .snapshot-body :global(ol) { padding-left: 1.5em; }
.snapshot-body :global(ul[data-type="taskList"]) { padding-left: 0; list-style: none; }
.snapshot-body :global(.snapshot-task) { display: flex; align-items: flex-start; gap: 8px; list-style: none; }
.snapshot-body :global(.snapshot-task > input) { flex: none; margin-top: 0.4em; accent-color: var(--accent); }
.snapshot-body :global(.snapshot-task > div) { flex: 1; min-width: 0; }
.snapshot-body :global(details) { margin: 1em 0; }
.snapshot-body :global(summary) { cursor: pointer; }
.snapshot-body :global(.snapshot-table) { max-width: 100%; overflow-x: auto; margin: 1em 0; }
.snapshot-body :global(table) { border-collapse: collapse; min-width: 100%; }
.snapshot-body :global(th), .snapshot-body :global(td) { border: 1px solid var(--border); padding: 6px 10px; vertical-align: top; }
.snapshot-body :global(th) { background: var(--bg-secondary); font-weight: 600; }
.snapshot-body :global(.snapshot-secret) { margin: 1em 0; padding: 12px; border: 1px solid var(--border); border-radius: 8px; background: color-mix(in srgb, var(--accent) 7%, var(--bg-secondary)); }
.snapshot-body :global(.snapshot-message) { color: var(--text-secondary); font-size: 0.9em; }
.snapshot-body :global(.snapshot-wiki-link) { color: var(--text-accent); text-decoration: underline; text-decoration-style: dotted; }
.snapshot-body :global(.snapshot-page-break) { margin: 1em 0; border-top: 1px dashed var(--border); }
.snapshot-body :global(.snapshot-page-break)::before { content: 'Page break'; color: var(--text-tertiary); font-size: 0.8em; }
.snapshot-body :global(.snapshot-callout) { border: 1px solid rgba(var(--snapshot-callout-color), 0.3); border-left: 3px solid rgb(var(--snapshot-callout-color)); border-radius: 8px; background: rgba(var(--snapshot-callout-color), 0.06); margin: 1em 0; }
.snapshot-body :global(.snapshot-callout-header) { padding: 8px 12px; color: rgb(var(--snapshot-callout-color)); }
.snapshot-body :global(.snapshot-callout-header > span) { display: inline-flex; vertical-align: middle; margin-right: 8px; }
.snapshot-body :global(.snapshot-callout-header > small) { margin-left: 8px; }
.snapshot-body :global(.snapshot-callout-body) { padding: 8px 14px 10px; }
.snapshot-body :global(.snapshot-math-block) { overflow-x: auto; margin: 16px 0; padding: 12px; text-align: center; }
.snapshot-body :global(.snapshot-math-block .katex-display) { margin: 0; }
.snapshot-body :global(.snapshot-diagram) { position: relative; margin: -0.5em 0 1em; }
.snapshot-body :global(.snapshot-diagram button) { border: 1px solid var(--border); border-radius: 6px; padding: 7px 14px; background: var(--bg-tertiary); color: var(--text-primary); font: inherit; font-size: 0.9em; cursor: pointer; }
.snapshot-body :global(.snapshot-diagram button:hover:not(:disabled)) { border-color: var(--accent); }
.snapshot-body :global(.snapshot-diagram button:disabled) { opacity: 0.6; cursor: wait; }
.snapshot-body :global(.snapshot-diagram [role="status"]) { display: block; margin-top: 6px; }
.snapshot-body :global(.snapshot-diagram-output) { overflow-x: auto; max-width: 100%; }
.snapshot-body :global(.snapshot-diagram-output svg) { max-width: 100%; height: auto; color: var(--text-primary); }
/* SVG stylesheets are removed; these local rules keep untrusted diagrams legible. */
.snapshot-body :global(.snapshot-diagram-output svg text), .snapshot-body :global(.snapshot-diagram-output svg tspan) { fill: var(--text-primary); font-family: inherit; }
.snapshot-body :global(.snapshot-diagram-output svg path), .snapshot-body :global(.snapshot-diagram-output svg line), .snapshot-body :global(.snapshot-diagram-output svg polyline) { stroke: var(--text-secondary); fill: none; }
.snapshot-body :global(.snapshot-diagram-output svg rect), .snapshot-body :global(.snapshot-diagram-output svg circle), .snapshot-body :global(.snapshot-diagram-output svg ellipse), .snapshot-body :global(.snapshot-diagram-output svg polygon) { stroke: var(--text-secondary); fill: var(--bg-secondary); }
.snapshot-body :global(.snapshot-diagram-output svg marker path) { fill: var(--text-secondary); }
.snapshot-body :global(.snapshot-diagram-staging) { position: absolute; visibility: hidden; pointer-events: none; width: 100%; }
.snapshot-body :global(summary:focus-visible), .snapshot-body :global(button:focus-visible), .snapshot-body :global(.snapshot-table:focus-visible) { outline: 2px solid var(--accent); outline-offset: 3px; }
</style>
+64 -21
View File
@@ -4,9 +4,11 @@
import { getTasks } from '$lib/api';
import { debounce } from '$lib/utils/debounce';
import { tasksLayout, tasksHideCompleted, tasksOnlyFlagged, tasksSort, appConfig } from '$lib/stores/app';
import { isAndroid } from '$lib/platform';
import { isAndroid, isMobile } from '$lib/platform';
import type { TaskItem, FileEvent } from '$lib/types';
import { isCurrentVaultEvent } from '$lib/utils/note-list-updates';
import { formatLocalDate } from '$lib/utils/time';
import { matchesTaskDueFilter, type TaskDueFilter } from '$lib/utils/task-filter';
let { onOpenTask = (_t: TaskItem) => {}, onToggleTask = async (_t: TaskItem) => {}, onSetTaskPriority = async (_t: TaskItem, _p: string | null) => {}, onSetTaskDue = async (_t: TaskItem, _d: string | null) => {} }: {
onOpenTask?: (t: TaskItem) => void;
@@ -86,6 +88,19 @@
onMount(() => {
void load();
let dayTimer: ReturnType<typeof setTimeout> | undefined;
const refreshDay = () => {
const now = new Date();
today = formatLocalDate(now);
clearTimeout(dayTimer);
const nextDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
dayTimer = setTimeout(refreshDay, nextDay.getTime() - now.getTime() + 1);
};
const resumeDay = () => {
if (document.visibilityState === 'visible') refreshDay();
};
refreshDay();
document.addEventListener('visibilitychange', resumeDay);
listen<FileEvent>('file-changed', ({ payload }) => handleFileChange(payload)).then((u) => {
if (disposed) u();
else unlisten = u;
@@ -94,26 +109,32 @@
disposed = true;
fullLoad += 1;
unlisten?.();
clearTimeout(dayTimer);
document.removeEventListener('visibilitychange', resumeDay);
};
});
// Local YYYY-MM-DD (en-CA gives ISO-style date)
const today = new Date().toLocaleDateString('en-CA');
let today = $state(formatLocalDate(new Date()));
let dueFilter = $state<TaskDueFilter>('all');
const prioRank: Record<string, number> = { high: 0, med: 1, low: 2 };
function prank(p: string | null) { return p ? (prioRank[p] ?? 3) : 3; }
// Free-text task filter: matches the task text or its note title (case-insensitive).
const normalizedTaskQuery = $derived(taskQuery.trim().toLowerCase());
function matchesQuery(t: TaskItem): boolean {
const q = taskQuery.trim().toLowerCase();
if (!q) return true;
return t.text.toLowerCase().includes(q) || t.note_title.toLowerCase().includes(q);
if (!normalizedTaskQuery) return true;
return t.text.toLowerCase().includes(normalizedTaskQuery) || t.note_title.toLowerCase().includes(normalizedTaskQuery);
}
const matchingTasks = $derived(tasks.filter((t) =>
(!$tasksHideCompleted || !t.completed)
&& (!$tasksOnlyFlagged || !!t.due || !!t.priority)
&& matchesQuery(t)
&& matchesTaskDueFilter(t.due, dueFilter, today, t.completed)
));
const filtered = $derived.by(() => {
let list = tasks.slice();
if ($tasksHideCompleted) list = list.filter((t) => !t.completed);
if ($tasksOnlyFlagged) list = list.filter((t) => !!t.due || !!t.priority);
list = list.filter(matchesQuery);
const list = matchingTasks.slice();
list.sort((a, b) => {
if (a.completed !== b.completed) return a.completed ? 1 : -1; // done last
if ($tasksSort === 'priority') {
@@ -134,8 +155,8 @@
const openCount = $derived(tasks.filter((t) => !t.completed).length);
function dueClass(due: string | null): string {
if (!due) return '';
function dueClass(due: string | null, completed: boolean): string {
if (!due || completed) return '';
if (due < today) return 'overdue';
if (due === today) return 'duetoday';
return '';
@@ -168,7 +189,7 @@
const calNow = new Date();
let calMonth = $state(calNow.getMonth());
let calYear = $state(calNow.getFullYear());
let selectedDate = $state(today);
let selectedDate = $state(formatLocalDate(calNow));
const calMonthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
// 0 = Sunday, 1 = Monday (default). Day names + grid offset follow the setting.
@@ -176,12 +197,9 @@
const DOW = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
const calDayNames = $derived([...DOW.slice(weekStartsOn), ...DOW.slice(0, weekStartsOn)]);
// Calendar tasks share the list's hide-completed filter (no sort).
const calBase = $derived(tasks.filter((t) => (!$tasksHideCompleted || !t.completed) && (!$tasksOnlyFlagged || !!t.due || !!t.priority) && matchesQuery(t)));
const tasksByDate = $derived.by(() => {
const m = new Map<string, TaskItem[]>();
for (const t of calBase) {
for (const t of matchingTasks) {
if (!t.due) continue;
const arr = m.get(t.due);
if (arr) arr.push(t);
@@ -190,7 +208,7 @@
return m;
});
const undatedTasks = $derived(calBase.filter((t) => !t.due));
const undatedTasks = $derived(matchingTasks.filter((t) => !t.due));
function byPriority(a: TaskItem, b: TaskItem) {
if (a.completed !== b.completed) return a.completed ? 1 : -1;
@@ -247,13 +265,15 @@
}
</script>
<div class="tasks-view">
<div class="tasks-view" class:mobile={isMobile}>
{#if prioMenuFor || dueMenuFor}
<!-- svelte-ignore a11y_consider_explicit_label -->
<button class="prio-backdrop" onclick={() => { prioMenuFor = null; dueMenuFor = null; }} aria-label="Close menu"></button>
{/if}
<div class="tasks-controls">
<input class="tasks-filter" type="text" placeholder="Filter tasks…" bind:value={taskQuery} onkeydown={(e) => { if (e.key === 'Escape') taskQuery = ''; }} aria-label="Filter tasks by text" />
<button class="tasks-ctl" class:active={dueFilter === 'today'} aria-pressed={dueFilter === 'today'} onclick={() => { dueFilter = dueFilter === 'today' ? 'all' : 'today'; if (dueFilter === 'today') calToday(); }} title="Show tasks due today">Today</button>
<button class="tasks-ctl" class:active={dueFilter === 'overdue'} aria-pressed={dueFilter === 'overdue'} onclick={() => { dueFilter = dueFilter === 'overdue' ? 'all' : 'overdue'; }} title="Show tasks due before today">Overdue</button>
<button class="tasks-ctl" class:active={$tasksHideCompleted} onclick={() => ($tasksHideCompleted = !$tasksHideCompleted)} title="Hide completed tasks">Hide done</button>
<button class="tasks-ctl" class:active={$tasksOnlyFlagged} onclick={() => ($tasksOnlyFlagged = !$tasksOnlyFlagged)} title="Only show tasks with a due date or priority">Flagged</button>
{#if !isAndroid}
@@ -306,7 +326,7 @@
<span class="task-label">{t.text}</span>
<span class="task-due-wrap">
<button
class="task-due {dueClass(t.due)}"
class="task-due {dueClass(t.due, t.completed)}"
class:set={!!t.due}
title="Set due date"
aria-label="Set due date"
@@ -388,7 +408,15 @@
</div>
</div>
{:else if filtered.length === 0}
<div class="tasks-empty">No tasks. Add <code>- [ ] something</code> to any note.{#if openCount === 0 && tasks.length}{' '}All done!{/if}</div>
<div class="tasks-empty">
{#if tasks.length === 0}
No tasks. Add <code>- [ ] something</code> to any note.
{:else if $tasksHideCompleted && openCount === 0}
All tasks are complete.
{:else}
No tasks match these filters.
{/if}
</div>
{:else}
<div class="tasks-list">
{#each filtered as t (rowKey(t))}{@render taskRow(t, false)}{/each}
@@ -444,6 +472,21 @@
color: var(--text-primary);
font-size: 12px;
}
.tasks-view.mobile .tasks-filter {
flex-basis: 100%;
min-height: 44px;
}
.tasks-view.mobile .tasks-ctl {
min-width: 44px;
min-height: 44px;
}
/* Marking a task done is the main action on a phone: widen the tap area without changing how the box looks,
staying clear of the priority control that follows it. */
.tasks-view.mobile .task-check::before {
content: '';
position: absolute;
inset: -16px -6px -16px -16px;
}
.tasks-filter::placeholder { color: var(--text-tertiary); }
.tasks-filter:focus { outline: none; border-color: var(--accent); }
.tasks-list {
+1 -1
View File
@@ -96,7 +96,7 @@
</div>
{/if}
<div class="titlebar-actions">
<button class="switch-vault-btn" onclick={async () => { if (await onBeforeVaultSwitch()) $vaultReady = false; }} title="Switch Vault">
<button class="switch-vault-btn" onclick={async () => { if (await onBeforeVaultSwitch()) $vaultReady = false; }} title="Manage Vaults" aria-label="Manage Vaults">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z" />
</svg>
@@ -120,6 +120,12 @@ export const CachedCodeBlockLowlight = CodeBlock.extend<CodeBlockOptions & { low
addOptions() {
return { ...this.parent!(), lowlight: null };
},
addAttributes() {
// A block written without a language keeps none, so a bare fence is not saved as the default
// language. Highlighting still falls back to the default language for such blocks.
const attributes: Record<string, object> = this.parent?.() ?? {};
return { ...attributes, language: { ...attributes.language, default: null } };
},
addProseMirrorPlugins() {
if (!this.options.lowlight) throw new Error('Code block highlighting requires a lowlight instance');
return [
+196
View File
@@ -0,0 +1,196 @@
import { Node, mergeAttributes } from '@tiptap/core';
import type { Node as ProseMirrorNode } from '@tiptap/pm/model';
import { clampImgGridCols, normalizeImgGridLayout } from '../imageGrid.ts';
export interface ImageGridDrag {
gridPos: number;
index: number;
}
export interface ImageGridOptions {
/** Replace the grid at `gridPos` with one image paragraph per image. */
onUngroup: (gridPos: number) => void;
/** Move image `fromIndex` of one grid before slot `toIndex` of the same or another grid. */
onMoveImage: (fromGridPos: number, fromIndex: number, toGridPos: number, toIndex: number) => void;
}
export interface ImageGridStorage {
/** The grid image being dragged. Shared by every grid in the editor so images can move between grids. */
drag: ImageGridDrag | null;
}
declare module '@tiptap/core' {
interface Storage {
imageGrid: ImageGridStorage;
}
}
function controlButton(label: string, title: string, onClick: () => void): HTMLButtonElement {
const button = document.createElement('button');
button.type = 'button';
button.className = 'img-grid-control';
button.title = title;
button.textContent = label;
button.addEventListener('mousedown', (event) => event.preventDefault());
button.addEventListener('click', (event) => {
event.preventDefault();
onClick();
});
return button;
}
/**
* Side-by-side image grid (#254). Saved as a `:::img-grid` block (see imageGrid.ts). The content is
* the editor's inline image nodes, so size, copy and the image toolbar work the same as elsewhere.
*/
export const ImageGrid = Node.create<ImageGridOptions, ImageGridStorage>({
name: 'imageGrid',
group: 'block',
content: 'image+',
defining: true,
addOptions() {
return { onUngroup: () => {}, onMoveImage: () => {} };
},
addStorage() {
return { drag: null };
},
addAttributes() {
return {
cols: {
default: 2,
parseHTML: (element: HTMLElement) => clampImgGridCols(element.getAttribute('data-cols') ?? ''),
renderHTML: (attributes: Record<string, unknown>) => ({ 'data-cols': String(clampImgGridCols(attributes.cols)) }),
},
layout: {
default: 'grid',
parseHTML: (element: HTMLElement) => normalizeImgGridLayout(element.getAttribute('data-layout')),
renderHTML: (attributes: Record<string, unknown>) => ({ 'data-layout': normalizeImgGridLayout(attributes.layout) }),
},
};
},
parseHTML() {
return [{ tag: 'div[data-img-grid]' }];
},
renderHTML({ HTMLAttributes }) {
// `data-img-grid` is what parseHTML matches, so copied grids paste back as grids.
return ['div', mergeAttributes(HTMLAttributes, { 'data-img-grid': '', class: 'img-grid' }), 0];
},
addNodeView() {
const { onUngroup, onMoveImage } = this.options;
const storage = this.storage;
return ({ node, getPos, editor }) => {
const dom = document.createElement('div');
const header = document.createElement('div');
const content = document.createElement('div');
dom.className = 'img-grid-wrap';
header.className = 'img-grid-header';
header.contentEditable = 'false';
dom.append(header, content);
const position = (): number | null => (typeof getPos === 'function' ? getPos() ?? null : null);
const render = (current: ProseMirrorNode) => {
const cols = clampImgGridCols(current.attrs.cols);
const layout = normalizeImgGridLayout(current.attrs.layout);
for (const element of [dom, content]) {
element.setAttribute('data-img-grid', '');
element.setAttribute('data-cols', String(cols));
element.setAttribute('data-layout', layout);
}
content.className = 'img-grid';
header.replaceChildren();
if (!editor.isEditable) return;
const setAttrs = (attrs: Record<string, unknown>) => {
const pos = position();
if (pos !== null) editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...current.attrs, ...attrs }));
};
header.append(
controlButton(`${cols} col`, 'Cycle columns (2-4)', () => setAttrs({ cols: cols >= 4 ? 2 : cols + 1 })),
controlButton(layout, 'Toggle grid / masonry', () => setAttrs({ layout: layout === 'masonry' ? 'grid' : 'masonry' })),
controlButton('ungroup', 'Ungroup back to single images', () => {
const pos = position();
if (pos !== null) onUngroup(pos);
}),
);
};
render(node);
const clearDropMark = () => {
content.classList.remove('img-grid-dropping');
content.removeAttribute('data-drop-index');
};
const dropIndexAt = (x: number, y: number): number => {
const images = Array.from(content.querySelectorAll('img'));
for (let i = 0; i < images.length; i++) {
const rect = images[i].getBoundingClientRect();
if (y < rect.top + rect.height / 2 - 4) return i;
if (y <= rect.bottom + 4 && x < rect.left + rect.width / 2) return i;
}
return images.length;
};
content.addEventListener('dragstart', (event) => {
if (!editor.isEditable) return;
const image = (event.target as HTMLElement | null)?.closest?.('img');
const pos = position();
if (!image || !content.contains(image) || pos === null) return;
const index = Array.from(content.querySelectorAll('img')).indexOf(image as HTMLImageElement);
if (index >= 0) storage.drag = { gridPos: pos, index };
});
content.addEventListener('dragover', (event) => {
if (!storage.drag || !editor.isEditable) return;
event.preventDefault();
event.stopPropagation();
if (event.dataTransfer) event.dataTransfer.dropEffect = 'move';
content.classList.add('img-grid-dropping');
content.setAttribute('data-drop-index', String(dropIndexAt(event.clientX, event.clientY)));
});
content.addEventListener('dragleave', (event) => {
const to = event.relatedTarget as globalThis.Node | null;
if (!to || !content.contains(to)) clearDropMark();
});
content.addEventListener('dragend', () => {
storage.drag = null;
clearDropMark();
});
content.addEventListener('drop', (event) => {
const drag = storage.drag;
if (!drag) return;
event.preventDefault();
event.stopPropagation();
const toIndex = dropIndexAt(event.clientX, event.clientY);
clearDropMark();
storage.drag = null;
const pos = position();
if (pos !== null) onMoveImage(drag.gridPos, drag.index, pos, toIndex);
});
return {
dom,
contentDOM: content,
update(updated) {
if (updated.type.name !== 'imageGrid') return false;
render(updated);
return true;
},
ignoreMutation(mutation) {
if (mutation.type === 'selection') return false;
return !content.contains(mutation.target);
},
stopEvent(event) {
if (header.contains(event.target as globalThis.Node)) return true;
// Grid reordering handles its own drops; ProseMirror would insert a second copy.
return (event.type === 'drop' || event.type === 'dragover') && !!storage.drag
&& content.contains(event.target as globalThis.Node);
},
destroy() {
clearDropMark();
},
};
};
},
});
+111
View File
@@ -0,0 +1,111 @@
import { markdownLanguage } from '@codemirror/lang-markdown';
import { splitSnapshot } from '../utils/history-comparison.ts';
import { createEditorMarkdownParser, displayMathRanges, inlineMathRanges, legacyLinkRanges } from './markdownParser.ts';
export interface ExternalNoteReference {
from: number;
to: number;
href: string;
html: boolean;
pdf?: boolean;
}
const markdown = createEditorMarkdownParser();
// The editor never treats indented text as a code block, so an indented image is still an image here.
const editorSyntax = (markdownLanguage.parser as unknown as {
configure(spec: { remove: string[] }): typeof markdownLanguage.parser;
}).configure({ remove: ['IndentedCode'] });
/** Byte ranges for local attachment destinations, without reserializing the user's Markdown. */
export function externalNoteReferences(raw: string): ExternalNoteReference[] {
const { body, metadata } = splitSnapshot(raw);
const spans: ExternalNoteReference[] = [];
const seen = new Set<string>();
const legacy = legacyLinkRanges(body);
const legacyByStart = new Map(legacy.map(range => [range.from, range]));
const math = [...displayMathRanges(body), ...inlineMathRanges(body)];
const environment: { references?: Record<string, { href: string }> } = {};
markdown.parse(body, environment);
const tree = editorSyntax.parse(body);
const usedLabels = new Set<string>();
const label = (node: { from: number; to: number }) => markdown.utils.normalizeReference(body.slice(node.from + 1, node.to - 1));
tree.iterate({ enter(node) {
if (!['Link', 'Image'].includes(node.name) || node.node.getChild('URL') || body[node.to] === '(' || math.some(range => node.from >= range.from && node.from < range.to)) return;
for (let parent = node.node.parent; parent; parent = parent.parent) if (parent.name === 'Image') return false;
const explicit = node.node.getChild('LinkLabel');
let key = explicit ? label(explicit) : '';
if (!key) {
const closing = node.node.getChildren('LinkMark').find(mark => body.slice(mark.from, mark.to) === ']');
if (closing) key = markdown.utils.normalizeReference(body.slice(node.from + (node.name === 'Image' ? 2 : 1), closing.from));
}
if (environment.references?.[key]) usedLabels.add(key);
} });
const selectedDefinitions = new Set<string>();
const add = (from: number, to: number, html = false, pdf = false) => {
if (math.some(range => from >= range.from && from < range.to)) return;
const href = markdown.utils.unescapeAll(body.slice(from, to));
let path = href.split(/[?#]/, 1)[0];
try { path = decodeURIComponent(path); } catch { /* Native validation handles malformed escapes. */ }
if (!path || /^[a-z][a-z\d+.-]*:/i.test(path) || /^[\\/]/.test(path) || /\.md$/i.test(path)) return;
const key = `${from}:${to}`;
if (seen.has(key)) return;
seen.add(key);
spans.push({ from: metadata.length + from, to: metadata.length + to, href, html, pdf });
};
tree.iterate({
enter(node) {
// Links and HTML inside image alt text are labels, not additional attachments.
let direct = true;
for (let ancestor = node.node.parent; ancestor; ancestor = ancestor.parent) {
if (ancestor.name === 'Image' && !(node.name === 'URL' && direct)) return false;
direct = false;
}
if (node.name === 'URL' && ['Image', 'Link', 'LinkReference'].includes(node.node.parent?.name ?? '')) {
const angle = body[node.from] === '<' && body[node.to - 1] === '>';
const from = node.from + (angle ? 1 : 0);
if (node.node.parent?.name === 'LinkReference') {
const definition = node.node.parent.getChild('LinkLabel');
if (!definition) return;
const key = label(definition);
const href = markdown.normalizeLink(markdown.utils.unescapeAll(body.slice(from, node.to - (angle ? 1 : 0))));
if (!usedLabels.has(key) || selectedDefinitions.has(key) || environment.references?.[key]?.href !== href) return;
selectedDefinitions.add(key);
}
const compatible = legacyByStart.get(from);
add(from, compatible?.to ?? node.to - (angle ? 1 : 0));
} else if (node.name === 'Paragraph' || /Heading[1-6]$/.test(node.name)) {
for (const range of legacy) {
if (range.from >= node.from && range.to <= node.to) add(range.from, range.to);
}
} else if (node.name === 'HTMLTag' || node.name === 'HTMLBlock') {
const html = body.slice(node.from, node.to);
// Skip comments and literal HTML code, not just Markdown fences.
const tags = /<!--[\s\S]*?(?:-->|$)|<(script|style|textarea|pre|code)\b[^>]*>[\s\S]*?(?:<\/\1\s*>|$)|<(img|a|div)\b(?:"[^"]*"|'[^']*'|[^'">])*>/gi;
for (const tag of html.matchAll(tags)) {
if (!tag[2]) continue;
const attributes = /(?:^|\s)([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
for (const attribute of tag[0].matchAll(attributes)) {
const name = attribute[1].toLowerCase();
const element = tag[2].toLowerCase();
if (!(element === 'img' && name === 'src') && !(element === 'a' && name === 'href') && !(element === 'div' && name === 'data-pdf-src')) continue;
const value = attribute[2] ?? attribute[3] ?? attribute[4];
const quoted = attribute[2] !== undefined || attribute[3] !== undefined;
const start = node.from + tag.index! + attribute.index! + attribute[0].length - value.length - (quoted ? 1 : 0);
add(start, start + value.length, true, name === 'data-pdf-src');
}
}
}
},
});
spans.sort((a, b) => a.from - b.from);
const encoder = new TextEncoder();
let previous = 0;
let bytes = 0;
return spans.map(span => {
bytes += encoder.encode(raw.slice(previous, span.from)).length;
const from = bytes;
bytes += encoder.encode(raw.slice(span.from, span.to)).length;
previous = span.to;
return { ...span, from, to: bytes };
});
}
+153
View File
@@ -0,0 +1,153 @@
/*
* Side-by-side / grid / masonry layout for images (#254).
*
* Portable syntax: a `:::` container whose lines are ordinary Markdown images. Other renderers
* (GitHub, Typora, ...) show the images stacked, plus the literal header and closer lines.
*
* :::img-grid cols=3 masonry
* ![](a.jpg)
* ![Beach|size=small](b.jpg)
* :::
*
* Header: `:::img-grid`, optionally followed by `cols=2|3|4` and `masonry` / `grid` (also written
* `layout=masonry` / `layout=grid`), in any order.
* Body: one image per line. The closer is `:::` on its own line, or trailing the last image
* (`![](b.jpg) :::`).
*
* Content is never partially converted. If any part of the block isn't understood (an unknown
* option, a caption, a linked image, a blank line, a missing closer), the block is left as
* ordinary Markdown. The parsing itself is a markdown-it block rule (`markdownParser.ts`), so a
* grid example inside a code block is never converted.
*/
export type ImgGridLayout = 'grid' | 'masonry';
export interface ImgGridAttrs {
cols: number;
layout: ImgGridLayout;
}
export const IMG_GRID_MIN_COLS = 2;
export const IMG_GRID_MAX_COLS = 4;
export const IMG_GRID_DEFAULT_COLS = 2;
export const IMG_GRID_CLOSER = ':::';
/** Clamp a column count from node attributes or the DOM into the supported range. */
export function clampImgGridCols(value: unknown): number {
const n = typeof value === 'string' ? parseInt(value, 10) : (value as number);
if (!Number.isFinite(n)) return IMG_GRID_DEFAULT_COLS;
return Math.min(IMG_GRID_MAX_COLS, Math.max(IMG_GRID_MIN_COLS, Math.floor(n)));
}
export function normalizeImgGridLayout(value: unknown): ImgGridLayout {
return value === 'masonry' ? 'masonry' : 'grid';
}
/**
* Parse a `:::img-grid ...` header line (already stripped of container indentation), or null.
* Any unknown or out-of-range option makes the whole line not a header, so it stays literal text
* instead of being silently rewritten.
*/
export function parseImgGridHeader(line: string): ImgGridAttrs | null {
const match = /^:::img-grid(?:[ \t]+(.*?))?[ \t]*$/.exec(line);
if (!match) return null;
let cols: number | null = null;
let layout: ImgGridLayout | null = null;
for (const token of (match[1] ?? '').split(/[ \t]+/).filter(Boolean)) {
const colsMatch = /^cols=(\d)$/.exec(token);
const layoutMatch = /^(?:layout=)?(grid|masonry)$/.exec(token);
if (colsMatch && cols === null) {
const n = Number(colsMatch[1]);
if (n < IMG_GRID_MIN_COLS || n > IMG_GRID_MAX_COLS) return null;
cols = n;
} else if (layoutMatch && layout === null) {
layout = layoutMatch[1] as ImgGridLayout;
} else {
return null;
}
}
return { cols: cols ?? IMG_GRID_DEFAULT_COLS, layout: layout ?? 'grid' };
}
/** Canonical header for saving. Defaults are omitted: `:::img-grid`, `:::img-grid cols=3 masonry`. */
export function serializeImgGridHeader(attrs: ImgGridAttrs): string {
const cols = clampImgGridCols(attrs.cols);
const options = [
cols !== IMG_GRID_DEFAULT_COLS ? `cols=${cols}` : '',
normalizeImgGridLayout(attrs.layout) === 'masonry' ? 'masonry' : '',
].filter(Boolean);
return [':::img-grid', ...options].join(' ');
}
/**
* Whole grid block for saving. `images` is the Markdown of the grid's images, one per line, as the
* editor serializes inline images. Returns '' when there is nothing to save, so a grid whose images
* all failed to resolve doesn't leave behind a header that would no longer parse.
*/
export function serializeImgGrid(attrs: ImgGridAttrs, images: string): string {
const body = images.replace(/\n+$/, '');
if (!body.trim()) return '';
return `${serializeImgGridHeader(attrs)}\n${body}\n${IMG_GRID_CLOSER}\n`;
}
/** Split a trailing ` :::` closer off the last image line: `![](b.jpg) :::` gives `![](b.jpg)`. */
export function splitTrailingCloser(line: string): string | null {
const match = /^(.*\S)[ \t]+:::[ \t]*$/.exec(line);
return match ? match[1] : null;
}
/** New grids get one column per image, within 2 to 4. */
export function defaultGridCols(imageCount: number): number {
return clampImgGridCols(imageCount);
}
/** One top-level block, for grouping neighboring images into a grid. */
export interface ImageBlockInfo {
isImage: boolean;
imageCount: number;
/** An empty paragraph. One of these before an image is how the editor holds the blank line above it. */
isEmpty?: boolean;
}
/**
* Grow from `target` across neighboring image-only blocks (image paragraphs and existing grids).
* Images a blank line apart load with one empty paragraph between them, so a single empty paragraph
* between two image blocks is part of the range, and so is the one directly above the first image:
* left behind, it would be saved as an empty-paragraph marker above the grid.
* Returns the block range and its image total, or null unless it holds at least two images.
*/
export function expandImageRange(
blocks: readonly ImageBlockInfo[],
target: number,
): { start: number; end: number; total: number } | null {
if (target < 0 || target >= blocks.length || !blocks[target].isImage) return null;
const isImage = (index: number) => index >= 0 && index < blocks.length && blocks[index].isImage;
const isEmpty = (index: number) => index >= 0 && index < blocks.length && !!blocks[index].isEmpty;
let start = target;
let end = target;
for (;;) {
if (isImage(start - 1)) start -= 1;
else if (isEmpty(start - 1) && isImage(start - 2)) start -= 2;
else break;
}
for (;;) {
if (isImage(end + 1)) end += 1;
else if (isEmpty(end + 1) && isImage(end + 2)) end += 2;
else break;
}
let total = 0;
for (let i = start; i <= end; i++) total += blocks[i].imageCount;
if (total < 2) return null;
if (isEmpty(start - 1)) start -= 1;
return { start, end, total };
}
/**
* Index an item ends up at when it is dragged from `from` and dropped before slot `to` of the same
* list (`to` counts slots in the list before removal, 0..length). Returns null for a no-op drop.
*/
export function reorderTarget(from: number, to: number, length: number): number | null {
if (from < 0 || from >= length) return null;
const target = Math.max(0, Math.min(length - 1, to > from ? to - 1 : to));
return target === from ? null : target;
}
+216 -18
View File
@@ -11,6 +11,111 @@ type MarkdownMark = {
const identityAssetSource: AssetSourceNormalizer = (source) => source;
function backtickDelimiter(text: string, minimum: number): string {
let length = minimum;
for (const match of text.matchAll(/`+/g)) {
length = Math.max(length, match[0].length + 1);
}
return '`'.repeat(length);
}
export interface MarkdownBlock {
text: string;
isImage: boolean;
/** An empty paragraph, which is kept as a marker line so its vertical space round-trips. */
isEmpty?: boolean;
}
const EMPTY_PARAGRAPH_MARKER = '<!-- -->';
/**
* ` "title"` suffix for an image destination, or '' without a title. Uses CommonMark backslash
* escapes, which the parser undoes exactly, so a title survives any number of saves unchanged.
* `&` is only escaped where it would otherwise read as an entity (`&amp;`, `&#38;`).
*/
export function serializeImageTitle(title: string | null | undefined): string {
if (!title) return '';
return ` "${title.replace(/[\\"]/g, '\\$&').replace(/&(?=#?[a-z0-9]+;)/gi, '\\&')}"`;
}
/**
* Join serialized top-level blocks into a note body. Blocks that already end in a blank line
* (lists) get no extra separator, and an empty paragraph directly before an image is written as
* the blank line the parser turns back into that paragraph, not as a marker glued to the image.
*/
export function joinMarkdownBlocks(blocks: readonly MarkdownBlock[]): string {
const entries = [...blocks];
while (entries.length > 0 && entries[entries.length - 1].isEmpty) entries.pop();
const parts: string[] = [];
// Only the last two characters decide the separator. Reading them from the growing result would
// flatten it on every block, which makes joining a large note quadratic.
let tail = '';
const append = (piece: string) => {
parts.push(piece);
tail = (tail + piece).slice(-2);
};
entries.forEach((entry, index) => {
if (entry.isEmpty && entries[index + 1]?.isImage) {
if (index > 0 && tail !== '\n\n') append(tail.endsWith('\n') ? '\n' : '\n\n');
return;
}
const text = entry.isEmpty ? EMPTY_PARAGRAPH_MARKER : entry.text;
// Images follow their predecessor directly; every other block needs a blank line before it.
if (index === 0) append(text);
else append((entry.isImage || tail === '\n\n' ? '' : '\n') + text);
});
return parts.join('').trim() + '\n';
}
export interface ListItemBlock {
text: string;
/** A nested bullet, numbered or task list. */
isList: boolean;
}
/**
* Join the serialized children of one list item. The first block follows the list marker; a nested
* list is indented four spaces directly under it; any other block (a further paragraph, a code
* block, a quote) is separated by a blank line and indented the same way so it stays in the item.
*/
export function joinListItemBlocks(blocks: readonly ListItemBlock[]): string {
const parts: string[] = [];
blocks.forEach((block, index) => {
if (block.isList) {
// A serialized list ends with a blank line; left in, it became an indented blank line here
// and an extra empty line after the outer list.
parts.push(block.text.replace(/\n+$/, '').split('\n').map((line) => (line ? ' ' + line : line)).join('\n'));
} else if (index === 0) {
parts.push(block.text);
} else {
const body = block.text.replace(/\n+$/, '');
if (body.trim() === '') return;
parts.push('', body.split('\n').map((line) => (line ? ' ' + line : line)).join('\n'));
}
});
return parts.join('\n') + '\n';
}
export function serializeCodeBlock(code: string, language: string = ''): string {
const fence = backtickDelimiter(code, 3);
return `${fence}${language}\n${code}\n${fence}\n`;
}
function serializeCodeSpan(text: string): string {
const delimiter = backtickDelimiter(text, 1);
const needsPadding = text.startsWith('`') || text.endsWith('`')
|| ((text.startsWith(' ') || text.endsWith(' ')) && /[^ ]/.test(text));
const padding = needsPadding ? ' ' : '';
return `${delimiter}${padding}${text}${padding}${delimiter}`;
}
function serializeInlineMath(tex: string, forceEncoded: boolean): string {
if (forceEncoded || !tex || /[\r\n$]/.test(tex) || /^\s|\s$/.test(tex) || /(?:^|[^\\])(?:\\\\)*\\$/.test(tex)) {
return `<span data-math-inline="${encodeURIComponent(tex)}" class="math-inline"></span>`;
}
return `$${tex}$`;
}
function stringAttr(mark: Mark, name: string): string {
const value = mark.attrs?.[name];
return typeof value === 'string' ? value : '';
@@ -52,15 +157,14 @@ function markdownMark(mark: Mark, text: string): MarkdownMark | null {
}
: null;
}
case 'code':
return { key: 'code', priority: 90, open: '`', close: '`' };
case 'link': {
const href = stringAttr(mark, 'href');
const title = stringAttr(mark, 'title');
return {
key: `link:${href}`,
key: `link:${href}:${title}`,
priority: 100,
open: '[',
close: `](${href})`,
close: `](${href}${serializeImageTitle(title)})`,
};
}
case 'wikiLink': {
@@ -78,8 +182,9 @@ function markdownMark(mark: Mark, text: string): MarkdownMark | null {
}
}
function marksForText(node: ProseMirrorNode): MarkdownMark[] {
function marksForText(node: ProseMirrorNode, linksOnly = false): MarkdownMark[] {
return node.marks
.filter((mark) => !linksOnly || mark.type.name === 'link' || mark.type.name === 'wikiLink')
.map((mark) => markdownMark(mark, node.text || ''))
.filter((mark): mark is MarkdownMark => mark !== null)
.sort((left, right) => left.priority - right.priority || left.key.localeCompare(right.key));
@@ -92,52 +197,145 @@ function commonMarkCount(active: MarkdownMark[], next: MarkdownMark[]): number {
return index;
}
export interface InlineSerializeOptions {
/** Leave `$` unescaped (only backslashes before it are doubled); callers must check it cannot form math. */
literalDollars?: boolean;
}
export function serializeInlineMarkdown(
node: ProseMirrorNode,
normalizeAssetSource: AssetSourceNormalizer = identityAssetSource,
options: InlineSerializeOptions = {},
): string {
let result = '';
let activeMarks: MarkdownMark[] = [];
// Length of `result` right after each active mark was opened, to tell when a mark holds nothing.
let openEnds: number[] = [];
// `**word **` is not emphasis in Markdown: delimiters must touch the text they wrap. Spaces at the
// edges of bold, italic and similar marks are therefore written outside the delimiters, and a mark
// that covers only spaces is not written at all.
const isDelimiter = (mark: MarkdownMark) => mark.open === mark.close && !mark.open.startsWith('<');
const transitionMarks = (nextMarks: MarkdownMark[]) => {
const shared = commonMarkCount(activeMarks, nextMarks);
for (let index = activeMarks.length - 1; index >= shared; index -= 1) {
result += activeMarks[index].close;
let trailing = '';
if (activeMarks.slice(shared).some(isDelimiter)) {
trailing = result.match(/[ \t\u00a0]+$/)?.[0] ?? '';
if (trailing) result = result.slice(0, -trailing.length);
}
for (let index = activeMarks.length - 1; index >= shared; index -= 1) {
const mark = activeMarks[index];
if (isDelimiter(mark) && result.length === openEnds[index]) result = result.slice(0, result.length - mark.open.length);
else result += mark.close;
}
result += trailing;
openEnds = openEnds.slice(0, shared);
for (let index = shared; index < nextMarks.length; index += 1) {
result += nextMarks[index].open;
openEnds.push(result.length);
}
activeMarks = nextMarks;
};
node.forEach((child, _offset, index) => {
for (let index = 0; index < node.childCount; index += 1) {
const child = node.child(index);
if (child.isText) {
transitionMarks(marksForText(child));
const isCode = child.marks.some((mark) => mark.type.name === 'code');
let text = child.text || '';
if (index === 0) {
text = text.replace(/^[\t\u2003]+/, (whitespace) => '&emsp;'.repeat(whitespace.length));
if (isCode) {
// One code span per run of code text under the same link. Formatting that covers the whole
// run is written around the span (**`code`**); formatting on only part of it is dropped,
// since it cannot be written inside a code span.
const links = marksForText(child, true);
let marks = marksForText(child);
while (index + 1 < node.childCount) {
const next = node.child(index + 1);
if (!next.isText || !next.marks.some((mark) => mark.type.name === 'code')) break;
const nextLinks = marksForText(next, true);
if (links.length !== nextLinks.length || commonMarkCount(links, nextLinks) !== links.length) break;
const nextKeys = new Set(marksForText(next).map((mark) => mark.key));
marks = marks.filter((mark) => nextKeys.has(mark.key));
text += next.text || '';
index += 1;
}
// Delimiters next to a code span sit between punctuation (the backtick) and the neighbor:
// `**\`x\`**s` is not emphasis when a letter or digit follows, nor `a**\`x\`**` when one
// precedes. Formatting that would open or close against such a neighbor is dropped.
const after = index + 1 < node.childCount ? node.child(index + 1) : null;
const afterKeys = new Set(after?.isText ? marksForText(after).map((mark) => mark.key) : []);
const wordAfter = /^[\p{L}\p{N}_]/u.test(after?.isText ? after.text || '' : '');
const wordBefore = /[\p{L}\p{N}_]$/u.test(result);
marks = marks.filter((mark) => !isDelimiter(mark)
|| !((wordAfter && !afterKeys.has(mark.key)) || (wordBefore && !activeMarks.some((active) => active.key === mark.key))));
transitionMarks(marks);
result += serializeCodeSpan(text);
} else {
if (index === 0) {
text = text.replace(/^[\t\u2003]+/, (whitespace) => '&emsp;'.repeat(whitespace.length));
}
const marks = marksForText(child);
const shared = commonMarkCount(activeMarks, marks);
const leading = marks.slice(shared).some(isDelimiter) ? text.match(/^[ \t\u00a0]+/)?.[0] ?? '' : '';
if (leading) {
// Spaces before newly opened delimiters stay outside them.
transitionMarks(marks.slice(0, shared));
result += leading;
text = text.slice(leading.length);
if (!text) continue;
}
transitionMarks(marks);
text = options.literalDollars
? text.replace(/\\+(?=\$)/g, (backslashes) => backslashes + backslashes)
: text.replace(/\\*\$/g, (match) => '\\'.repeat((match.length - 1) * 2 + 1) + '$');
if (index + 1 < node.childCount && node.child(index + 1).type.name === 'mathInline') {
text = text.replace(/\\+$/, (backslashes) => backslashes + backslashes);
}
result += text;
}
result += text;
return;
continue;
}
transitionMarks([]);
if (child.type.name === 'image') {
const source = normalizeAssetSource(child.attrs.src || '');
if (!source) return;
if (!source) continue;
const alt = child.attrs.alt || '';
const size = child.attrs['data-size'] || child.attrs.size || 'full';
const sizeSuffix = size && size !== 'full' ? `|size=${size}` : '';
if (result) result += '\n';
result += `![${alt}${sizeSuffix}](${source})`;
result += `![${alt}${sizeSuffix}](${source}${serializeImageTitle(child.attrs.title)})`;
} else if (child.type.name === 'mathInline') {
result += `$${child.attrs.tex || ''}$`;
const previous = index > 0 ? node.child(index - 1) : null;
const next = index + 1 < node.childCount ? node.child(index + 1) : null;
const forceEncoded = previous?.type.name === 'mathInline' || next?.type.name === 'mathInline'
|| /\$$/.test(previous?.text || '') || /^[\d$]/.test(next?.text || '');
result += serializeInlineMath(child.attrs.tex || '', forceEncoded);
} else if (child.type.name === 'hardBreak') {
result += ' \n';
}
});
}
transitionMarks([]);
return result;
}
/**
* Like serializeInlineMarkdown, but writes a `$` as typed unless that could change the note's
* math. `inlineMathPayloads` returns the TeX of each inline formula the parser finds in a string;
* if plain dollars would add, remove or re-pair a formula, every `$` is escaped as before.
*/
export function serializeInlineMarkdownKeepingDollars(
node: ProseMirrorNode,
normalizeAssetSource: AssetSourceNormalizer,
inlineMathPayloads: (markdown: string) => string[],
): string {
const escaped = serializeInlineMarkdown(node, normalizeAssetSource);
if (!escaped.includes('$')) return escaped;
const plain = serializeInlineMarkdown(node, normalizeAssetSource, { literalDollars: true });
// A `$$` could start a display formula in a later block, so only single dollars are left alone.
if (plain.includes('$$')) return escaped;
const expected = inlineMathPayloads(escaped);
const actual = inlineMathPayloads(plain);
return actual.length === expected.length && actual.every((tex, index) => tex === expected[index]) ? plain : escaped;
}
+533
View File
@@ -0,0 +1,533 @@
import MarkdownIt from 'markdown-it';
import type Token from 'markdown-it/lib/token.mjs';
import type StateBlock from 'markdown-it/lib/rules_block/state_block.mjs';
import type StateInline from 'markdown-it/lib/rules_inline/state_inline.mjs';
import fenceRule from 'markdown-it/lib/rules_block/fence.mjs';
import markdownItMark from 'markdown-it-mark';
import markdownItSup from 'markdown-it-sup';
import markdownItSub from 'markdown-it-sub';
import { IMG_GRID_CLOSER, parseImgGridHeader, splitTrailingCloser } from './imageGrid.ts';
interface WikiTitle { path: string; title: string }
export interface MarkdownEnvironment { wikiLinkTitles?: readonly WikiTitle[] }
const wikiIndexKey = Symbol('wiki-index');
type WikiIndex = { paths: Map<string, string>; titles: Map<string, { path: string; depth: number }> };
function displayMath(state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
const start = state.bMarks[startLine] + state.tShift[startLine];
if (state.src.slice(start, state.eMarks[startLine]).trim() !== '$$') return false;
let closing = startLine + 1;
for (; closing < endLine; closing++) {
if (state.sCount[closing] < state.blkIndent && !state.isEmpty(closing)) return false;
const from = state.bMarks[closing] + state.tShift[closing];
if (state.src.slice(from, state.eMarks[closing]).trim() === '$$') break;
}
if (closing === endLine) return false;
if (silent) return true;
const token = state.push('helix_math_block', 'div', 0);
token.block = true;
token.content = state.getLines(startLine + 1, closing, state.blkIndent, false).trim();
token.map = [startLine, closing + 1];
token.meta = { sourceStart: start };
token.attrSet('data-math-block', encodeURIComponent(token.content));
token.attrSet('class', 'math-block');
state.line = closing + 1;
return true;
}
function inlineMath(state: StateInline, silent: boolean): boolean {
const start = state.pos;
if (state.src[start] !== '$' || state.src[start - 1] === '$' ||
start + 1 >= state.posMax || /[\s$]/u.test(state.src[start + 1])) return false;
let end = start + 1;
for (; end < state.posMax; end++) {
const char = state.src[end];
if (char === '\n') return false;
if (char === '\\') { end++; continue; }
if (char === '$') break;
}
if (end >= state.posMax || end === start + 1 || /\s/u.test(state.src[end - 1]) ||
state.src[end + 1] === '$' || /[0-9]/.test(state.src[end + 1] ?? '')) return false;
if (!silent) {
const token = state.push('helix_math_inline', 'span', 0);
token.content = state.src.slice(start + 1, end).trim();
token.meta = { from: start, to: end + 1 };
token.attrSet('data-math-inline', encodeURIComponent(token.content));
token.attrSet('class', 'math-inline');
}
state.pos = end + 1;
return true;
}
// markdown-it trims two spaces even when a code span contains only spaces.
// Preserve CommonMark's all-space exception without replacing its normal backtick parser.
function spaceCode(state: StateInline, silent: boolean): boolean {
if (state.src[state.pos] !== '`') return false;
let content = state.pos;
while (content < state.posMax && state.src[content] === '`') content++;
const length = content - state.pos;
let closing = content;
while (closing < state.posMax && (state.src[closing] === ' ' || state.src[closing] === '\n')) closing++;
if (closing === content) return false;
let end = closing;
while (end < state.posMax && state.src[end] === '`') end++;
if (end - closing !== length) return false;
if (!silent) {
const token = state.push('code_inline', 'code', 0);
token.markup = state.src.slice(state.pos, content);
token.content = state.src.slice(content, closing).replace(/\n/g, ' ');
}
state.pos = end;
return true;
}
function installMath(md: MarkdownIt) {
md.block.ruler.before('fence', 'helix_math_block', displayMath, { alt: ['paragraph', 'reference', 'blockquote', 'list'] });
md.inline.ruler.after('backticks', 'helix_math_inline', inlineMath);
md.renderer.rules.helix_math_block = (tokens, index, _options, _env, renderer) =>
`<div${renderer.renderAttrs(tokens[index])}></div>\n`;
md.renderer.rules.helix_math_inline = (tokens, index, _options, _env, renderer) =>
`<span${renderer.renderAttrs(tokens[index])}></span>`;
}
function wikiLink(state: StateInline, silent: boolean): boolean {
const notes: readonly WikiTitle[] | undefined = state.env.wikiLinkTitles;
if (!notes || state.src.slice(state.pos, state.pos + 2) !== '[[') return false;
const end = state.src.indexOf(']]', state.pos + 2);
if (end < 0 || end + 2 > state.posMax) return false;
const raw = state.src.slice(state.pos + 2, end);
if (!raw || raw.includes(']')) return false;
if (!silent) {
let index: WikiIndex | undefined = state.env[wikiIndexKey];
if (!index) {
index = { paths: new Map(), titles: new Map() };
for (const note of notes) {
if (!index.paths.has(note.path)) index.paths.set(note.path, note.path);
const title = note.title.toLowerCase();
let depth = 0;
for (let i = 0; i < note.path.length; i++) if (note.path[i] === '/') depth++;
const previous = index.titles.get(title);
if (!previous || depth < previous.depth) index.titles.set(title, { path: note.path, depth });
}
state.env[wikiIndexKey] = index;
}
const pipe = raw.indexOf('|');
const reference = (pipe < 0 ? raw : raw.slice(0, pipe)).trim();
const display = (pipe < 0 ? reference : raw.slice(pipe + 1)).trim();
const title = reference.replace(/#.*$/, '').replace(/\^.*$/, '').trim();
const basename = title.includes('/') ? title.slice(title.lastIndexOf('/') + 1) : title;
const path = (title.includes('/') ? index.paths.get(title + '.md') : undefined)
?? index.titles.get(basename.toLowerCase())?.path ?? '';
const token = state.push('helix_wiki', 'span', 0);
token.attrSet('data-wiki-link', '');
token.attrSet('data-path', path);
token.attrSet('data-title', reference);
token.attrSet('class', 'wiki-link');
token.content = display;
}
state.pos = end + 2;
return true;
}
/**
* Remove a trailing `"title"` or `'title'` (preceded by whitespace or %20, optionally followed by %20) from a
* legacy link destination. A single scan from the end: the equivalent regular expression is quadratic on a
* long run of spaces.
*/
export function stripLinkTitle(destination: string): string {
const skippableBefore = (index: number) => (index >= 3 && destination.startsWith('%20', index - 3) ? 3 : /\s/u.test(destination[index - 1] ?? '') ? 1 : 0);
let end = destination.length;
for (let width = skippableBefore(end); width; width = skippableBefore(end)) end -= width;
const quote = destination[end - 1];
if (quote !== '"' && quote !== "'") return destination.trim();
const open = destination.lastIndexOf(quote, end - 2);
if (open <= 0) return destination.trim();
let start = open;
for (let width = skippableBefore(start); width; width = skippableBefore(start)) start -= width;
return start === open ? destination.trim() : destination.slice(0, start).trim();
}
/** The title text after a legacy destination (`"title"` or `'title'`, maybe after `>` or %20), or null. */
function legacyImageTitle(rest: string): string | null {
let text = rest.startsWith('>') ? rest.slice(1) : rest;
text = text.replace(/^(?:\s|%20)+/u, '');
while (text.endsWith('%20') || /\s$/u.test(text)) text = text.endsWith('%20') ? text.slice(0, -3) : text.slice(0, -1);
const quote = text[0];
if ((quote !== '"' && quote !== "'") || text.length < 2 || text[text.length - 1] !== quote) return null;
return text.slice(1, -1);
}
/**
* Whether `src[start, end)` is a valid CommonMark destination with an optional title. Such images
* are left to markdown-it's own image rule, which handles escaped quotes and `(title)` correctly.
*/
function isCommonMarkDestination(state: StateInline, start: number, end: number): boolean {
const { helpers } = state.md;
const skipSpace = (pos: number) => {
while (pos < end && /[ \t\n]/u.test(state.src[pos])) pos++;
return pos;
};
let pos = skipSpace(start);
const destination = helpers.parseLinkDestination(state.src, pos, end);
if (!destination.ok) return false;
pos = destination.pos;
const afterDestination = pos;
pos = skipSpace(pos);
if (pos < end && pos !== afterDestination) {
const title = helpers.parseLinkTitle(state.src, pos, end);
if (!title.ok) return false;
pos = skipSpace(title.pos);
}
return pos === end;
}
/**
* Looking for the `)` that ends a destination reads to the end of the line when there is none, and a
* line can hold thousands of `[label](` starts (pasted or generated text), which made parsing it
* quadratic. Two guards keep it linear: no search starts beyond the block's last `)`, and once the
* searches that found nothing have read this many characters in one block, the rest of the block
* is left to the CommonMark link rule.
*/
const LEGACY_LINK_SCAN_BUDGET = 2_000_000;
const legacyLinkScans = new WeakMap<StateInline, { max: number; lastClose: number; wasted: number }>();
/** Preserve legacy space-containing and %20/title destinations only in real link syntax. */
function legacyLink(state: StateInline, silent: boolean): boolean {
const start = state.pos;
const image = state.src[start] === '!' && state.src[start + 1] === '[';
const labelStart = start + (image ? 1 : 0);
if (state.src[labelStart] !== '[') return false;
const labelEnd = state.md.helpers.parseLinkLabel(state, labelStart, !image);
if (labelEnd < 0 || state.src[labelEnd + 1] !== '(') return false;
const destinationStart = labelEnd + 2;
let scans = legacyLinkScans.get(state);
if (!scans || scans.max < state.posMax) {
scans = { max: state.posMax, lastClose: state.src.lastIndexOf(')', state.posMax - 1), wasted: scans?.wasted ?? 0 };
legacyLinkScans.set(state, scans);
}
if (destinationStart > scans.lastClose || scans.wasted > LEGACY_LINK_SCAN_BUDGET) return false;
let end = destinationStart;
let depth = 0;
let quote = '';
let angle = false;
for (; end < state.posMax; end++) {
const char = state.src[end];
if (char === '\n') { scans.wasted += end - destinationStart; return false; }
if (char === '\\') { end++; continue; }
if (quote) { if (char === quote) quote = ''; continue; }
if (angle) { if (char === '>') angle = false; continue; }
if (char === '<') { angle = true; continue; }
if ((char === '"' || char === "'") &&
(/\s/u.test(state.src[end - 1]) || state.src.slice(end - 3, end) === '%20')) { quote = char; continue; }
if (char === '(') depth++;
else if (char === ')') { if (!depth) break; depth--; }
}
if (end >= state.posMax) { scans.wasted += end - destinationStart; return false; }
const raw = state.src.slice(destinationStart, end);
// Links and images with a valid CommonMark destination and title keep the native parser (and
// their title). A `%20"title"` suffix (exported by some apps) is still legacy syntax, not part of the path.
if (!/%20["'(]/u.test(raw) && isCommonMarkDestination(state, destinationStart, end)) return false;
// Native CommonMark links, including nested destinations, retain their normal parser.
if (!/\s|%20["'(]/u.test(raw)) return false;
let destination = raw.trim();
if (destination.startsWith('<')) {
const angle = destination.match(/^<([^>]*)>(?:(?:\s|%20)*(?:"[^"]*"|'[^']*'|\([^)]*\)))?(?:\s|%20)*$/u);
if (!angle) return false;
destination = angle[1];
} else {
destination = stripLinkTitle(destination);
}
const href = state.md.normalizeLink(destination.replace(/ /g, '%20'));
if (!state.md.validateLink(href)) return false;
if (!silent) {
const label = state.src.slice(labelStart + 1, labelEnd);
const from = destinationStart + raw.indexOf(destination);
const sourceHref = { from, to: from + destination.length };
if (image) {
const children: Token[] = [];
state.md.inline.parse(label, state.md, state.env, children);
const token = state.push('image', 'img', 0);
token.meta = { sourceHref };
token.attrSet('src', href.replace(/^\/{2,}(?=home\/)/u, '/'));
token.attrSet('alt', '');
const title = legacyImageTitle(raw.slice(raw.indexOf(destination) + destination.length));
if (title) token.attrSet('title', state.md.utils.unescapeAll(title));
token.children = children;
token.content = label;
} else {
const token = state.push('link_open', 'a', 1);
token.meta = { sourceHref };
token.attrSet('href', href);
const title = legacyImageTitle(raw.slice(raw.indexOf(destination) + destination.length));
if (title) token.attrSet('title', state.md.utils.unescapeAll(title));
const oldMax = state.posMax;
state.pos = labelStart + 1;
state.posMax = labelEnd;
const linkState = state as StateInline & { linkLevel: number };
linkState.linkLevel++;
state.md.inline.tokenize(state);
linkState.linkLevel--;
state.push('link_close', 'a', -1);
state.posMax = oldMax;
}
}
state.pos = end + 1;
return true;
}
function emptyParagraph(state: StateBlock, startLine: number, _endLine: number, silent: boolean): boolean {
const from = state.bMarks[startLine] + state.tShift[startLine];
if (state.src.slice(from, state.eMarks[startLine]).trim() !== '<!-- -->') return false;
if (silent) return true;
const token = state.push('helix_empty', 'p', 0);
token.block = true;
token.map = [startLine, startLine + 1];
state.line = startLine + 1;
return true;
}
/** True when `text` is exactly one Markdown image and nothing else (no caption, no link around it). */
function isSingleImage(state: StateBlock, text: string): boolean {
if (!text.startsWith('![')) return false;
const tokens: Token[] = [];
state.md.inline.parse(text, state.md, state.env, tokens);
return tokens.length === 1 && tokens[0].type === 'image';
}
/**
* `:::img-grid` image grids (see imageGrid.ts). As a block rule, it never sees lines inside code
* blocks. A block that isn't a well-formed grid is left completely to the other rules.
*/
function imageGrid(state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean {
if (state.sCount[startLine] - state.blkIndent >= 4) return false;
const lineText = (line: number) => state.src.slice(state.bMarks[line] + state.tShift[line], state.eMarks[line]);
const header = lineText(startLine);
if (!header.startsWith(':::img-grid')) return false;
const attrs = parseImgGridHeader(header);
if (!attrs) return false;
const images: { line: number; content: string }[] = [];
let closing = -1;
for (let line = startLine + 1; line < endLine && closing < 0; line++) {
if (state.isEmpty(line) || state.sCount[line] < state.blkIndent) return false;
const text = lineText(line).replace(/[ \t]+$/, '');
if (text === IMG_GRID_CLOSER) {
closing = line;
break;
}
const beforeCloser = splitTrailingCloser(text);
const image = beforeCloser ?? text;
if (!isSingleImage(state, image)) return false;
images.push({ line, content: image });
if (beforeCloser !== null) closing = line;
}
if (closing < 0 || images.length === 0) return false;
if (silent) return true;
const open = state.push('helix_img_grid_open', 'div', 1);
open.block = true;
open.map = [startLine, closing + 1];
open.attrSet('data-img-grid', '');
open.attrSet('data-cols', String(attrs.cols));
open.attrSet('data-layout', attrs.layout);
open.attrSet('class', 'img-grid');
for (const image of images) {
const inline = state.push('inline', '', 0);
inline.content = image.content;
inline.map = [image.line, image.line + 1];
inline.children = [];
}
state.push('helix_img_grid_close', 'div', -1).block = true;
state.line = closing + 1;
return true;
}
export function createEditorMarkdownParser(): MarkdownIt {
const md = MarkdownIt({ html: true, linkify: false, breaks: false })
.use(markdownItMark).use(markdownItSup).use(markdownItSub);
// Existing editor convention: tab-indented prose does not become an indented code block.
md.disable('code');
installMath(md);
md.inline.ruler.before('link', 'helix_wiki', wikiLink);
md.inline.ruler.before('backticks', 'helix_space_code', spaceCode);
md.inline.ruler.before('link', 'helix_legacy_link', legacyLink);
md.block.ruler.before('html_block', 'helix_empty', emptyParagraph, { alt: ['paragraph', 'reference', 'blockquote', 'list'] });
md.block.ruler.before('fence', 'helix_img_grid', imageGrid, { alt: ['paragraph', 'reference', 'blockquote', 'list'] });
md.renderer.rules.helix_empty = () => '<p></p>\n';
md.renderer.rules.helix_wiki = (tokens, index, _options, _env, renderer) =>
`<span${renderer.renderAttrs(tokens[index])}>${md.utils.escapeHtml(tokens[index].content)}</span>`;
md.core.ruler.before('block', 'helix_wiki_index', state => { delete state.env[wikiIndexKey]; });
md.core.ruler.before('inline', 'helix_tasks', state => {
const lists: { open: Token; firstItem: boolean; item: Token | null; firstParagraph: boolean }[] = [];
for (const token of state.tokens) {
if (token.type === 'bullet_list_open' || token.type === 'ordered_list_open') {
lists.push({ open: token, firstItem: true, item: null, firstParagraph: true });
} else if (token.type === 'bullet_list_close' || token.type === 'ordered_list_close') lists.pop();
else if (token.type === 'list_item_open' && lists.length) {
const list = lists[lists.length - 1];
list.item = token;
list.firstParagraph = true;
} else if (token.type === 'list_item_close' && lists.length) {
const list = lists[lists.length - 1];
list.item = null;
list.firstItem = false;
} else if (token.type === 'inline' && lists.length) {
const list = lists[lists.length - 1];
if (!list.item || !list.firstParagraph || token.level !== list.item.level + 2) continue;
list.firstParagraph = false;
// Task items only exist in bullet lists; `1. [x] text` stays an ordinary numbered item.
if (list.open.type !== 'bullet_list_open') continue;
const checkbox = token.content.match(/^\[(x| )\](?:[^\S\n]+|$)/u);
if (!checkbox) continue;
token.content = token.content.slice(checkbox[0].length);
list.item.attrSet('data-type', 'taskItem');
list.item.attrSet('data-checked', checkbox[1] === 'x' ? 'true' : 'false');
if (list.firstItem) list.open.attrSet('data-type', 'taskList');
}
}
});
md.core.ruler.after('inline', 'helix_images', state => {
let paragraph: Token | null = null;
let cell: Token | null = null;
// Tight list items render no <p>, so a simple item (open, hidden paragraph, inline, close) carries the marker.
const items: { token: Token; index: number; hasCode: boolean }[] = [];
let lines: string[] | undefined;
for (const [index, token] of state.tokens.entries()) {
if (token.type === 'paragraph_open' || token.type === 'heading_open') paragraph = token;
else if (token.type === 'paragraph_close' || token.type === 'heading_close') paragraph = null;
else if (token.type === 'th_open' || token.type === 'td_open') cell = token;
else if (token.type === 'th_close' || token.type === 'td_close') cell = null;
else if (token.type === 'list_item_open') items.push({ token, index, hasCode: false });
else if (token.type === 'list_item_close') {
const item = items.pop();
if (item?.hasCode && index - item.index === 4) item.token.attrSet('data-helix-code', '');
}
if (token.type !== 'inline') continue;
for (const child of token.children ?? []) {
// ProseMirror's DOM parser otherwise collapses or drops whitespace-only code spans.
if (child.type === 'code_inline') {
child.attrSet('style', 'white-space: pre-wrap');
paragraph?.attrSet('data-helix-code', '');
cell?.attrSet('data-helix-code', '');
if (paragraph?.hidden && items.length) items[items.length - 1].hasCode = true;
}
if (child.type === 'image') {
const src = child.attrGet('src');
if (src?.startsWith('//')) child.attrSet('src', src.replace(/^\/{2,}(?=home\/)/u, '/'));
}
}
if (!paragraph || paragraph.type !== 'paragraph_open' || paragraph.level !== 0 || token.children?.length !== 1 ||
token.children[0].type !== 'image' || !token.map || token.map[0] < 2) continue;
lines ??= state.src.split('\n');
const line = token.map[0];
if (lines[line].startsWith('![') && lines[line - 1].trim() === '') paragraph.meta = { helixImageGap: true };
}
});
// Callout rendering splits the title line from the body at the first newline of the opening
// paragraph, so that one soft break must stay a newline instead of becoming a space.
md.core.ruler.after('inline', 'helix_callout_title', state => {
for (let index = 0; index + 2 < state.tokens.length; index++) {
const [quote, paragraph, inline] = [state.tokens[index], state.tokens[index + 1], state.tokens[index + 2]];
if (quote.type !== 'blockquote_open' || paragraph.type !== 'paragraph_open' || inline.type !== 'inline') continue;
if (!/^\[![\w-]+\]/u.test(inline.content)) continue;
const softbreak = inline.children?.find(child => child.type === 'softbreak');
if (softbreak) softbreak.meta = { helixCalloutTitle: true };
}
});
md.renderer.rules.paragraph_open = (tokens, index, options, _env, renderer) =>
(tokens[index].meta?.helixImageGap ? '<p></p>\n' : '') + renderer.renderToken(tokens, index, options);
md.block.ruler.at('fence', (state, startLine, endLine, silent) => {
if (!fenceRule(state, startLine, endLine, silent)) return false;
if (silent) return true;
const token = state.tokens[state.tokens.length - 1];
if (token.info.trim() === 'helix-secret' && token.map) {
const line = token.map[1] - 1;
const closing = state.src.slice(state.bMarks[line] + state.tShift[line], state.eMarks[line]);
let length = 0;
while (closing[length] === token.markup[0]) length++;
const indentation = state.sCount[line] - state.blkIndent;
token.meta = { helixSecretClosed: indentation >= 0 && indentation < 4 &&
length >= token.markup.length && /^[ \t]*$/u.test(closing.slice(length)) };
}
return true;
}, { alt: ['paragraph', 'reference', 'blockquote', 'list'] });
const renderFence = md.renderer.rules.fence!;
md.renderer.rules.fence = (tokens, index, options, env, renderer) => {
const token = tokens[index];
if (token.info.trim() === 'helix-secret' && token.meta?.helixSecretClosed) {
return `<div data-secret-block="${md.utils.escapeHtml(encodeURIComponent(token.content.replace(/\n$/, '')))}"></div>\n`;
}
// Only the final structural LF is parser-added; leading and extra trailing blank lines are literal.
return renderFence(tokens, index, options, env, renderer).replace(/\n<\/code>/u, '</code>');
};
md.renderer.rules.softbreak = (tokens, index) => (tokens[index].meta?.helixCalloutTitle ? '\n' : ' ');
md.renderer.rules.hardbreak = () => '<br>';
return md;
}
const mathBlocks = MarkdownIt({ html: true });
installMath(mathBlocks);
mathBlocks.inline.ruler.before('link', 'helix_legacy_link', legacyLink);
/**
* Inline tokens of each blank-line-separated block, parsed on its own like the editor does, with
* source ranges relative to the whole note. Inline syntax such as a code span cannot pair across
* blocks, so one stray backtick must not hide the links and formulas after it.
*/
function inlineTokensByBlock(source: string): Token[] {
const tokens: Token[] = [];
let start = 0;
let offset = 0;
const flush = (end: number) => {
if (end <= start) return;
const found: Token[] = [];
mathBlocks.inline.parse(source.slice(start, end), mathBlocks, {}, found);
for (const token of found) {
if (token.meta?.sourceHref) {
const { from, to } = token.meta.sourceHref;
token.meta = { ...token.meta, sourceHref: { from: from + start, to: to + start } };
} else if (token.type === 'helix_math_inline') {
token.meta = { ...token.meta, from: token.meta.from + start, to: token.meta.to + start };
}
tokens.push(token);
}
};
for (const line of source.split('\n')) {
if (/^[ \t\r]*$/.test(line)) {
flush(offset);
start = offset + line.length + 1;
}
offset += line.length + 1;
}
flush(source.length);
return tokens;
}
/** Source destinations for the editor's legacy space/title link compatibility. */
export function legacyLinkRanges(source: string): { from: number; to: number }[] {
if (!source.includes('](')) return [];
return inlineTokensByBlock(source).filter(token => token.meta?.sourceHref).map(token => token.meta.sourceHref);
}
/** Inline math coordinates use the same delimiter and escape rules as rendering. */
export function inlineMathRanges(source: string): { from: number; to: number }[] {
if (!source.includes('$')) return [];
return inlineTokensByBlock(source).filter(token => token.type === 'helix_math_inline').map(token => token.meta);
}
/** Source coordinates of semantic display-math blocks, including nested containers. */
export function displayMathRanges(source: string): { from: number; to: number; prefix: string }[] {
if (!source.includes('$$')) return [];
const tokens: Token[] = [];
mathBlocks.block.parse(source, mathBlocks, {}, tokens);
const ranges = tokens.filter(token => token.type === 'helix_math_block' && token.map);
if (!ranges.length) return [];
const starts = [0];
for (let i = 0; i < source.length; i++) if (source[i] === '\n') starts.push(i + 1);
return ranges.map(token => {
const from = starts[token.map![0]];
return { from, to: starts[token.map![1]] ?? source.length,
prefix: source.slice(from, token.meta.sourceStart) };
});
}
+5
View File
@@ -0,0 +1,5 @@
declare module 'markdown-it/lib/rules_block/fence.mjs' {
import type StateBlock from 'markdown-it/lib/rules_block/state_block.mjs';
const fence: (state: StateBlock, startLine: number, endLine: number, silent: boolean) => boolean;
export default fence;
}
+129
View File
@@ -0,0 +1,129 @@
import { markdownLanguage } from '@codemirror/lang-markdown';
import { MapMode, type ChangeDesc, type EditorState } from '@codemirror/state';
import type { Node as ProseMirrorNode } from '@tiptap/pm/model';
import type { Mapping } from '@tiptap/pm/transform';
import MarkdownIt from 'markdown-it';
import type Token from 'markdown-it/lib/token.mjs';
import markdownItMark from 'markdown-it-mark';
import markdownItSub from 'markdown-it-sub';
import markdownItSup from 'markdown-it-sup';
import { displayMathRanges } from './markdownParser.ts';
export interface OutlineHeading {
level: number;
text: string;
pos: number;
end: number;
}
const inlineMarkdown = MarkdownIt({ html: true, linkify: false, breaks: false })
.use(markdownItMark)
.use(markdownItSub)
.use(markdownItSup);
function inlineText(tokens: readonly Token[]): string {
let text = '';
for (const token of tokens) {
if (token.children) text += inlineText(token.children);
else if (token.type === 'text' || token.type === 'code_inline') text += token.content;
else if (token.type === 'softbreak' || token.type === 'hardbreak' ||
(token.type === 'html_inline' && /^<br\s*\/?\s*>$/i.test(token.content))) text += ' ';
}
return text;
}
export function documentHeadings(doc: ProseMirrorNode): OutlineHeading[] {
const headings: OutlineHeading[] = [];
doc.descendants((node, pos) => {
if (node.type.name !== 'heading') return;
headings.push({ level: node.attrs.level, text: node.textContent, pos, end: pos + node.nodeSize });
return false;
});
return headings;
}
/** Match the rich editor's closed $$ blocks without shifting source coordinates. */
function maskDisplayMath(markdown: string): string {
const ranges = displayMathRanges(markdown);
if (!ranges.length) return markdown;
const parts: string[] = [];
let copied = 0;
for (const { from, to, prefix } of ranges) {
parts.push(markdown.slice(copied, from), prefix, markdown.slice(from + prefix.length, to).replace(/[^\n]/g, ' '));
copied = to;
}
parts.push(markdown.slice(copied));
return parts.join('');
}
/** Markdown headings only: raw HTML heading blocks are not interpreted as headings. */
export function sourceHeadings(state: EditorState): OutlineHeading[] {
const headings: OutlineHeading[] = [];
// Parse the complete document, independent of CodeMirror's viewport/time-limited syntax tree.
const markdown = state.doc.toString();
const parsedMarkdown = maskDisplayMath(markdown);
const tree = markdownLanguage.parser.parse(parsedMarkdown);
const environment = {};
// Resolve reference labels only when the document can contain link definitions.
if (parsedMarkdown.includes(']:')) inlineMarkdown.parse(parsedMarkdown, environment);
tree.iterate({
enter(node) {
if (!/^(?:ATX|Setext)Heading[1-6]$/.test(node.name)) return;
const setext = node.name.startsWith('SetextHeading');
const cursor = node.node.cursor();
let from = node.from;
let label = '';
do {
if (cursor.name !== 'HeaderMark' && cursor.name !== 'QuoteMark' && cursor.name !== 'ListMark') continue;
// Setext's underline and its enclosing quote/list prefix are not label content.
const to = setext && cursor.name === 'HeaderMark'
? state.doc.lineAt(cursor.from).from : cursor.from;
label += state.sliceDoc(from, Math.max(from, to));
from = cursor.to;
} while (cursor.next() && cursor.from < node.to);
if (!setext) label += state.sliceDoc(from, node.to);
headings.push({
level: Number(node.name.at(-1)),
text: inlineText(inlineMarkdown.parseInline(label, environment)).replace(/\s+/gu, ' ').trim(),
pos: node.from,
end: node.to,
});
return false;
},
});
return headings;
}
/** Headings are in document order; body content remains associated with the preceding heading. */
export function activeHeadingIndex(headings: readonly OutlineHeading[], pos: number): number {
let low = 0;
let high = headings.length;
while (low < high) {
const middle = (low + high) >>> 1;
if (headings[middle].pos <= pos) low = middle + 1;
else high = middle;
}
return low - 1;
}
/** Temporary anchors only; validate against fresh extraction before navigating. */
export function mapSourceHeadings(headings: readonly OutlineHeading[], changes: ChangeDesc): OutlineHeading[] {
const mapped: OutlineHeading[] = [];
for (const heading of headings) {
const pos = changes.mapPos(heading.pos, 1, MapMode.TrackAfter);
const end = changes.mapPos(heading.end, -1);
if (pos !== null && pos < end) mapped.push({ ...heading, pos, end });
}
return mapped;
}
/** A replaced opening token no longer identifies the original semantic heading. */
export function mapDocumentHeadings(headings: readonly OutlineHeading[], mapping: Mapping): OutlineHeading[] {
const mapped: OutlineHeading[] = [];
for (const heading of headings) {
const start = mapping.mapResult(heading.pos, 1);
const end = mapping.map(heading.end, -1);
if (!start.deleted && start.pos < end) mapped.push({ ...heading, pos: start.pos, end });
}
return mapped;
}
+2
View File
@@ -77,6 +77,7 @@ export class SourceController {
constructor(onChange: (changes: ChangeDesc, edited: boolean) => void = () => {}, events: {
paste?: (event: ClipboardEvent) => boolean;
drop?: (event: DragEvent) => boolean;
scroll?: () => void;
} = {}) {
this.onChange = onChange;
this.extensions = [
@@ -95,6 +96,7 @@ export class SourceController {
},
paste: (event, view) => !view.state.readOnly && (events.paste?.(event) ?? false),
drop: (event, view) => !view.state.readOnly && (events.drop?.(event) ?? false),
scroll: () => { events.scroll?.(); return false; },
}),
this.configuration.of(this.settings()),
];
+1
View File
@@ -113,6 +113,7 @@ export interface AppConfig {
system_dark_theme: string;
accent_color: string | null;
font_size: number | null;
preferred_font_size?: number | null;
scroll_to_change_font_size: boolean;
toolbar_layout: ToolbarLayout | null;
font_family: string | null;
+103
View File
@@ -0,0 +1,103 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
classifyClipboardType,
collectClipboardPayload,
isEmptyPayload,
needsNativeClipboard,
pastedFileName,
plainTextFromUriList,
withNativeClipboard
} from './clipboard-paste.ts';
const item = entries => ({
types: Object.keys(entries),
getType: async type => {
const value = entries[type];
return value instanceof Blob ? value : new Blob([value], { type });
}
});
const png = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: 'image/png' });
test('classifies text formats as text, attachments as files and metadata as ignored', () => {
assert.equal(classifyClipboardType('text/plain'), 'plain');
assert.equal(classifyClipboardType('text/plain;charset=utf-8'), 'plain');
assert.equal(classifyClipboardType('TEXT/HTML'), 'html');
for (const type of ['text/uri-list', 'text/rtf', 'text/x-moz-url', 'text/csv']) {
assert.equal(classifyClipboardType(type), 'string', type);
}
for (const type of ['image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf']) {
assert.equal(classifyClipboardType(type), 'file', type);
}
for (const type of ['web application/x-custom', 'chromium/x-web-custom-data', 'application/x-moz-nativeimage', 'application/octet-stream', '']) {
assert.equal(classifyClipboardType(type), 'ignore', type);
}
});
test('a copied link stays text and never becomes a uri-list attachment', async () => {
const payload = await collectClipboardPayload([item({ 'text/uri-list': 'https://example.com/a\r\n' })]);
assert.equal(payload.files.length, 0);
assert.equal(payload.text, 'https://example.com/a');
assert.deepEqual(payload.strings, [['text/uri-list', 'https://example.com/a\r\n']]);
});
test('text/plain wins over the uri-list fallback', async () => {
const payload = await collectClipboardPayload([item({ 'text/plain': 'Example', 'text/uri-list': 'https://example.com' })]);
assert.equal(payload.text, 'Example');
assert.equal(payload.files.length, 0);
});
test('uri-list comments and blank lines are skipped', () => {
assert.equal(plainTextFromUriList('# copied from browser\n\n https://a.example \nhttps://b.example'), 'https://a.example');
assert.equal(plainTextFromUriList('# only a comment\n'), '');
});
test('images and PDFs become named files alongside the text and HTML', async () => {
const pdf = new Blob(['%PDF-1.7'], { type: 'application/pdf' });
const payload = await collectClipboardPayload([
item({ 'text/html': '<img src="https://example.com/cat.png">', 'image/png': png }),
item({ 'application/pdf': pdf })
]);
assert.equal(payload.html, '<img src="https://example.com/cat.png">');
assert.deepEqual(payload.files.map(file => [file.name, file.type]), [
['pasted-image.png', 'image/png'],
['pasted-file-2.pdf', 'application/pdf']
]);
assert.equal(isEmptyPayload(payload), false);
});
test('metadata-only clipboards are empty and are never read', async () => {
const read = [];
const payload = await collectClipboardPayload([{
types: ['chromium/x-web-custom-data', 'web application/x-custom'],
getType: async type => { read.push(type); return new Blob(['x']); }
}]);
assert.deepEqual(read, []);
assert.equal(isEmptyPayload(payload), true);
});
test('pasted file names use friendly extensions', () => {
assert.equal(pastedFileName('image/jpeg', 0), 'pasted-image.jpg');
assert.equal(pastedFileName('image/svg+xml', 1), 'pasted-image-2.svg');
assert.equal(pastedFileName('application/pdf', 0), 'pasted-file.pdf');
});
test('formatting from other apps survives when the web clipboard only offers plain text (P6)', async () => {
// WebKitGTK: navigator.clipboard.read() gives just text/plain for a web page copied in a browser.
const web = await collectClipboardPayload([item({ 'text/plain': 'Bold link' })]);
assert.equal(needsNativeClipboard(web), true);
const merged = withNativeClipboard(web, { text: 'Bold link (native)', html: '<b>Bold</b> <a href="https://x.example">link</a>' });
assert.equal(merged.html, '<b>Bold</b> <a href="https://x.example">link</a>');
assert.equal(merged.text, 'Bold link', 'web text is kept when present');
});
test('a failed web clipboard read falls back to native text and HTML entirely', () => {
assert.equal(needsNativeClipboard(null), true);
assert.deepEqual(withNativeClipboard(null, { text: 'plain', html: null }), { text: 'plain', html: '', strings: [], files: [] });
assert.equal(isEmptyPayload(withNativeClipboard(null, {})), true, 'nothing textual: the native image read comes next');
});
test('the native clipboard is not consulted when the web clipboard already has HTML or files', async () => {
assert.equal(needsNativeClipboard(await collectClipboardPayload([item({ 'text/html': '<b>x</b>' })])), false);
assert.equal(needsNativeClipboard(await collectClipboardPayload([item({ 'image/png': png })])), false);
});
+117
View File
@@ -0,0 +1,117 @@
/**
* Turns what the async clipboard API (`navigator.clipboard.read()`) returns into the data a keyboard
* paste would carry, so the editor's context-menu Paste can replay it as a real paste event.
*
* The clipboard holds many representations of one copy: plain text, HTML, URI lists, bitmaps, and
* browser/OS bookkeeping formats. Only actual attachments may become files. Text formats such as
* `text/uri-list` stay text, and unknown metadata is dropped. Turning them into files would insert
* junk attachments like `pasted-0.uri-list`.
*/
/** How one clipboard MIME type is pasted. */
export type ClipboardTypeRole = 'plain' | 'html' | 'string' | 'file' | 'ignore';
/** Attachment types a keyboard paste would deliver as files (see `insertFile` in Editor.svelte). */
const FILE_TYPES = /^(?:image\/[\w.+-]+|application\/pdf)$/;
/** Strips parameters (`text/plain;charset=utf-8`) and case. */
export function normalizeClipboardType(type: string): string {
return type.split(';')[0].trim().toLowerCase();
}
export function classifyClipboardType(type: string): ClipboardTypeRole {
const mime = normalizeClipboardType(type);
if (mime === 'text/plain') return 'plain';
if (mime === 'text/html') return 'html';
// Other text formats (uri-list, rtf, x-moz-url, ...) are string data on the paste event, never files.
if (/^text\/[\w.+-]+$/.test(mime)) return 'string';
if (FILE_TYPES.test(mime)) return 'file';
// `web ...` custom formats, chromium/..., application/x-..., and anything else unrecognized.
return 'ignore';
}
/** First URI of an RFC 2483 `text/uri-list`. Lines starting with `#` are comments. */
export function plainTextFromUriList(list: string): string {
for (const line of list.split(/\r?\n/)) {
const uri = line.trim();
if (uri && !uri.startsWith('#')) return uri;
}
return '';
}
const EXTENSIONS: Record<string, string> = { 'image/jpeg': 'jpg', 'image/svg+xml': 'svg', 'application/pdf': 'pdf' };
/** File name for a pasted attachment, matching the `pasted-image.png` name of the native image fallback. */
export function pastedFileName(type: string, index: number): string {
const mime = normalizeClipboardType(type);
const extension = EXTENSIONS[mime] ?? (mime.split('/')[1]?.replace(/[^\w]/g, '') || 'bin');
const base = mime.startsWith('image/') ? 'pasted-image' : 'pasted-file';
return `${base}${index > 0 ? `-${index + 1}` : ''}.${extension}`;
}
export interface ClipboardItemLike {
readonly types: readonly string[];
getType(type: string): Promise<Blob>;
}
export interface ClipboardPastePayload {
text: string;
html: string;
/** Other text formats, keyed by normalized MIME type. */
strings: [type: string, value: string][];
files: File[];
}
/** Web clipboard data still missing formats a keyboard paste would carry, so the native clipboard is needed. */
export function needsNativeClipboard(payload: ClipboardPastePayload | null): boolean {
return !payload || (!payload.html && payload.files.length === 0);
}
/**
* Fill in what the web clipboard didn't provide from the native clipboard. WebKitGTK often returns
* nothing, or only `text/plain`, for content copied from other applications; Ctrl+V would still
* have received the HTML (formatting, links), so a paste without it is not the same paste.
*/
export function withNativeClipboard(
payload: ClipboardPastePayload | null,
native: { text?: string | null; html?: string | null },
): ClipboardPastePayload {
return {
text: payload?.text || native.text || '',
html: payload?.html || native.html || '',
strings: payload?.strings ?? [],
files: payload?.files ?? [],
};
}
export function isEmptyPayload(payload: ClipboardPastePayload): boolean {
return !payload.text && !payload.html && payload.files.length === 0;
}
/** Reads every clipboard item into paste data, classifying each representation. */
export async function collectClipboardPayload(items: readonly ClipboardItemLike[]): Promise<ClipboardPastePayload> {
const payload: ClipboardPastePayload = { text: '', html: '', strings: [], files: [] };
for (const item of items) {
for (const type of item.types) {
const role = classifyClipboardType(type);
if (role === 'ignore') continue;
const blob = await item.getType(type);
if (role === 'file') {
const mime = normalizeClipboardType(type);
payload.files.push(new File([blob], pastedFileName(mime, payload.files.length), { type: mime }));
continue;
}
const value = await blob.text();
if (!value) continue;
if (role === 'plain') payload.text ||= value;
else if (role === 'html') payload.html ||= value;
else payload.strings.push([normalizeClipboardType(type), value]);
}
}
// Some apps offer a copied link only as a URI list. Paste the link as text instead of nothing.
if (!payload.text) {
const uriList = payload.strings.find(([type]) => type === 'text/uri-list');
if (uriList) payload.text = plainTextFromUriList(uriList[1]);
}
return payload;
}
+97
View File
@@ -0,0 +1,97 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { buildGraphTopology, getGraphNeighborhood } from './graph-topology.ts';
const edge = (sourceIdx, targetIdx) => ({ sourceIdx, targetIdx });
function scannedNeighborhood(nodeIdx, edges) {
const nodeSet = new Set();
const edgeSet = new Set();
if (nodeIdx < 0) return { nodeSet, edgeSet };
nodeSet.add(nodeIdx);
edges.forEach((e, i) => {
if (e.sourceIdx === nodeIdx || e.targetIdx === nodeIdx) {
edgeSet.add(i);
nodeSet.add(e.sourceIdx);
nodeSet.add(e.targetIdx);
}
});
return { nodeSet, edgeSet };
}
test('topology retains duplicate edges and counts self-edges twice in degree but once in adjacency', () => {
const edges = [edge(0, 1), edge(0, 1), edge(1, 0), edge(0, 0), edge(2, 2)];
const topology = buildGraphTopology(4, edges);
assert.deepEqual(topology.incidentEdges, [[0, 1, 2, 3], [0, 1, 2], [4], []]);
assert.deepEqual(topology.nodeDegree, [5, 3, 2, 0]);
assert.deepEqual(topology.connectedSet, new Set([0, 1, 2]));
assert.deepEqual(getGraphNeighborhood(0, edges, topology.incidentEdges), {
nodeSet: new Set([0, 1]),
edgeSet: new Set([0, 1, 2, 3]),
});
});
test('changing active nodes produces only direct neighbors without changing prior sets', () => {
const edges = [edge(0, 1), edge(2, 1), edge(2, 3), edge(3, 4)];
const { incidentEdges } = buildGraphTopology(6, edges);
const first = getGraphNeighborhood(0, edges, incidentEdges);
const second = getGraphNeighborhood(2, edges, incidentEdges);
assert.deepEqual(first, { nodeSet: new Set([0, 1]), edgeSet: new Set([0]) });
assert.deepEqual(second, { nodeSet: new Set([2, 1, 3]), edgeSet: new Set([1, 2]) });
assert.deepEqual(getGraphNeighborhood(5, edges, incidentEdges), {
nodeSet: new Set([5]), edgeSet: new Set(),
});
assert.deepEqual(getGraphNeighborhood(-1, edges, incidentEdges), {
nodeSet: new Set(), edgeSet: new Set(),
});
});
test('rebuilding topology uses the replacement edges for the same active node', () => {
const beforeEdges = [edge(0, 1)];
const afterEdges = [edge(2, 0), edge(0, 0)];
const before = buildGraphTopology(3, beforeEdges);
const after = buildGraphTopology(3, afterEdges);
assert.deepEqual(getGraphNeighborhood(0, beforeEdges, before.incidentEdges), {
nodeSet: new Set([0, 1]), edgeSet: new Set([0]),
});
assert.deepEqual(getGraphNeighborhood(0, afterEdges, after.incidentEdges), {
nodeSet: new Set([0, 2]), edgeSet: new Set([0, 1]),
});
});
test('indexed neighborhoods match full scans for directed, duplicate and self edges', () => {
const edges = Array.from({ length: 150 }, (_, i) => edge(i % 13, (i * 7 + 3) % 13));
edges.push(edge(0, 0), edge(0, 1), edge(0, 1));
const { incidentEdges } = buildGraphTopology(14, edges);
for (let nodeIdx = -1; nodeIdx < 14; nodeIdx++) {
assert.deepEqual(getGraphNeighborhood(nodeIdx, edges, incidentEdges), scannedNeighborhood(nodeIdx, edges));
}
});
test('construction reads each endpoint once and neighborhood work visits only incident edges', () => {
let endpointReads = 0;
const pairs = [[0, 1], [0, 0], [2, 0], ...Array.from({ length: 1000 }, () => [2, 3])];
const edges = pairs.map(([source, target]) => ({
get sourceIdx() { endpointReads++; return source; },
get targetIdx() { endpointReads++; return target; },
}));
const { incidentEdges } = buildGraphTopology(5, edges);
assert.equal(endpointReads, edges.length * 2);
endpointReads = 0;
assert.deepEqual(getGraphNeighborhood(0, edges, incidentEdges), {
nodeSet: new Set([0, 1, 2]), edgeSet: new Set([0, 1, 2]),
});
assert.equal(endpointReads, 6);
endpointReads = 0;
getGraphNeighborhood(4, edges, incidentEdges);
getGraphNeighborhood(-1, edges, incidentEdges);
assert.equal(endpointReads, 0);
});
test('an empty graph has no degree, adjacency or neighborhood entries', () => {
const topology = buildGraphTopology(0, []);
assert.deepEqual(topology, { incidentEdges: [], nodeDegree: [], connectedSet: new Set() });
assert.deepEqual(getGraphNeighborhood(-1, [], topology.incidentEdges), {
nodeSet: new Set(), edgeSet: new Set(),
});
});
+40
View File
@@ -0,0 +1,40 @@
interface GraphTopologyEdge {
sourceIdx: number;
targetIdx: number;
}
export function buildGraphTopology(nodeCount: number, edges: readonly GraphTopologyEdge[]) {
const incidentEdges: number[][] = Array.from({ length: nodeCount }, () => []);
const nodeDegree = new Array<number>(nodeCount).fill(0);
const connectedSet = new Set<number>();
for (let i = 0; i < edges.length; i++) {
const { sourceIdx, targetIdx } = edges[i];
connectedSet.add(sourceIdx);
connectedSet.add(targetIdx);
nodeDegree[sourceIdx]++;
nodeDegree[targetIdx]++;
incidentEdges[sourceIdx].push(i);
// A self-edge contributes twice to degree, but only once to the neighborhood.
if (targetIdx !== sourceIdx) incidentEdges[targetIdx].push(i);
}
return { incidentEdges, nodeDegree, connectedSet };
}
export function getGraphNeighborhood(
nodeIdx: number,
edges: readonly GraphTopologyEdge[],
incidentEdges: readonly (readonly number[])[],
) {
const nodeSet = new Set<number>();
const edgeSet = new Set<number>();
const incident = incidentEdges[nodeIdx];
if (!incident) return { nodeSet, edgeSet };
nodeSet.add(nodeIdx);
for (const i of incident) {
const edge = edges[i];
edgeSet.add(i);
nodeSet.add(edge.sourceIdx);
nodeSet.add(edge.targetIdx);
}
return { nodeSet, edgeSet };
}
+340
View File
@@ -0,0 +1,340 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { comparableBody, compareHistory, splitSnapshot } from './history-comparison.ts';
function rowsOf(comparison) {
return comparison.chunks.flatMap((chunk) => chunk.rows);
}
async function compare(current, snapshot) {
const comparison = await compareHistory(current, snapshot);
const rows = rowsOf(comparison);
const normalize = (body) => body.replace(/\r\n/g, '\n');
const restoreText = (excludedKind) => rows
.filter((row) => row.kind !== excludedKind)
.map((row) => row.text + (row.newline ? '\n' : ''))
.join('');
assert.equal(restoreText('added'), normalize(current), 'all current content is retained');
assert.equal(restoreText('removed'), normalize(snapshot), 'all snapshot content is retained');
assert.equal(comparison.added, rows.filter((row) => row.kind === 'added').length);
assert.equal(comparison.removed, rows.filter((row) => row.kind === 'removed').length);
let currentLine = 1;
let snapshotLine = 1;
for (const row of rows) {
assert.equal(row.currentLine, row.kind === 'added' ? null : currentLine++);
assert.equal(row.snapshotLine, row.kind === 'removed' ? null : snapshotLine++);
assert.equal(typeof row.newline, 'boolean');
assert.equal(row.text.includes('\n'), false);
}
for (let index = 0; index < comparison.chunks.length; index++) {
const chunk = comparison.chunks[index];
assert.ok(chunk.rows.length > 0, 'empty chunks are not displayed');
assert.notEqual(chunk.kind, comparison.chunks[index - 1]?.kind, 'adjacent chunks are merged');
if (chunk.kind === 'unchanged') {
assert.ok(chunk.rows.every((row) => row.kind === 'same'));
}
}
return comparison;
}
function lines(count) {
return Array.from({ length: count }, (_, index) => `line ${index + 1}`);
}
function document(lines) {
return lines.join('\n') + '\n';
}
test('restoring a snapshot adds snapshot-only lines and removes current-only lines', async () => {
const comparison = await compare('keep\nlive\n', 'keep\narchived\nextra\n');
assert.equal(comparison.added, 2);
assert.equal(comparison.removed, 1);
assert.deepEqual(rowsOf(comparison).map((row) => [row.kind, row.text]), [
['same', 'keep'],
['removed', 'live'],
['added', 'archived'],
['added', 'extra'],
]);
});
test('insertion keeps both line-number columns aligned with their own documents', async () => {
const comparison = await compare('first\nlast', 'first\ninserted\nlast');
assert.deepEqual(rowsOf(comparison), [
{ kind: 'same', text: 'first', currentLine: 1, snapshotLine: 1, newline: true },
{ kind: 'added', text: 'inserted', currentLine: null, snapshotLine: 2, newline: true },
{ kind: 'same', text: 'last', currentLine: 2, snapshotLine: 3, newline: false },
]);
});
test('deletion leaves snapshot line numbers unchanged by removed rows', async () => {
const comparison = await compare('first\ndeleted\nlast', 'first\nlast');
assert.deepEqual(rowsOf(comparison), [
{ kind: 'same', text: 'first', currentLine: 1, snapshotLine: 1, newline: true },
{ kind: 'removed', text: 'deleted', currentLine: 2, snapshotLine: null, newline: true },
{ kind: 'same', text: 'last', currentLine: 3, snapshotLine: 2, newline: false },
]);
});
test('replacement presents the current line before its snapshot replacement', async () => {
const comparison = await compare('first\nlive\nlast\n', 'first\narchived\nlast\n');
assert.deepEqual(rowsOf(comparison), [
{ kind: 'same', text: 'first', currentLine: 1, snapshotLine: 1, newline: true },
{ kind: 'removed', text: 'live', currentLine: 2, snapshotLine: null, newline: true },
{ kind: 'added', text: 'archived', currentLine: null, snapshotLine: 2, newline: true },
{ kind: 'same', text: 'last', currentLine: 3, snapshotLine: 3, newline: true },
]);
});
test('empty bodies and blank lines remain distinct without phantom trailing rows', async () => {
const cases = [
['', '', 0, 0],
['', '\n', 1, 0],
['\n', '', 0, 1],
['', '\n\n', 2, 0],
['\n', '\n', 0, 0],
['\n\n', '\n', 0, 1],
['', 'text', 1, 0],
['text', '', 0, 1],
];
for (const [current, snapshot, added, removed] of cases) {
const comparison = await compare(current, snapshot);
assert.equal(comparison.added, added);
assert.equal(comparison.removed, removed);
}
assert.deepEqual((await compare('', '')).chunks, []);
assert.deepEqual(rowsOf(await compare('', '\n')), [
{ kind: 'added', text: '', currentLine: null, snapshotLine: 1, newline: true },
]);
});
test('adding a final newline is visible even when the line text is identical', async () => {
const comparison = await compare('last', 'last\n');
assert.equal(comparison.added, 1);
assert.equal(comparison.removed, 1);
assert.deepEqual(rowsOf(comparison), [
{ kind: 'removed', text: 'last', currentLine: 1, snapshotLine: null, newline: false },
{ kind: 'added', text: 'last', currentLine: null, snapshotLine: 1, newline: true },
]);
});
test('removing a final newline retains the visible missing-newline flag', async () => {
const comparison = await compare('last\n', 'last');
assert.deepEqual(rowsOf(comparison).map((row) => [row.kind, row.text, row.newline]), [
['removed', 'last', true],
['added', 'last', false],
]);
});
test('CRLF-only differences compare equal, including blank lines and the final newline', async () => {
const comparison = await compare('first\r\n\r\nlast\r\n', 'first\n\nlast\n');
assert.equal(comparison.added, 0);
assert.equal(comparison.removed, 0);
assert.deepEqual(rowsOf(comparison).map((row) => [row.text, row.newline]), [
['first', true], ['', true], ['last', true],
]);
const withoutFinalNewline = await compare('last\r\n', 'last');
assert.equal(withoutFinalNewline.added, 1);
assert.equal(withoutFinalNewline.removed, 1);
});
test('whitespace and standalone carriage returns are not ignored', async () => {
const whitespace = await compare('same \n\t\nlast', 'same\n \nlast');
assert.equal(whitespace.added, 2);
assert.equal(whitespace.removed, 2);
const carriageReturn = await compare('first\rlast', 'firstlast');
assert.equal(carriageReturn.added, 1);
assert.equal(carriageReturn.removed, 1);
assert.equal(rowsOf(carriageReturn)[0].text, 'first\rlast');
});
test('snapshot splitting retains raw BOM/CRLF metadata and untouched body bytes', () => {
const metadata = '\uFEFF---\r\ntitle: Snapshot\r\ndescription: "inline --- stays here"\r\n---\r\n';
const body = '# Body\r\n\r\n---\r\ntrailing spaces ';
assert.deepEqual(splitSnapshot(metadata + body), { metadata, body });
});
test('frontmatter closes only at a complete delimiter line, not inline YAML values', () => {
const metadata = '---\ntitle: "before --- after"\nother: ---\n---\n';
const body = 'body\n---\nremaining body\n';
assert.deepEqual(splitSnapshot(metadata + body), { metadata, body });
assert.deepEqual(splitSnapshot('---\n---'), { metadata: '---\n---', body: '' });
assert.deepEqual(splitSnapshot('---\ntitle: archived\n---'), {
metadata: '---\ntitle: archived\n---', body: '',
});
});
test('incomplete, nonleading and non-line delimiters remain entirely in the body', () => {
const bodies = [
'',
'---',
'---\ntitle: "inline --- is not a closing line"\nbody',
'---\ntitle: archived\n--- suffix\nbody',
'---\ntitle: archived\r---\nbody',
'---\ntitle: archived\n---\r',
'intro\n---\ntitle: archived\n---\nbody',
' ---\ntitle: archived\n---\nbody',
'\uFEFFbody\r\n',
];
for (const body of bodies) {
assert.deepEqual(splitSnapshot(body), { body, metadata: '' });
}
});
test('metadata changes do not become body changes when consumers compare split bodies', async () => {
const current = splitSnapshot('---\ntitle: Live\ntags: [live]\n---\nsame body\n');
const snapshot = splitSnapshot('---\ntitle: Archived\ntags: [old]\n---\nsame body\n');
const comparison = await compare(current.body, snapshot.body);
assert.notEqual(current.metadata, snapshot.metadata);
assert.equal(comparison.added, 0);
assert.equal(comparison.removed, 0);
});
test('long edge runs retain all hidden rows with exactly three visible context lines', async () => {
const current = lines(25);
const snapshot = [...current];
snapshot[12] = 'archived line 13';
const comparison = await compare(document(current), document(snapshot));
assert.deepEqual(comparison.chunks.map((chunk) => [chunk.kind, chunk.rows.length]), [
['unchanged', 9], ['lines', 8], ['unchanged', 9],
]);
assert.deepEqual(comparison.chunks[1].rows.map((row) => row.text), [
'line 10', 'line 11', 'line 12', 'line 13', 'archived line 13',
'line 14', 'line 15', 'line 16',
]);
assert.deepEqual(comparison.chunks[0].rows.map((row) => row.currentLine), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
assert.deepEqual(comparison.chunks[2].rows.map((row) => row.snapshotLine), [17, 18, 19, 20, 21, 22, 23, 24, 25]);
});
test('long middle runs preserve three context rows on each side for expansion', async () => {
const current = lines(16);
const snapshot = [...current];
snapshot[0] = 'archived first';
snapshot[15] = 'archived last';
const comparison = await compare(document(current), document(snapshot));
assert.deepEqual(comparison.chunks.map((chunk) => [chunk.kind, chunk.rows.length]), [
['lines', 5], ['unchanged', 8], ['lines', 5],
]);
assert.deepEqual(comparison.chunks[0].rows.filter((row) => row.kind === 'same').map((row) => row.text), [
'line 2', 'line 3', 'line 4',
]);
assert.deepEqual(comparison.chunks[1].rows.map((row) => row.text), current.slice(4, 12));
assert.deepEqual(comparison.chunks[2].rows.filter((row) => row.kind === 'same').map((row) => row.text), [
'line 13', 'line 14', 'line 15',
]);
});
test('overlapping context is not duplicated and the seventh middle line can collapse', async () => {
for (const unchangedCount of [6, 7]) {
const current = ['live first', ...lines(unchangedCount), 'live last'];
const snapshot = ['archived first', ...lines(unchangedCount), 'archived last'];
const comparison = await compare(document(current), document(snapshot));
const hidden = comparison.chunks.filter((chunk) => chunk.kind === 'unchanged');
assert.equal(hidden.length, unchangedCount === 6 ? 0 : 1);
if (hidden.length) assert.deepEqual(hidden[0].rows.map((row) => row.text), ['line 4']);
assert.equal(rowsOf(comparison).filter((row) => row.kind === 'same').length, unchangedCount);
}
});
test('identical notes have zero additions/removals and retain long unchanged content', async () => {
const short = await compare('same\nbody', 'same\nbody');
assert.equal(short.added, 0);
assert.equal(short.removed, 0);
assert.deepEqual(short.chunks.map((chunk) => chunk.kind), ['lines']);
const body = document(lines(3000));
const long = await compare(body, body);
assert.equal(long.added, 0);
assert.equal(long.removed, 0);
assert.deepEqual(long.chunks.map((chunk) => [chunk.kind, chunk.rows.length]), [['unchanged', 3000]]);
});
test('large notes preserve hidden content as well as an appended snapshot line', async () => {
const current = document(lines(3000));
const comparison = await compare(current, current + 'appended\n');
assert.equal(comparison.added, 1);
assert.equal(comparison.removed, 0);
assert.deepEqual(comparison.chunks.map((chunk) => [chunk.kind, chunk.rows.length]), [
['unchanged', 2997], ['lines', 4],
]);
});
test('comparable bodies ignore only leading and trailing blank lines', () => {
assert.equal(comparableBody(''), '');
assert.equal(comparableBody('\n\n'), '');
assert.equal(comparableBody('body'), 'body\n');
assert.equal(comparableBody('\n\n# T\nbody\n\n'), '# T\nbody\n');
assert.equal(comparableBody('\r\n# T\r\nbody\r\n'), '# T\r\nbody\n');
assert.equal(comparableBody('a\n\n\nb'), 'a\n\n\nb\n');
assert.equal(comparableBody(comparableBody('x\n')), 'x\n');
});
test('a stripped current body matches a snapshot that keeps its file whitespace', async () => {
// The app hands the review the note body without edge newlines; the stored snapshot keeps them.
const current = '# T\nbody';
for (const raw of [
'---\ntitle: T\n---\n# T\nbody\n',
'---\ntitle: T\n---\n\n# T\nbody\n',
'---\r\ntitle: T\r\n---\r\n\r\n# T\r\nbody\r\n',
]) {
const comparison = await compareHistory(comparableBody(current), comparableBody(splitSnapshot(raw).body));
assert.equal(comparison.added, 0, JSON.stringify(raw));
assert.equal(comparison.removed, 0, JSON.stringify(raw));
}
const changed = await compareHistory(
comparableBody(current),
comparableBody(splitSnapshot('---\ntitle: T\n---\n# T\nedited\n').body),
);
assert.deepEqual([changed.added, changed.removed], [1, 1]);
assert.deepEqual(
rowsOf(changed).filter((row) => row.kind !== 'same').map((row) => [row.kind, row.text]),
[['removed', 'body'], ['added', 'edited']],
);
});
const numbered = (count, prefix) => Array.from({ length: count }, (_, index) => `${prefix} ${index}`).join('\n') + '\n';
test('a heavily rewritten note is compared quickly and still reports every change', async () => {
const started = performance.now();
const comparison = await compare(numbered(1000, 'old line'), numbered(1000, 'new text'));
assert.ok(performance.now() - started < 1500, 'the comparison must not wait on timers per edit');
assert.equal(comparison.added, 1000);
assert.equal(comparison.removed, 1000);
});
test('beyond the diff limit, shared start and end lines stay unchanged and the middle is replaced', async () => {
const head = numbered(100, 'same start');
const tail = numbered(100, 'same end');
const current = head + numbered(2500, 'old middle') + tail;
const snapshot = head + numbered(1800, 'new middle') + tail;
const started = performance.now();
const comparison = await compare(current, snapshot);
assert.ok(performance.now() - started < 1500);
assert.equal(comparison.removed, 2500);
assert.equal(comparison.added, 1800);
assert.equal(rowsOf(comparison).filter((row) => row.kind === 'same').length, 200);
assert.equal(rowsOf(comparison)[0].text, 'same start 0');
assert.equal(rowsOf(comparison).at(-1).text, 'same end 99');
});
test('the coarse fallback handles CRLF, missing final newlines and empty sides', async () => {
const big = numbered(2000, 'a').replace(/\n/g, '\r\n');
const other = numbered(2000, 'b');
const crlf = await compare(big, other);
assert.equal(crlf.added + crlf.removed, 4000);
const noFinalNewline = await compare(numbered(1500, 'a').slice(0, -1), numbered(1500, 'b').slice(0, -1));
assert.equal(noFinalNewline.added, 1500);
assert.equal(rowsOf(noFinalNewline).at(-1).newline, false);
const empty = await compare('', numbered(2000, 'b'));
assert.deepEqual([empty.added, empty.removed], [2000, 0]);
const emptied = await compare(numbered(2000, 'b'), '');
assert.deepEqual([emptied.added, emptied.removed], [0, 2000]);
});
test('differences under the limit still use the minimal line diff', async () => {
const current = numbered(5000, 'line');
const snapshot = current.replace('line 10\n', 'edited 10\n').replace('line 4000\n', 'edited 4000\n');
const comparison = await compare(current, snapshot);
assert.deepEqual([comparison.added, comparison.removed], [2, 2]);
});
+127
View File
@@ -0,0 +1,127 @@
import { diffLines, type Change } from 'diff';
export interface HistoryRow {
kind: 'same' | 'added' | 'removed';
text: string;
currentLine: number | null;
snapshotLine: number | null;
/** Whether this line has a terminating LF; CRLF is normalized for comparison. */
newline: boolean;
}
export interface HistoryChunk {
kind: 'lines' | 'unchanged';
rows: HistoryRow[];
}
export interface HistoryComparison {
added: number;
removed: number;
chunks: HistoryChunk[];
}
/** Separate only a complete leading frontmatter block, keeping both slices verbatim. */
export function splitSnapshot(raw: string): { body: string; metadata: string } {
const opening = /^(?:\uFEFF)?---\r?\n/.exec(raw);
if (!opening) return { body: raw, metadata: '' };
const closingDelimiter = /\n---(?:\r?\n|$)/g;
closingDelimiter.lastIndex = opening[0].length - 1;
const closing = closingDelimiter.exec(raw);
if (!closing) return { body: raw, metadata: '' };
const bodyStart = closing.index + closing[0].length;
return { body: raw.slice(bodyStart), metadata: raw.slice(0, bodyStart) };
}
/**
* Make two note bodies comparable. The note body the app loads has its leading and trailing
* blank lines removed, while a stored snapshot keeps the file's exact text, so differences in
* those lines are not changes the editor can show or restore.
*/
export function comparableBody(body: string): string {
const trimmed = body.replace(/^(?:\r?\n)+/, '').replace(/(?:\r?\n)+$/, '');
return trimmed === '' ? '' : trimmed + '\n';
}
/** Diff cost grows with the square of the edit distance; beyond this, a coarse diff is shown instead. */
const MAX_EDIT_LENGTH = 1000;
/** Always-correct fallback: shared leading and trailing lines stay unchanged, the middle is replaced. */
function coarseChanges(current: string, snapshot: string): Change[] {
const lines = (text: string) => text.replace(/\r\n/g, '\n').match(/[^\n]*\n|[^\n]+$/g) ?? [];
const before = lines(current);
const after = lines(snapshot);
let start = 0;
while (start < before.length && start < after.length && before[start] === after[start]) start++;
let end = 0;
while (end < before.length - start && end < after.length - start
&& before[before.length - 1 - end] === after[after.length - 1 - end]) end++;
const part = (items: string[], kind: 'same' | 'added' | 'removed'): Change[] => items.length
? [{ value: items.join(''), count: items.length, added: kind === 'added', removed: kind === 'removed' }]
: [];
return [
...part(before.slice(0, start), 'same'),
...part(before.slice(start, before.length - end), 'removed'),
...part(after.slice(start, after.length - end), 'added'),
...part(before.slice(before.length - end), 'same'),
];
}
/** Added/removed rows describe restoring the snapshot over the current editor body. */
export async function compareHistory(
currentBody: string,
snapshotBody: string,
): Promise<HistoryComparison> {
// jsdiff's callback mode waits on a timer for every step, which takes seconds on large changes.
const changes = diffLines(currentBody, snapshotBody, { stripTrailingCr: true, maxEditLength: MAX_EDIT_LENGTH })
?? coarseChanges(currentBody, snapshotBody);
const chunks: HistoryChunk[] = [];
let added = 0;
let removed = 0;
let currentLine = 1;
let snapshotLine = 1;
const contextLines = 3;
for (let changeIndex = 0; changeIndex < changes.length; changeIndex++) {
const change = changes[changeIndex];
const kind: HistoryRow['kind'] = change.added ? 'added' : change.removed ? 'removed' : 'same';
const leadingContext = changeIndex > 0 ? contextLines : 0;
const trailingContext = changeIndex < changes.length - 1 ? contextLines : 0;
const collapse = kind === 'same' && change.count > (
changes.length === 1 ? contextLines * 2 : leadingContext + trailingContext
);
let offset = 0;
let rowIndex = 0;
while (offset < change.value.length) {
const newlineIndex = change.value.indexOf('\n', offset);
const newline = newlineIndex !== -1;
const end = newline ? newlineIndex : change.value.length;
const chunkKind: HistoryChunk['kind'] = collapse
&& rowIndex >= leadingContext
&& rowIndex < change.count - trailingContext
? 'unchanged'
: 'lines';
let chunk = chunks[chunks.length - 1];
if (!chunk || chunk.kind !== chunkKind) {
chunk = { kind: chunkKind, rows: [] };
chunks.push(chunk);
}
chunk.rows.push({
kind,
text: change.value.slice(offset, end),
currentLine: kind === 'added' ? null : currentLine++,
snapshotLine: kind === 'removed' ? null : snapshotLine++,
newline,
});
if (kind === 'added') added++;
if (kind === 'removed') removed++;
offset = newline ? end + 1 : end;
rowIndex++;
}
}
return { added, removed, chunks };
}
+48
View File
@@ -0,0 +1,48 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { isNetworkPath, networkPathWithin, resolveFileReference, resolveVaultFilePath } from './paths.ts';
test('network paths are recognized by their text', () => {
for (const path of ['\\\\host\\share\\r.pdf', '//host/share/r.pdf', '\\\\?\\UNC\\host\\share', '//?/unc/host/share']) {
assert.equal(isNetworkPath(path), true, path);
}
for (const path of ['C:\\Users\\me\\note.md', 'C:/Users/me/note.md', '\\\\?\\C:\\Users\\me', '/home/me/note.md', 'relative/note.md', '']) {
assert.equal(isNetworkPath(path), false, path);
}
});
test('a network path counts as inside a folder only by its own text', () => {
const roots = ['\\\\nas\\notes\\Vault', 'C:/Users/me/Vault', null];
assert.equal(networkPathWithin('//NAS/notes/vault/sub/a.png', roots), true);
assert.equal(networkPathWithin('\\\\?\\UNC\\nas\\notes\\Vault\\a.png', roots), true);
assert.equal(networkPathWithin('//nas/notes/Vault2/a.png', roots), false);
assert.equal(networkPathWithin('//nas/notes/Vault/../../other/a.png', roots), false);
assert.equal(networkPathWithin('//evil/share/a.png', roots), false);
});
test('on Windows a reference to a network path outside the vault resolves to no file', () => {
const vault = 'C:/Users/me/Vault';
const note = 'C:/Users/me/Vault/Folder/note.md';
for (const target of ['//evil/share/r.pdf', '\\\\evil\\share\\r.pdf', '//evil/share/image.png', '%2F%2Fevil/share/x.png']) {
assert.equal(resolveFileReference(target, note, vault, true).path, '', target);
}
// Ordinary references are untouched.
assert.equal(resolveVaultFilePath('image.png', note, vault, true), 'C:/Users/me/Vault/Folder/image.png');
assert.equal(resolveVaultFilePath('.helixnotes/attachments/a.png', note, vault, true), 'C:/Users/me/Vault/.helixnotes/attachments/a.png');
assert.equal(resolveVaultFilePath('C:/Users/me/Pictures/a.png', note, vault, true), 'C:/Users/me/Pictures/a.png');
});
test('a vault or an opened note on a network share keeps working on Windows', () => {
const vault = '\\\\nas\\notes\\Vault';
const note = '\\\\nas\\notes\\Vault\\Folder\\note.md';
assert.equal(resolveVaultFilePath('image.png', note, vault, true), '//nas/notes/Vault/Folder/image.png');
assert.equal(resolveVaultFilePath('//nas/notes/Vault/other.png', note, vault, true), '//nas/notes/Vault/other.png');
assert.equal(resolveVaultFilePath('//nas/notes/Elsewhere/x.png', note, vault, true), '');
// A note opened from outside any vault: its own folder is allowed.
assert.equal(resolveVaultFilePath('pic.png', '\\\\files\\home\\me\\note.md', null, true), '//files/home/me/pic.png');
assert.equal(resolveVaultFilePath('//other/share/pic.png', '\\\\files\\home\\me\\note.md', null, true), '');
});
test('elsewhere a path starting with two slashes stays an ordinary local path', () => {
assert.equal(resolveVaultFilePath('//home/me/pic.png', '/home/me/Vault/note.md', '/home/me/Vault', false), '//home/me/pic.png');
});
+70 -6
View File
@@ -46,17 +46,80 @@ export function assetUrlToLocalPath(source: string): string | null {
}
}
/** The owning configured vault, never the active vault of an unrelated external note. */
export function noteAssetRoot(notePath: string | null, activeVault: string | null, vaults: readonly string[] = []): string | null {
if (!notePath || isPathInDirectory(notePath, activeVault)) return activeVault;
return [...vaults].filter(root => isPathInDirectory(notePath, root)).sort((a, b) => b.length - a.length)[0] ?? null;
}
/**
* Whether Windows would resolve this path over the network: `\\host\share`, `//host/share` or the
* verbatim `\\?\UNC\host`. Merely loading or examining such a path makes Windows contact the host
* and offer it the user's login, so a path from note content is judged by its text first.
*/
export function isNetworkPath(path: string): boolean {
const normalized = path.replace(/\\/g, '/');
const verbatim = /^\/\/[?.]\/(.*)$/.exec(normalized);
return verbatim ? /^unc\//i.test(verbatim[1]) : normalized.startsWith('//');
}
function networkPathKey(path: string): string {
return path.replace(/\\/g, '/').replace(/^\/\/[?.]\/unc\//i, '//').replace(/\/+$/, '').toLowerCase();
}
/** Whether a network path lies, by its text alone, inside one of the given folders. */
export function networkPathWithin(path: string, roots: readonly (string | null | undefined)[]): boolean {
const key = networkPathKey(path);
if (key.split('/').includes('..')) return false;
return roots.some((root) => {
if (!root || !isNetworkPath(root)) return false;
const rootKey = networkPathKey(root);
return key === rootKey || key.startsWith(rootKey + '/');
});
}
const windowsHost = typeof navigator !== 'undefined' && /windows nt/i.test(navigator.userAgent);
/** Decode only the URI path, keeping query/fragment data out of filesystem paths. */
export function resolveFileReference(source: string, notePath: string | null, vaultRoot: string | null, windows = windowsHost): { path: string; suffix: string } {
const boundary = source.search(/[?#]/);
const encoded = boundary < 0 ? source : source.slice(0, boundary);
let path: string;
try { path = decodeURIComponent(encoded); } catch { path = encoded; }
return { path: resolveVaultFilePath(path, notePath, vaultRoot, windows), suffix: boundary < 0 ? '' : source.slice(boundary) };
}
/** Percent-encode each path segment for a Markdown destination; a Windows drive prefix stays literal. */
export function encodeMarkdownPath(path: string): string {
const normalized = path.replace(/\\/g, '/');
const drive = /^[A-Za-z]:(?=\/|$)/.exec(normalized)?.[0] ?? '';
return drive + normalized.slice(drive.length).split('/').map(segment => encodeURIComponent(segment)
.replace(/[!'()*]/g, character => `%${character.charCodeAt(0).toString(16).toUpperCase()}`)).join('/');
}
/**
* The local file a reference in a note points at. On Windows a network path outside the vault and
* the note's own folder resolves to '' (no file), so nothing downstream touches it.
*/
export function resolveVaultFilePath(
targetPath: string,
notePath: string | null,
vaultRoot: string | null,
windows = windowsHost,
): string {
const target = normalizeLocalAssetPath(targetPath);
if (windows && isNetworkPath(target)) {
const noteFolder = notePath ? notePath.replace(/\\/g, '/').replace(/\/[^/]*$/, '') : null;
if (!networkPathWithin(target, [vaultRoot, noteFolder])) return '';
}
if (target.startsWith('/') || /^[A-Za-z]:\//.test(target)) return target;
const root = vaultRoot?.replace(/\\/g, '/').replace(/\/$/, '') ?? '';
if (target.startsWith('.helixnotes/') && root) {
return resolvePathFromFile(`${root}/.vault-root`, target);
if (root) {
// Older imported managed links may still contain notebook-depth prefixes.
const managed = target.match(/^(?:\.{1,2}\/)*(\.helixnotes\/attachments\/.*)$/);
if (managed) return resolvePathFromFile(`${root}/.vault-root`, managed[1]);
if (target.startsWith('.helixnotes/')) return resolvePathFromFile(`${root}/.vault-root`, target);
}
if (notePath) return resolvePathFromFile(notePath, target);
if (root) return resolvePathFromFile(`${root}/.vault-root`, target);
@@ -79,14 +142,15 @@ export function assetSourceToMarkdown(
const absolutePath = assetUrlToLocalPath(source);
if (absolutePath === null) return source;
const url = new URL(source);
const suffix = url.search + url.hash;
const normalizedRoot = vaultRoot?.replace(/\\/g, '/').replace(/\/$/, '');
if (!normalizedRoot || !absolutePath.startsWith(normalizedRoot + '/')) return absolutePath;
if (!normalizedRoot || !isPathInDirectory(absolutePath, normalizedRoot)) return encodeMarkdownPath(absolutePath) + suffix;
const vaultRelative = absolutePath.substring(normalizedRoot.length + 1);
if (vaultRelative.startsWith('.helixnotes/')) return vaultRelative;
if (!notePath) return vaultRelative;
if (vaultRelative.startsWith('.helixnotes/') || !notePath) return encodeMarkdownPath(vaultRelative) + suffix;
const normalizedNotePath = notePath.replace(/\\/g, '/');
const noteDirectory = normalizedNotePath.substring(0, normalizedNotePath.lastIndexOf('/'));
return relativePath(noteDirectory, absolutePath);
return encodeMarkdownPath(relativePath(noteDirectory, absolutePath)) + suffix;
}
export function resolvePathFromFile(filePath: string, targetPath: string): string {
+32
View File
@@ -0,0 +1,32 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { matchesTaskDueFilter } from './task-filter.ts';
import { formatLocalDate } from './time.ts';
const today = '2027-01-01';
const dates = [null, '2026-12-31', today, '2027-01-02'];
test('Today includes only tasks due on the current local day', () => {
assert.deepEqual(dates.filter(due => matchesTaskDueFilter(due, 'today', today)), [today]);
});
test('Overdue excludes today, future dates, and undated tasks across a year boundary', () => {
assert.deepEqual(dates.filter(due => matchesTaskDueFilter(due, 'overdue', today)), ['2026-12-31']);
});
test('Overdue never includes a completed task, while Today and all still do', () => {
assert.equal(matchesTaskDueFilter('2026-12-31', 'overdue', today, false), true);
assert.equal(matchesTaskDueFilter('2026-12-31', 'overdue', today, true), false);
assert.equal(matchesTaskDueFilter(today, 'today', today, true), true);
assert.equal(matchesTaskDueFilter('2026-12-31', 'all', today, true), true);
});
test('clearing the date filter restores dated and undated tasks', () => {
assert.equal(matchesTaskDueFilter(null, 'all', today), true);
assert.equal(matchesTaskDueFilter('2027-01-02', 'all', today), true);
});
test('task date keys use the local calendar rather than a locale-specific display string', () => {
assert.equal(formatLocalDate(new Date(2026, 11, 31, 23, 59)), '2026-12-31');
assert.equal(formatLocalDate(new Date(2027, 0, 1, 0, 0)), '2027-01-01');
});
+8
View File
@@ -0,0 +1,8 @@
export type TaskDueFilter = 'all' | 'today' | 'overdue';
/** Overdue means late and still open: a completed task is never overdue. */
export function matchesTaskDueFilter(due: string | null, filter: TaskDueFilter, today: string, completed = false): boolean {
if (filter === 'all') return true;
if (!due) return false;
return filter === 'today' ? due === today : !completed && due < today;
}
+20
View File
@@ -0,0 +1,20 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { formatClockTime } from './time.ts';
const at = (hour, minute) => new Date(2026, 1, 15, hour, minute);
test('12-hour insertion distinguishes midnight, noon, and evening', () => {
assert.equal(formatClockTime(at(0, 7), '12h'), '12:07 AM');
assert.equal(formatClockTime(at(12, 0), '12h'), '12:00 PM');
assert.equal(formatClockTime(at(23, 5), '12h'), '11:05 PM');
});
test('24-hour insertion retains zero-padded clock values', () => {
assert.equal(formatClockTime(at(0, 7), '24h'), '00:07');
assert.equal(formatClockTime(at(23, 5), '24h'), '23:05');
});
test('relative display mode inserts an absolute time rather than an aging label', () => {
assert.equal(formatClockTime(at(17, 28), 'relative'), '17:28');
});
+16
View File
@@ -9,6 +9,22 @@ const fullDateFormatter = new Intl.DateTimeFormat(undefined, {
const monthFormatter = new Intl.DateTimeFormat(undefined, { month: 'long' });
const monthYearFormatter = new Intl.DateTimeFormat(undefined, { month: 'long', year: 'numeric' });
export function formatLocalDate(date: Date): string {
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${date.getFullYear()}-${month}-${day}`;
}
export function formatClockTime(date: Date, timeFormat?: string): string {
const hours = date.getHours();
const minutes = String(date.getMinutes()).padStart(2, '0');
if (timeFormat === '12h') {
return `${hours % 12 || 12}:${minutes} ${hours < 12 ? 'AM' : 'PM'}`;
}
// Relative labels are for display, not text permanently inserted into a note.
return `${String(hours).padStart(2, '0')}:${minutes}`;
}
export function formatRelativeTime(dateStr: string): string {
const date = new Date(dateStr);
if (Number.isNaN(date.getTime())) return date.toString();
+10 -6
View File
@@ -7,7 +7,7 @@
import { darkThemes, isMobile, isAndroid } from '$lib/platform';
import type { CustomTheme } from '$lib/types';
import ResizeHandles from '$lib/components/ResizeHandles.svelte';
import { resolveVaultFilePath } from '$lib/utils/paths';
import { noteAssetRoot, resolveFileReference } from '$lib/utils/paths';
let { children } = $props();
@@ -90,11 +90,15 @@
openUrl(href).catch((err) => console.error('Failed to open URL:', err));
} else if (!href.startsWith('#')) {
const config = get(appConfig);
const absPath = resolveVaultFilePath(
decodeURIComponent(href),
get(activeNotePath),
config?.active_vault ?? null,
);
const note = get(activeNotePath);
const root = noteAssetRoot(note, config?.active_vault ?? null, config?.vaults.map(vault => vault.path) ?? []);
const absPath = resolveFileReference(href, note, root).path;
if (!absPath) {
// A network path outside the vault is not opened as a file (Windows). Written as
// `//host/page` it is a web address without its scheme.
if (/^\/\/[^/\\?.]/.test(href)) openUrl('https:' + href).catch((err) => console.error('Failed to open URL:', err));
return;
}
// Internal .md note link - navigate within the app
if (absPath.endsWith('.md')) {
window.dispatchEvent(new CustomEvent('helixnotes-open-note', { detail: absPath }));
+4 -1
View File
@@ -119,7 +119,8 @@
} else if (event.key === '0') {
event.preventDefault();
event.stopPropagation();
setEditorFontSize(defaultEditorFontSize);
// Back to the size chosen in Appearance, not the built-in default.
setEditorFontSize($appConfig?.preferred_font_size ?? defaultEditorFontSize);
}
};
@@ -215,6 +216,8 @@
opensans: '"Open Sans", -apple-system, sans-serif',
literata: '"Literata", Georgia, serif',
mono: '"JetBrains Mono", "Fira Code", "Cascadia Code", monospace',
ubuntu: '"Ubuntu", -apple-system, sans-serif',
'ubuntu-mono': '"Ubuntu Mono", monospace',
};
const stack = fontStacks[config.font_family];
if (stack) document.documentElement.style.setProperty('--editor-font-family', stack);
+96
View File
@@ -0,0 +1,96 @@
-------------------------------
UBUNTU FONT LICENCE Version 1.0
-------------------------------
PREAMBLE
This licence allows the licensed fonts to be used, studied, modified and
redistributed freely. The fonts, including any derivative works, can be
bundled, embedded, and redistributed provided the terms of this licence
are met. The fonts and derivatives, however, cannot be released under
any other licence. The requirement for fonts to remain under this
licence does not require any document created using the fonts or their
derivatives to be published under this licence, as long as the primary
purpose of the document is not to be a vehicle for the distribution of
the fonts.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this licence and clearly marked as such. This may
include source files, build scripts and documentation.
"Original Version" refers to the collection of Font Software components
as received under this licence.
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to
a new environment.
"Copyright Holder(s)" refers to all individuals and companies who have a
copyright ownership of the Font Software.
"Substantially Changed" refers to Modified Versions which can be easily
identified as dissimilar to the Font Software by users of the Font
Software comparing the Original Version with the Modified Version.
To "Propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification and with or without charging
a redistribution fee), making available to the public, and in some
countries other activities as well.
PERMISSION & CONDITIONS
This licence does not grant any rights under trademark law and all such
rights are reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of the Font Software, to propagate the Font Software, subject to
the below conditions:
1) Each copy of the Font Software must contain the above copyright
notice and this licence. These can be included either as stand-alone
text files, human-readable headers or in the appropriate machine-
readable metadata fields within text or binary files as long as those
fields can be easily viewed by the user.
2) The font name complies with the following:
(a) The Original Version must retain its name, unmodified.
(b) Modified Versions which are Substantially Changed must be renamed to
avoid use of the name of the Original Version or similar names entirely.
(c) Modified Versions which are not Substantially Changed must be
renamed to both (i) retain the name of the Original Version and (ii) add
additional naming elements to distinguish the Modified Version from the
Original Version. The name of such Modified Versions must be the name of
the Original Version, with "derivative X" where X represents the name of
the new work, appended to that name.
3) The name(s) of the Copyright Holder(s) and any contributor to the
Font Software shall not be used to promote, endorse or advertise any
Modified Version, except (i) as required by this licence, (ii) to
acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with
their explicit written permission.
4) The Font Software, modified or unmodified, in part or in whole, must
be distributed entirely under this licence, and must not be distributed
under any other licence. The requirement for fonts to remain under this
licence does not affect any document created using the Font Software,
except any version of the Font Software extracted from a document
created using the Font Software may only be distributed under this
licence.
TERMINATION
This licence becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
Copyright 2010,2011 Canonical Ltd.
This Font Software is licensed under the Ubuntu Font Licence, Version
1.0. https://launchpad.net/ubuntu-font-licence
+96
View File
@@ -0,0 +1,96 @@
-------------------------------
UBUNTU FONT LICENCE Version 1.0
-------------------------------
PREAMBLE
This licence allows the licensed fonts to be used, studied, modified and
redistributed freely. The fonts, including any derivative works, can be
bundled, embedded, and redistributed provided the terms of this licence
are met. The fonts and derivatives, however, cannot be released under
any other licence. The requirement for fonts to remain under this
licence does not require any document created using the fonts or their
derivatives to be published under this licence, as long as the primary
purpose of the document is not to be a vehicle for the distribution of
the fonts.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this licence and clearly marked as such. This may
include source files, build scripts and documentation.
"Original Version" refers to the collection of Font Software components
as received under this licence.
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to
a new environment.
"Copyright Holder(s)" refers to all individuals and companies who have a
copyright ownership of the Font Software.
"Substantially Changed" refers to Modified Versions which can be easily
identified as dissimilar to the Font Software by users of the Font
Software comparing the Original Version with the Modified Version.
To "Propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification and with or without charging
a redistribution fee), making available to the public, and in some
countries other activities as well.
PERMISSION & CONDITIONS
This licence does not grant any rights under trademark law and all such
rights are reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of the Font Software, to propagate the Font Software, subject to
the below conditions:
1) Each copy of the Font Software must contain the above copyright
notice and this licence. These can be included either as stand-alone
text files, human-readable headers or in the appropriate machine-
readable metadata fields within text or binary files as long as those
fields can be easily viewed by the user.
2) The font name complies with the following:
(a) The Original Version must retain its name, unmodified.
(b) Modified Versions which are Substantially Changed must be renamed to
avoid use of the name of the Original Version or similar names entirely.
(c) Modified Versions which are not Substantially Changed must be
renamed to both (i) retain the name of the Original Version and (ii) add
additional naming elements to distinguish the Modified Version from the
Original Version. The name of such Modified Versions must be the name of
the Original Version, with "derivative X" where X represents the name of
the new work, appended to that name.
3) The name(s) of the Copyright Holder(s) and any contributor to the
Font Software shall not be used to promote, endorse or advertise any
Modified Version, except (i) as required by this licence, (ii) to
acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with
their explicit written permission.
4) The Font Software, modified or unmodified, in part or in whole, must
be distributed entirely under this licence, and must not be distributed
under any other licence. The requirement for fonts to remain under this
licence does not affect any document created using the Font Software,
except any version of the Font Software extracted from a document
created using the Font Software may only be distributed under this
licence.
TERMINATION
This licence becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5
View File
@@ -0,0 +1,5 @@
Copyright 2010,2011 Canonical Ltd.
This Font Software is licensed under the Ubuntu Font Licence, Version
1.0. https://launchpad.net/ubuntu-font-licence
+106
View File
@@ -0,0 +1,106 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { createContext, Script } from 'node:vm';
import { parse } from 'svelte/compiler';
import { transformWithEsbuild } from 'vite';
import * as paths from '../src/lib/utils/paths.ts';
import { createEditorMarkdownParser } from '../src/lib/editor/markdownParser.ts';
const source = await readFile(new URL('../src/lib/components/Editor.svelte', import.meta.url), 'utf8');
const declaration = parse(source, { modern: true }).instance.content.body.find(node =>
node.type === 'FunctionDeclaration' && node.id.name === 'resolveImageSrc'
);
const owningRoot = parse(source, { modern: true }).instance.content.body.find(node => node.type === 'FunctionDeclaration' && node.id.name === 'currentNoteAssetRoot');
const { code } = await transformWithEsbuild(`${owningRoot ? source.slice(owningRoot.start, owningRoot.end) : ''}\n${source.slice(declaration.start, declaration.end)}`, 'resolve-image.ts', { loader: 'ts', format: 'esm' });
const script = new Script(`${code}\nresolveImageSrc;`);
const parser = createEditorMarkdownParser();
const imageSource = markdown => parser.parseInline(markdown, {}).flatMap(token => token.children ?? []).find(token => token.type === 'image')?.attrGet('src');
const assetPath = url => decodeURIComponent(new URL(url).pathname.slice(1));
const encodedName = name => encodeURIComponent(name).replace(/[!'()*]/g, char => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
function fixture(note = '/vault/Projects/Note.md', vault = '/vault', known = []) {
const context = createContext({ ...paths,
$activeNotePath: note, $appConfig: { active_vault: vault, vaults: known.map(path => ({ path })) },
normalizePath: path => paths.resolvePathFromFile('', path),
convertFileSrc: path => `http://asset.localhost/${encodeURIComponent(path)}`,
});
return { context, resolve: script.runInContext(context) };
}
for (const name of ['photo%20one.png', 'photo).png', 'photo(one.png', 'photo#one?.png', "café 雪 + 'image'.png"]) {
test(`rich save/reload preserves the actual attachment filename ${JSON.stringify(name)}`, () => {
const f = fixture();
const expected = `/vault/.helixnotes/attachments/${name}`;
let markdown = `![Photo](../.helixnotes/attachments/${encodedName(name)}?v=1#preview)`;
for (let round = 0; round < 3; round++) {
const src = imageSource(markdown);
assert.ok(src, 'round trips must not turn an image into malformed Markdown');
const asset = f.resolve(src);
assert.equal(assetPath(asset), expected, 'query and fragment never become part of the filesystem path');
assert.equal(new URL(asset).search, '?v=1');
assert.equal(new URL(asset).hash, '#preview');
markdown = `![Photo](${paths.assetSourceToMarkdown(asset, f.context.$activeNotePath, '/vault')})`;
}
});
}
test('Windows and UNC asset URLs preserve encoded filenames across repeated saves', () => {
for (const [vault, note, absolute] of [
['C:\\Notes', 'C:\\Notes\\Projects\\Note.md', 'C:/Notes/.helixnotes/attachments/photo%20).png'],
['\\\\server\\share\\Notes', '\\\\server\\share\\Notes\\Projects\\Note.md', '//server/share/Notes/.helixnotes/attachments/photo%20).png']
]) {
const f = fixture(note, vault);
let markdown = '![Photo](.helixnotes/attachments/photo%2520%29.png#preview)';
for (let round = 0; round < 3; round++) {
const asset = f.resolve(imageSource(markdown));
assert.equal(assetPath(asset), absolute);
markdown = `![Photo](${paths.assetSourceToMarkdown(asset, note, vault)})`;
}
}
});
test('an external note uses its configured source vault, not the active destination vault', () => {
const f = fixture('/source/Notebook/Note.md', '/destination', ['/source', '/destination']);
assert.equal(assetPath(f.resolve('.helixnotes/attachments/image.png')), '/source/.helixnotes/attachments/image.png');
assert.equal(assetPath(f.resolve('../.helixnotes/attachments/image.png')), '/source/.helixnotes/attachments/image.png');
});
test('an unconfigured external note resolves its own relative assets without borrowing active-vault metadata', () => {
const f = fixture('/external/Note.md', '/destination');
assert.equal(assetPath(f.resolve('.helixnotes/attachments/image.png')), '/external/.helixnotes/attachments/image.png');
assert.equal(assetPath(f.resolve('images/image.png')), '/external/images/image.png');
});
for (const name of ['Screenshot #1.png', '100% (final).png', 'a%20b #c?d.png', "café 雪 + 'x'.png"]) {
test(`a newly stored attachment ${JSON.stringify(name)} resolves to its own file and saves back unchanged`, () => {
const f = fixture();
// The backend returns the raw stored path, which uploadAsset must encode before using it as a destination.
const stored = `.helixnotes/attachments/0b1c2d3e_${name}`;
const destination = paths.encodeMarkdownPath(stored);
assert.equal(imageSource(`![x](${destination})`), destination, 'the encoded destination parses as one image');
const asset = f.resolve(destination);
assert.equal(assetPath(asset), `/vault/${stored}`);
assert.equal(new URL(asset).search, '');
assert.equal(paths.assetSourceToMarkdown(asset, '/vault/Projects/Note.md', '/vault'), destination);
});
}
test('uploadAsset encodes the stored path before every Markdown or URI use', () => {
const start = source.indexOf('async function uploadAsset');
const body = source.slice(start, source.indexOf('async function insertFile'));
assert.match(body, /const destination = encodeMarkdownPath\(relativePath\)/);
assert.equal(body.replace('encodeMarkdownPath(relativePath)', '').includes('relativePath)'), false);
assert.equal((body.match(/\$\{relativePath\}|src: relativePath|href: relativePath|resolveImageSrc\(relativePath\)/g) ?? []).length, 0);
});
test('Windows drive letters stay literal when a path outside the vault is written to Markdown', () => {
assert.equal(paths.encodeMarkdownPath('C:/Users/me/pic one.png'), 'C:/Users/me/pic%20one.png');
assert.equal(paths.encodeMarkdownPath('C:\\Users\\me\\a#b.png'), 'C:/Users/me/a%23b.png');
assert.equal(paths.encodeMarkdownPath('/home/me/pic one.png'), '/home/me/pic%20one.png');
assert.equal(paths.encodeMarkdownPath('relative/a:b.png'), 'relative/a%3Ab.png');
assert.equal(
paths.assetSourceToMarkdown('http://asset.localhost/C%3A%2FUsers%2Fme%2Fpic%20one.png', '/v/Note.md', '/v'),
'C:/Users/me/pic%20one.png',
);
assert.equal(paths.resolveFileReference('C:/Users/me/pic%20one.png', '/v/Note.md', '/v').path, 'C:/Users/me/pic one.png');
});
+113
View File
@@ -0,0 +1,113 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { externalNoteReferences } from '../src/lib/editor/externalNote.ts';
const ranges = raw => externalNoteReferences(raw).map(reference => ({
...reference, original: Buffer.from(raw).subarray(reference.from, reference.to).toString('utf8')
}));
test('finds Markdown attachment destinations without changing labels, titles, angles, or fragments', () => {
const raw = '![Image|size=small](images/photo%20one.png "Caption")\n[PDF](<files/report (1).pdf#page=2> "Title")\n[CSV][ref]\n\n[ref]: files/list.csv\n';
assert.deepEqual(ranges(raw).map(({ href, original, html }) => ({ href, original, html })), [
{ href: 'images/photo%20one.png', original: 'images/photo%20one.png', html: false },
{ href: 'files/report (1).pdf#page=2', original: 'files/report (1).pdf#page=2', html: false },
{ href: 'files/list.csv', original: 'files/list.csv', html: false }
]);
});
test('legacy space-containing destinations supported by the editor import without copying code examples', () => {
const raw = '![Legacy](images/photo one.png "Caption")\n[File](files/report%20"Title")\n\n~~~md\n![Example](missing image.png)\n~~~\n\n`![Inline](missing image.png)`\n';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/photo one.png', 'files/report']);
});
test('unused and overridden reference definitions are not imported', () => {
assert.deepEqual(ranges('A text-only note.\n\n[unused]: missing.png\n'), []);
const raw = '![Image][photo]\n\n[photo]: images/real.png\n[PHOTO]: missing.png\n[unused]: also-missing.png\n';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/real.png']);
});
test('full, collapsed, and shortcut references select the first matching definition', () => {
for (const use of ['![One][ Photo ]', '![photo][]', '![photo]', '[Photo][]', '[photo]']) {
const raw = `${use}\n\n[PHOTO]: images/real.png\n[ photo ]: missing.png\n`;
assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/real.png'], use);
}
});
test('reference labels in code, math, and image alt text do not authorize unused definitions', () => {
const raw = '`![fake][hidden]`\n\n$[hidden]$\n\n![An [hidden] label](real.png)\n\n[hidden]: missing.png\n';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']);
});
test('UTF-8 byte offsets retain Unicode, BOM, CRLF, and untouched custom frontmatter', () => {
const raw = '\uFEFF---\r\ncustom: "![metadata](do-not-copy.png)"\r\n---\r\n雪 📝 ![图片](资源/café%20雪.png)\r\n';
assert.equal(ranges(raw).length, 1);
assert.equal(ranges(raw)[0].original, '资源/café%20雪.png');
});
test('literal code, comments, closed display math, and HTML code do not become attachments', () => {
const raw = '`![code](inline.png)`\n\n~~~~md\n![code](fenced.png)\n~~~~\n\n$$\n![math](math.png)\n$$\n\n<!-- <img src="comment.png"> -->\n\n<pre><code><img src="literal.png"></code></pre>\n\n![real](real.png)';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']);
});
test('inline math stays literal while escaped dollars and dollar-containing destinations remain links', () => {
const raw = '$![math](math.png)$ \\$![real](real.png)\\$ ![price](images/$price$.png)';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png', 'images/$price$.png']);
});
test('remote URLs, anchors, absolute paths, and linked Markdown notes stay unchanged', () => {
const raw = '![remote](https://example.com/pic.png) [mail](mailto:test@example.com) [anchor](#section)\n![absolute](/private/file.png) [note](Related.MD#heading) [encoded](Related%2EMD)\n![data](data:image/png;base64,AAAA)';
assert.deepEqual(ranges(raw), []);
});
test('HTML images, file links, and PDF embeds retain attribute byte ranges and decode entities', () => {
const raw = '<img alt="a > b src=&quot;missing.png&quot;" src="images/a&amp;b.png"> <a href=files/report.pdf>File</a>\n\n<div data-pdf-src="files/view.pdf#page=2" data-pdf-name="View"></div>';
assert.deepEqual(ranges(raw).map(({ href, original, html }) => ({ href, original, html })), [
{ href: 'images/a&b.png', original: 'images/a&amp;b.png', html: true },
{ href: 'files/report.pdf', original: 'files/report.pdf', html: true },
{ href: 'files/view.pdf#page=2', original: 'files/view.pdf#page=2', html: true }
]);
assert.equal(ranges(raw)[2].pdf, true);
assert.equal(ranges(raw)[0].pdf, false);
});
test('links inside image alt text are not copied as additional attachments', () => {
const raw = '![An [example](missing.txt)](real.png)';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']);
});
test('escaped Markdown paths are decoded without consuming code examples or link titles', () => {
const raw = '[Report](files/report\\(1\\).pdf "Other (unused.png)")';
assert.equal(ranges(raw)[0].href, 'files/report(1).pdf');
assert.equal(ranges(raw)[0].original, 'files/report\\(1\\).pdf');
});
test('a stray backtick in one paragraph does not hide attachments in later paragraphs', () => {
const raw = 'a ` b\n\n![x](my pic.png)\n\nc ` d\n\n![y](plain.png)\n';
assert.deepEqual(ranges(raw).map(reference => [reference.href, reference.original]), [
['my pic.png', 'my pic.png'], ['plain.png', 'plain.png']
]);
// Real code spans still hide what they contain, and only within their own paragraph.
assert.deepEqual(ranges('`![x](code only.png)` then ![y](real one.png)\n\n![z](next one.png)\n').map(reference => reference.href),
['real one.png', 'next one.png']);
});
test('byte offsets stay correct for legacy links after other blocks, blank lines and CRLF', () => {
for (const newline of ['\n', '\r\n']) {
const raw = `# Héllo${newline}${newline}text ${newline}${newline} ${newline}![x](my pic.png)${newline}${newline}![y](other pic.png)${newline}`;
const found = ranges(raw);
assert.deepEqual(found.map(reference => reference.original), ['my pic.png', 'other pic.png'], JSON.stringify(newline));
}
});
test('indented images are imported because the editor never treats indented text as code', () => {
assert.deepEqual(ranges('para\n\n ![x](a.png)\n').map(reference => reference.href), ['a.png']);
assert.deepEqual(ranges('para\n\n\t![x](b.png)\n').map(reference => reference.href), ['b.png']);
assert.deepEqual(ranges('- item\n\n ![x](c.png)\n').map(reference => reference.href), ['c.png']);
// Fenced code stays literal, indented or not.
assert.deepEqual(ranges('```\n![x](a.png)\n```\n\n ```\n ![y](b.png)\n ```\n'), []);
});
test('formulas after a stray backtick no longer hide attachments inside them', () => {
const raw = 'a ` b\n\n$![x](not a file.png)$\n\n![y](real file.png)\n';
assert.deepEqual(ranges(raw).map(reference => reference.href), ['real file.png']);
});
+252
View File
@@ -0,0 +1,252 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { getSchema } from '@tiptap/core';
import StarterKit from '@tiptap/starter-kit';
import Image from '@tiptap/extension-image';
import { DOMSerializer } from '@tiptap/pm/model';
import {
expandImageRange,
parseImgGridHeader,
reorderTarget,
serializeImgGrid,
serializeImgGridHeader,
splitTrailingCloser
} from '../src/lib/editor/imageGrid.ts';
import { ImageGrid } from '../src/lib/editor/extensions/imageGrid.ts';
import { createEditorMarkdownParser } from '../src/lib/editor/markdownParser.ts';
import { joinMarkdownBlocks, serializeInlineMarkdown } from '../src/lib/editor/markdown.ts';
const parser = createEditorMarkdownParser();
const SizedImage = Image.extend({
addAttributes() {
return { ...this.parent?.(), size: { default: 'full' } };
}
});
const schema = getSchema([StarterKit, SizedImage.configure({ inline: true }), ImageGrid]);
/** Grid blocks as the editor would load them: cols/layout plus each image's src, alt and size. */
function parseGrids(markdown) {
const grids = [];
let current = null;
for (const token of parser.parse(markdown, {})) {
if (token.type === 'helix_img_grid_open') {
current = { cols: Number(token.attrGet('data-cols')), layout: token.attrGet('data-layout'), images: [] };
} else if (token.type === 'helix_img_grid_close') {
grids.push(current);
current = null;
} else if (current && token.type === 'inline') {
for (const child of token.children) {
if (child.type !== 'image') continue;
// The editor splits `|size=` off the alt text when it loads images (normalizeImageHtml).
const alt = child.content;
const size = /\|size=(small|medium|full)$/.exec(alt);
current.images.push({ src: child.attrGet('src'), alt: size ? alt.slice(0, size.index) : alt, size: size ? size[1] : 'full', title: child.attrGet('title') });
}
}
}
return grids;
}
/** Save a parsed grid the way Editor.svelte does: inline image Markdown wrapped by serializeImgGrid. */
function saveGrid(grid) {
const node = schema.nodes.imageGrid.create(
{ cols: grid.cols, layout: grid.layout },
grid.images.map((image) => schema.nodes.image.create(image))
);
return serializeImgGrid(node.attrs, serializeInlineMarkdown(node, decodeURI));
}
const roundTrip = (markdown) => parseGrids(markdown).map(saveGrid).join('');
const rendersGrid = (markdown) => parser.render(markdown).includes('data-img-grid');
test('parses canonical headers and their aliases', () => {
assert.deepEqual(parseImgGridHeader(':::img-grid'), { cols: 2, layout: 'grid' });
assert.deepEqual(parseImgGridHeader(':::img-grid cols=3'), { cols: 3, layout: 'grid' });
assert.deepEqual(parseImgGridHeader(':::img-grid cols=4 masonry'), { cols: 4, layout: 'masonry' });
assert.deepEqual(parseImgGridHeader(':::img-grid layout=masonry cols=3'), { cols: 3, layout: 'masonry' });
assert.deepEqual(parseImgGridHeader(':::img-grid grid '), { cols: 2, layout: 'grid' });
});
test('rejects headers it would have to rewrite, so they stay literal', () => {
for (const line of [
':::img-grid cols=1',
':::img-grid cols=5',
':::img-grid cols=99',
':::img-grid cols=3 cols=4',
':::img-grid masonry grid',
':::img-grid gap=4',
':::img-gridx',
'::: img-grid',
'```img-grid'
]) {
assert.equal(parseImgGridHeader(line), null, line);
}
});
test('saves canonical headers', () => {
assert.equal(serializeImgGridHeader({ cols: 2, layout: 'grid' }), ':::img-grid');
assert.equal(serializeImgGridHeader({ cols: 3, layout: 'masonry' }), ':::img-grid cols=3 masonry');
assert.equal(serializeImgGridHeader({ cols: 2, layout: 'masonry' }), ':::img-grid masonry');
assert.equal(serializeImgGrid({ cols: 2, layout: 'grid' }, ''), '', 'an empty grid saves nothing');
});
test('round-trips grids byte for byte', () => {
for (const markdown of [
':::img-grid\n![](a.png)\n![](b.png)\n:::\n',
':::img-grid cols=3 masonry\n![One](photos/one.jpg)\n![Two](photos/two.jpg)\n![Three](photos/three.jpg)\n:::\n',
':::img-grid cols=4\n![](a.png)\n:::\n'
]) {
assert.equal(roundTrip(markdown), markdown);
}
});
test('keeps small and medium sizes of grid images (Yuri: sizes became full on reload)', () => {
const markdown = ':::img-grid\n![a|size=small](a.png)\n![b|size=medium](b.png)\n![c](c.png)\n:::\n';
assert.deepEqual(parseGrids(markdown)[0].images.map((image) => image.size), ['small', 'medium', 'full']);
assert.equal(roundTrip(markdown), markdown);
});
test('leaves a grid example inside a fenced code block literal (Yuri: fences were converted)', () => {
for (const markdown of [
'```md\n:::img-grid\n![](a.png)\n:::\n```\n',
'~~~\n:::img-grid cols=3\n![](a.png)\n![](b.png)\n:::\n~~~\n',
'- item\n\n ```\n :::img-grid\n ![](a.png)\n :::\n ```\n'
]) {
assert.equal(rendersGrid(markdown), false, markdown);
assert.match(parser.render(markdown), /:::img-grid/);
}
});
test('never partially converts: captions, links, text and gaps keep the whole block as Markdown', () => {
for (const markdown of [
':::img-grid\n![](a.png)\nA caption\n![](b.png)\n:::\n',
':::img-grid\n![](a.png)\n[![](b.png)](https://example.com)\n:::\n',
':::img-grid\n![](a.png) and text\n:::\n',
':::img-grid\n![](a.png)\n\n![](b.png)\n:::\n',
':::img-grid\n![](a.png)\n![](b.png)\n',
':::img-grid\n:::\n',
':::img-grid cols=9\n![](a.png)\n:::\n'
]) {
assert.equal(rendersGrid(markdown), false, markdown);
const html = parser.render(markdown);
for (const source of markdown.match(/\]\(([^)]+)\)/g) ?? []) {
const file = source.slice(2, -1);
if (file.endsWith('.png')) assert.ok(html.includes(`src="${file}"`), `${file} kept in ${JSON.stringify(markdown)}`);
}
if (markdown.includes('A caption')) assert.match(html, /A caption/);
if (markdown.includes('https://example.com')) assert.match(html, /href="https:\/\/example.com"/);
}
});
test('parses titles and angle-bracket destinations like any other image (Yuri: title encoded into the URL)', () => {
const [grid] = parseGrids(':::img-grid\n![cat](cat.png "Photo (2024)")\n![](<my file.png>)\n![](<a (1).png>)\n:::\n');
assert.deepEqual(grid.images.map((image) => image.src), ['cat.png', 'my%20file.png', 'a%20(1).png']);
// Grid images follow the same title policy as every other image; either way the image must survive.
const saved = roundTrip(':::img-grid\n![cat](cat.png "Photo (2024)")\n![](b.png)\n:::\n');
assert.match(saved, /^:::img-grid\n!\[cat\]\(cat\.png[ )]/);
assert.deepEqual(parseGrids(saved)[0].images.map((image) => image.src), ['cat.png', 'b.png']);
assert.equal(roundTrip(saved), saved);
});
test('ampersands and quotes in alt text and paths do not grow on repeated saves (Yuri: & re-escaped)', () => {
let markdown = ':::img-grid\n![Tom & Jerry](a&b.png)\n![](c.png)\n:::\n';
for (let i = 0; i < 3; i++) markdown = roundTrip(markdown);
assert.equal(markdown, ':::img-grid\n![Tom & Jerry](a&b.png)\n![](c.png)\n:::\n');
});
test('accepts a closer trailing the last image and saves it on its own line', () => {
assert.equal(splitTrailingCloser('![](b.png) :::'), '![](b.png)');
assert.equal(splitTrailingCloser('![](b.png):::'), null);
const markdown = ':::img-grid\n![](a.png)\n![](b.png) :::\nAfter\n';
assert.equal(rendersGrid(markdown), true);
assert.match(parser.render(markdown), /<p>After<\/p>/);
assert.equal(roundTrip(markdown), ':::img-grid\n![](a.png)\n![](b.png)\n:::\n');
});
test('grids nest in lists and quotes', () => {
assert.equal(rendersGrid('- item\n\n :::img-grid\n ![](a.png)\n ![](b.png)\n :::\n'), true);
assert.equal(rendersGrid('> :::img-grid\n> ![](a.png)\n> :::\n'), true);
});
test('saved notes reload with the grid intact next to text and images', () => {
const grid = saveGrid({ cols: 3, layout: 'grid', images: [{ src: 'a.png', alt: '', size: 'full' }, { src: 'b.png', alt: '', size: 'full' }] });
const note = joinMarkdownBlocks([
{ text: 'Intro\n', isImage: false },
{ text: grid, isImage: false },
{ text: '![](c.png)\n', isImage: true },
{ text: 'Outro\n', isImage: false }
]);
assert.equal(note, 'Intro\n\n:::img-grid cols=3\n![](a.png)\n![](b.png)\n:::\n![](c.png)\n\nOutro\n');
const html = parser.render(note);
assert.equal(html.match(/data-img-grid/g)?.length, 1);
assert.match(html, /<p><img src="c.png" alt=""><\/p>/);
assert.equal(roundTrip(note), grid);
});
/** Just enough DOM for ProseMirror's DOMSerializer. */
const fakeDocument = {
createElement(tag) {
return {
tag,
attributes: {},
children: [],
setAttribute(name, value) { this.attributes[name] = value; },
appendChild(child) { this.children.push(child); return child; }
};
},
createTextNode(text) { return { text }; }
};
test('renderHTML marks grids with data-img-grid so copy and paste keeps the layout (Yuri: lost on paste)', () => {
const node = schema.nodes.imageGrid.create({ cols: 3, layout: 'masonry' }, [
schema.nodes.image.create({ src: 'a.png' }),
schema.nodes.image.create({ src: 'b.png' })
]);
const dom = DOMSerializer.fromSchema(schema).serializeNode(node, { document: fakeDocument });
assert.equal(dom.tag, 'div');
assert.equal(dom.attributes['data-img-grid'], '');
assert.equal(dom.attributes['data-cols'], '3');
assert.equal(dom.attributes['data-layout'], 'masonry');
assert.deepEqual(dom.children.map((child) => child.tag), ['img', 'img']);
// ...and that is exactly what the parse rule matches.
assert.deepEqual(schema.nodes.imageGrid.spec.parseDOM.map((rule) => rule.tag), ['div[data-img-grid]']);
});
test('groups neighboring image blocks only when there are at least two images', () => {
const blocks = [
{ isImage: false, imageCount: 0 },
{ isImage: true, imageCount: 1 },
{ isImage: true, imageCount: 2 },
{ isImage: false, imageCount: 0 },
{ isImage: true, imageCount: 1 }
];
assert.deepEqual(expandImageRange(blocks, 1), { start: 1, end: 2, total: 3 });
assert.equal(expandImageRange(blocks, 4), null);
assert.equal(expandImageRange(blocks, 0), null);
});
test('groups images a blank line apart, which load with an empty paragraph between them', () => {
const text = { isImage: false, imageCount: 0 };
const image = { isImage: true, imageCount: 1 };
const empty = { isImage: false, imageCount: 0, isEmpty: true };
// text, blank line, image, blank line, image, blank line, image, text
const blocks = [text, empty, image, empty, image, empty, image, text];
for (const target of [2, 4, 6]) {
// The empty paragraph above the first image goes too, or it would be saved as a marker above the grid.
assert.deepEqual(expandImageRange(blocks, target), { start: 1, end: 6, total: 3 });
}
// Two empty paragraphs are deliberate spacing and are not bridged.
assert.equal(expandImageRange([image, empty, empty, image], 0), null);
// An empty paragraph that does not lead to another image stays out of the range.
assert.deepEqual(expandImageRange([image, image, empty, text], 0), { start: 0, end: 1, total: 2 });
assert.deepEqual(expandImageRange([empty, image, empty, image], 1), { start: 0, end: 3, total: 2 });
assert.deepEqual(expandImageRange([image, empty, image], 2), { start: 0, end: 2, total: 2 });
});
test('drag reorder targets account for the removed item', () => {
assert.equal(reorderTarget(0, 3, 3), 2, 'drop at the end');
assert.equal(reorderTarget(2, 0, 3), 0, 'drop at the start');
assert.equal(reorderTarget(1, 1, 3), null, 'drop on itself');
assert.equal(reorderTarget(1, 2, 3), null, 'drop just after itself');
assert.equal(reorderTarget(3, 0, 3), null, 'stale index');
});
+105
View File
@@ -0,0 +1,105 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { getSchema } from '@tiptap/core';
import StarterKit from '@tiptap/starter-kit';
import Image from '@tiptap/extension-image';
import { createEditorMarkdownParser } from '../src/lib/editor/markdownParser.ts';
import { serializeImageTitle, serializeInlineMarkdown } from '../src/lib/editor/markdown.ts';
const parser = createEditorMarkdownParser();
const schema = getSchema([StarterKit, Image.configure({ inline: true })]);
const save = (attrs) => serializeInlineMarkdown(schema.nodes.paragraph.create(null, schema.nodes.image.create(attrs)));
const images = (markdown) => parser.parseInline(markdown, {})[0].children.filter((token) => token.type === 'image');
const load = (markdown) => {
const [image] = images(markdown);
return { src: image.attrGet('src'), alt: image.content, title: image.attrGet('title') };
};
const titles = [
'Photo (2024)',
'Tom & Jerry',
'&amp; is an entity',
'&#38; and &copy;',
'Say "hi"',
"it's",
'C:\\Users\\shawn',
'ends with a backslash \\',
'\\" tricky',
'[brackets] and *stars* and `ticks`',
'<b>not html</b>',
'$5 and $$',
'unicode 🎉 ✓'
];
test('image titles round-trip exactly through save and load', () => {
for (const title of titles) {
const markdown = save({ src: 'cat.png', alt: 'cat', title });
assert.deepEqual(load(markdown), { src: 'cat.png', alt: 'cat', title }, markdown);
}
});
test('repeated saves do not change a title (no growing escapes)', () => {
for (const title of titles) {
const first = save({ src: 'cat.png', alt: '', title });
let markdown = first;
for (let i = 0; i < 3; i++) markdown = save({ src: 'cat.png', alt: '', title: load(markdown).title });
assert.equal(markdown, first, title);
}
});
test('titles are written in readable CommonMark form', () => {
assert.equal(save({ src: 'cat.png', alt: 'cat', title: 'Photo (2024)' }), '![cat](cat.png "Photo (2024)")');
assert.equal(save({ src: 'cat.png', alt: '', title: 'Tom & Jerry' }), '![](cat.png "Tom & Jerry")');
assert.equal(save({ src: 'cat.png', alt: '', title: 'Say "hi"' }), '![](cat.png "Say \\"hi\\"")');
assert.equal(save({ src: 'cat.png', alt: '', title: '&amp;' }), '![](cat.png "\\&amp;")');
assert.equal(save({ src: 'cat.png', alt: '' }), '![](cat.png)');
assert.equal(serializeImageTitle(''), '');
assert.equal(serializeImageTitle(null), '');
});
test('titles written by other apps load as hover text', () => {
assert.equal(load('![cat](cat.png "Photo (2024)")').title, 'Photo (2024)');
assert.equal(load("![cat](cat.png 'single')").title, 'single');
assert.equal(load('![cat](cat.png (parens))').title, 'parens');
assert.equal(load('![cat](<my cat.png> "angle")').src, 'my%20cat.png');
assert.equal(load('![cat](<my cat.png> "angle")').title, 'angle');
});
test('legacy space and %20 destinations keep their image titles', () => {
assert.deepEqual(load('![a](my file.png "Hover")'), { src: 'my%20file.png', alt: 'a', title: 'Hover' });
assert.deepEqual(load("![a](my file.png 'Hover')"), { src: 'my%20file.png', alt: 'a', title: 'Hover' });
assert.deepEqual(load('![a](my%20file.png%20"Hover")'), { src: 'my%20file.png', alt: 'a', title: 'Hover' });
assert.deepEqual(load('![a](<my file.png>%20"Hover")'), { src: 'my%20file.png', alt: 'a', title: 'Hover' });
assert.equal(load('![a](my file.png)').title, null);
});
test('links keep their titles too', () => {
const html = parser.render('[a](x.md "Link title") ![b](y.png "Image title")');
assert.match(html, /<a href="x.md" title="Link title">a<\/a>/);
assert.match(html, /<img src="y.png" alt="b" title="Image title">/);
// A link without a title, or with an empty one, gets no title attribute.
assert.doesNotMatch(parser.render('[a](x.md) [b](y.md "")'), /title=/);
});
test('link titles round-trip exactly through save and load', () => {
const linkOf = (markdown) => parser.parseInline(markdown, {})[0].children.find((token) => token.type === 'link_open');
for (const title of titles) {
for (const href of ['https://example.com/a', 'notes/other.md']) {
const mark = schema.marks.link.create({ href, title });
let markdown = serializeInlineMarkdown(schema.nodes.paragraph.create(null, schema.text('label', [mark])));
for (let i = 0; i < 3; i++) {
const token = linkOf(markdown);
assert.deepEqual([token.attrGet('href'), token.attrGet('title')], [href, title], markdown);
const again = schema.marks.link.create({ href: token.attrGet('href'), title: token.attrGet('title') });
const next = serializeInlineMarkdown(schema.nodes.paragraph.create(null, schema.text('label', [again])));
assert.equal(next, markdown, 'the written form must not change over repeated saves');
markdown = next;
}
}
}
// Legacy destinations (spaces in the path) keep their title as well.
assert.equal(linkOf('[doc](my file.md "Caption")').attrGet('title'), 'Caption');
// No title, no suffix.
assert.equal(serializeInlineMarkdown(schema.nodes.paragraph.create(null, schema.text('a', [schema.marks.link.create({ href: 'x.md' })]))), '[a](x.md)');
});
+255
View File
@@ -0,0 +1,255 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createEditorMarkdownParser, displayMathRanges, stripLinkTitle } from '../src/lib/editor/markdownParser.ts';
const parser = createEditorMarkdownParser();
const payload = '$x$\n$$\na+b\n$$\n[[Note]]\n[label](path with spaces)\n![image](<image path> "title")\n- [x] task\n<!-- -->\n<div data-pdf-src="a.pdf" data-pdf-name="A"></div>';
const escapedCode = (html) => html.match(/<pre><code[^>]*>([\s\S]*?)<\/code><\/pre>/)?.[1];
const unescape = (html) => html.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&amp;/g, '&');
const mathPayloads = (html) => [...html.matchAll(/data-math-(?:inline|block)="([^"]*)"/g)].map(match => decodeURIComponent(unescape(match[1])));
for (const [name, opening, closing] of [
['backtick', '```text', '```'], ['tilde', '~~~text', '~~~'],
['long backtick', '````text', '````'], ['indented fence', ' ~~~text', ' ~~~'],
]) {
test(`${name} fences preserve all literal syntax`, () => {
const html = parser.render(`${opening}\n${payload}\n${closing}`, { wikiLinkTitles: [{ path: 'Note.md', title: 'Note' }] });
assert.equal(unescape(escapedCode(html)), payload);
assert.equal(mathPayloads(html).length, 0);
assert.doesNotMatch(html, /<span[^>]*data-wiki-link|<li[^>]*data-checked|<div[^>]*data-secret-block/);
});
}
test('shorter and mismatched fences remain code; following math still renders', () => {
const code = '```\n~~~\n$x$';
const html = parser.render('````text\n' + code + '\n````\n\nActual $y$.');
assert.equal(unescape(escapedCode(html)), code);
assert.deepEqual(mathPayloads(html), ['y']);
});
test('fences interrupt prose without needing a blank line', () => {
const html = parser.render('Prose\n```text\n$x$\n```\nMath $y$.');
assert.equal(unescape(escapedCode(html)), '$x$');
assert.deepEqual(mathPayloads(html), ['y']);
});
test('quoted and listed fences protect their contents', () => {
const quoted = parser.render('> ~~~text\n> $x$\n> [[Note]]\n> ~~~', { wikiLinkTitles: [] });
assert.equal(unescape(escapedCode(quoted)), '$x$\n[[Note]]');
const listed = parser.render('- item\n\n ```text\n $x$\n <!-- -->\n ```');
assert.equal(unescape(escapedCode(listed)), '$x$\n<!-- -->');
});
test('unclosed fences keep their payload literal', () => {
const html = parser.render('~~~text\n$x$\n<!-- -->');
assert.equal(unescape(escapedCode(html)), '$x$\n<!-- -->');
assert.deepEqual(mathPayloads(html), []);
});
test('fence rendering preserves leading, internal and extra trailing blank lines', () => {
const code = '\nfirst\n\n\nlast\n\n';
const html = parser.render('```text\n' + code + '\n```');
assert.equal(unescape(escapedCode(html)), code);
});
test('inline and multiline code protect math, wiki and legacy links', () => {
const html = parser.render('`` $x$ [[Note]] [a](a b) ` `` and `line\n$x$`', { wikiLinkTitles: [] });
assert.match(html, /<code[^>]*>\$x\$ \[\[Note\]\] \[a\]\(a b\) `<\/code>/);
assert.match(html, /<code[^>]*>line \$x\$<\/code>/);
assert.deepEqual(mathPayloads(html), []);
});
test('escaped dollars stay prose while real math retains exact escaped-dollar and backtick TeX', () => {
const html = parser.render('Escaped \\$x$ and $a\\$b$ and $`raw`$; $10 to $50 and $ x$ stay prose.');
assert.deepEqual(mathPayloads(html), ['a\\$b', '`raw`']);
assert.match(html, /Escaped \$x\$/);
assert.match(html, /\$10 to \$50/);
});
test('math payloads are consumed before wiki, link and task transformations', () => {
const tex = '[[Note]]\n[label](path with spaces)\n```\n- [x] TeX';
const html = parser.render('$$\n' + tex + '\n$$\n\n$x+y$', { wikiLinkTitles: [] });
assert.deepEqual(mathPayloads(html), [tex, 'x+y']);
assert.doesNotMatch(html, /data-wiki-link|data-checked|<pre>/);
});
test('unclosed display math stays literal and normal fenced indentation convention stays unchanged', () => {
assert.deepEqual(mathPayloads(parser.render('$$\nunclosed')), []);
assert.doesNotMatch(parser.render('\tordinary tab-indented text'), /<pre>/);
});
test('semantic math ranges do not mistake fenced or inline code for display math', () => {
const source = '~~~text\n$$\n# code\n$$\n~~~\n\n> $$\n> x\n> ---\n> $$\n\n# Real';
const ranges = displayMathRanges(source);
assert.equal(ranges.length, 1);
assert.equal(source.slice(ranges[0].from, ranges[0].to), '> $$\n> x\n> ---\n> $$\n');
assert.deepEqual(displayMathRanges('```text\n$$\n```\n\n# Real'), []);
});
test('wiki lookup preserves path precedence, shallowest title and escaped attributes', () => {
const env = { wikiLinkTitles: [
{ path: 'deep/nested/Note.md', title: 'Note' }, { path: 'folder/Note.md', title: 'NOTE' },
{ path: 'other/Note.md', title: 'Note' }, { path: 'Quoted".md', title: 'Quoted"' },
] };
const html = parser.render('[[Note]] [[deep/nested/Note#Heading|Alias]] [[Quoted"]] [[Missing]]', env);
assert.match(html, /data-path="folder\/Note\.md"/);
assert.match(html, /data-path="deep\/nested\/Note\.md" data-title="deep\/nested\/Note#Heading"[^>]*>Alias<\/span>/);
assert.match(html, /data-path="Quoted&quot;\.md" data-title="Quoted&quot;"/);
assert.match(html, /data-path="" data-title="Missing"/);
assert.doesNotMatch(parser.render('\\[[Note]]', env), /data-wiki-link/);
assert.doesNotMatch(parser.render('[[Note]]'), /data-wiki-link/);
});
test('wiki lookup reads titles once per render and refreshes after in-place cache changes', () => {
let reads = 0;
const notes = Array.from({ length: 250 }, (_, index) => ({
path: `folder/Note ${index}.md`, get title() { reads++; return `Note ${index}`; },
}));
const env = { wikiLinkTitles: notes };
const source = '[[Note 249]] '.repeat(100);
parser.render(source, env);
assert.equal(reads, notes.length);
notes[249] = { path: 'Moved.md', title: 'Note 249' };
assert.match(parser.render(source, env), /data-path="Moved\.md"/);
});
test('ordinary and legacy links/images remain functional while unsafe schemes remain rejected', () => {
const html = parser.render('[a](<path with spaces> "title") [b](path with spaces) [c](path%20"title") ![d](<image path>%20"title") ![e](///home/user/image.png)');
assert.match(html, /href="path%20with%20spaces"/);
assert.match(html, /href="path"/);
assert.match(html, /src="image%20path"/);
assert.match(html, /src="\/home\/user\/image\.png"/);
// Links and images keep their titles, legacy destinations included; a link without one gets none.
assert.match(html, /<a href="path%20with%20spaces" title="title">a<\/a>/);
assert.match(html, /<a href="path%20with%20spaces">b<\/a>/);
assert.match(html, /<a href="path" title="title">c<\/a>/);
assert.match(html, /<img src="image%20path" alt="d" title="title">/);
assert.match(html, /<img src="\/home\/user\/image\.png" alt="e">/);
assert.doesNotMatch(parser.render('[bad](javascript:alert with spaces) ![bad](data:text/html;base64,foo bar)'), /href=|src=/);
});
test('compatibility destinations retain quoted parentheses and native nested URLs', () => {
const html = parser.render('[a](path with spaces "a (title)") [b](<image path> (title)) [c](path(one))');
assert.match(html, /href="path%20with%20spaces"/);
assert.match(html, /href="image%20path"/);
assert.match(html, /href="path\(one\)"/);
});
test('task and empty-paragraph handling uses real block structure, including nested and empty tasks', () => {
const html = parser.render('- [x] **done**\n- [ ]\n - [ ] nested\n\n<!-- -->\n# Next');
assert.equal([...html.matchAll(/data-type="taskList"/g)].length, 2);
assert.equal([...html.matchAll(/data-type="taskItem"/g)].length, 3);
assert.match(html, /data-checked="true"[^>]*>[\s\S]*<strong>done<\/strong>/);
assert.match(html, /<p><\/p>\n<h1>Next<\/h1>/);
assert.doesNotMatch(parser.render('`- [x] not a task` and `<!-- -->`'), /data-checked|<p><\/p>/);
});
test('image-only gaps remain outside code and do not alter inline images', () => {
assert.match(parser.render('Text\n\n![a](image.png)'), /<p>Text<\/p>\n<p><\/p>\n<p><img/);
assert.doesNotMatch(parser.render('Text ![a](image.png)'), /<p><\/p>/);
});
test('only actual closed secret fences become secret blocks, not literal examples', () => {
assert.match(parser.render('```helix-secret\nexample-payload\n```'), /data-secret-block="example-payload"/);
const literal = '```helix-secret\nexample-payload\n```';
assert.equal(unescape(escapedCode(parser.render('````text\n' + literal + '\n````'))), literal);
assert.doesNotMatch(parser.render('```helix-secret\nunclosed'), /data-secret-block/);
});
test('invalid final secret-fence markers remain literal code', () => {
for (const closing of [' ```', '\t```', '```\u00a0']) {
const html = parser.render('```helix-secret\nexample-payload\n' + closing);
assert.doesNotMatch(html, /data-secret-block/);
assert.equal(unescape(escapedCode(html)), 'example-payload\n' + closing);
}
});
test('code whitespace is marked for DOM import without changing soft or hard line-break semantics', () => {
const html = parser.render('# ` `\n\nbefore `trailing `\nnext \nlast');
assert.match(html, /<h1 data-helix-code="">/);
assert.match(html, /<p data-helix-code="">/);
assert.match(html, /<\/code> next<br>last/);
});
test('a callout keeps a newline between its title line and body, while ordinary soft breaks stay spaces', () => {
assert.match(parser.render('> [!warning] Heads up\n> body here\n> more'), /\[!warning\] Heads up\nbody here more/);
assert.match(parser.render('> [!note]\n> body\n> more'), /\[!note\]\nbody more/);
assert.match(parser.render('> [!todo] Outer\n> > [!tip] Inner\n> > body'), /\[!todo\] Outer<\/p>[\s\S]*\[!tip\] Inner\nbody/);
assert.match(parser.render('> > [!tip] Inner\n> > body'), /\[!tip\] Inner\nbody/);
assert.match(parser.render('> plain\n> quote'), /plain quote/);
assert.match(parser.render('one\ntwo'), /one two/);
assert.match(parser.render('> [!tip] Title only'), /\[!tip\] Title only<\/p>/);
});
test('task markers only turn bullet-list items into tasks, never numbered items', () => {
const ordered = parser.render('1. [x] done\n2. [ ] todo\n3. plain');
assert.doesNotMatch(ordered, /data-type|data-checked/);
assert.match(ordered, /<li>\[x\] done<\/li>\s*<li>\[ \] todo<\/li>/);
const bullets = parser.render('- [x] done\n- [ ] todo');
assert.equal([...bullets.matchAll(/data-type="taskItem"/g)].length, 2);
assert.match(bullets, /<ul data-type="taskList">/);
const nested = parser.render('1. one\n - [x] nested task');
assert.equal([...nested.matchAll(/data-type="taskItem"/g)].length, 1);
assert.doesNotMatch(nested, /<ol[^>]*data-type/);
});
test('code spans mark simple list items and table cells so whitespace-only code survives DOM import', () => {
assert.match(parser.render('- a ` ` b\n- plain'), /<li data-helix-code="">a <code[^>]*> <\/code> b<\/li>\s*<li>plain<\/li>/);
assert.match(parser.render('1. one ` ` two'), /<li data-helix-code="">one /);
assert.match(parser.render('- [ ] task ` ` x'), /<li data-type="taskItem" data-checked="false" data-helix-code="">/);
assert.match(parser.render('| a | b |\n| - | - |\n| ` ` | c |'), /<td data-helix-code=""><code[^>]*> <\/code><\/td>\s*<td>c<\/td>/);
assert.match(parser.render('| ` ` | b |\n| - | - |\n| x | y |'), /<th data-helix-code="">/);
});
test('list items with nested blocks are not marked, so no stray whitespace text appears around them', () => {
const nested = parser.render('- a `x`\n - nested `y`');
assert.match(nested, /<li>a <code/);
assert.match(nested, /<li data-helix-code="">nested /);
assert.doesNotMatch(parser.render('- item `x`\n\n ```js\n code\n ```'), /<li data-helix-code/);
assert.doesNotMatch(parser.render('- no code here\n- still none'), /data-helix-code/);
// Loose lists keep their paragraph marker as before.
assert.match(parser.render('- loose ` ` x\n\n- second'), /<li>\s*<p data-helix-code="">loose /);
});
test('stripLinkTitle behaves exactly like the title expression it replaced, on every short combination', () => {
const oracle = (destination) => destination.replace(/(?:\s|%20)+(?:"[^"]*"|'[^']*')(?:\s|%20)*$/u, '').trim();
const tokens = ['%20', ' ', '"', "'", 'a', '\t', '%2', '<'];
let checked = 0;
const walk = (prefix, depth) => {
const input = prefix.trim(); // the parser trims the destination first
assert.equal(stripLinkTitle(input), oracle(input), JSON.stringify(input));
checked++;
if (depth === 0) return;
for (const token of tokens) walk(prefix + token, depth - 1);
};
walk('', 6);
assert.ok(checked > 200000);
for (const title of ['page.png "Caption"', "page.png 'Caption'", 'page.png%20"Caption"', 'a "b" "c"', 'a"b"', 'x "it\'s"%20%20']) {
assert.equal(stripLinkTitle(title), oracle(title), title);
}
});
test('a link destination with a very long run of spaces is parsed in linear time', () => {
for (const spaces of [5000, 40000]) {
const started = performance.now();
parser.render('[a](x' + ' '.repeat(spaces) + 'y)');
assert.ok(performance.now() - started < 300, `${spaces} spaces took ${Math.round(performance.now() - started)} ms`);
}
// Titles still work next to long whitespace.
assert.match(parser.render('![a](images/photo one.png "Caption")'), /src="images\/photo%20one.png"/);
});
test('a line with thousands of unclosed link starts is parsed in linear time', () => {
// Each start used to read to the end of the line looking for its `)`: about 1 s at 4000 starts.
for (const unit of ['![a](x.png "a ', '![a](x.png (t ', '[a](x.png "a ', '![a]( ']) {
for (const closer of ['', ')']) {
const started = performance.now();
parser.render(unit.repeat(8000) + closer);
assert.ok(performance.now() - started < 400, `${JSON.stringify(unit)} x 8000${closer} took ${Math.round(performance.now() - started)} ms`);
}
}
// Legacy links still resolve after many failed starts on the same line, and on later lines.
const noise = '[a]( '.repeat(500);
assert.match(parser.render(noise + '[doc](my file.md) and ![pic](photo one.png "Caption")'), /href="my%20file.md".*src="photo%20one.png"/s);
assert.match(parser.render(noise + '\n[doc](my file.md)'), /href="my%20file.md"/);
});
+449 -4
View File
@@ -3,7 +3,7 @@ import { readFile } from 'node:fs/promises';
import test from 'node:test';
import MarkdownIt from 'markdown-it';
import { Schema } from '@tiptap/pm/model';
import { transformWithEsbuild } from 'vite';
import { createServer, transformWithEsbuild } from 'vite';
const source = await readFile(
new URL('../src/lib/editor/markdown.ts', import.meta.url),
@@ -14,22 +14,39 @@ const { code } = await transformWithEsbuild(source, 'markdown.ts', {
format: 'esm',
target: 'esnext'
});
const { serializeInlineMarkdown } = await import(
const { joinListItemBlocks, joinMarkdownBlocks, serializeCodeBlock, serializeInlineMarkdown, serializeInlineMarkdownKeepingDollars } = await import(
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
);
const vite = await createServer({
configFile: false,
server: { middlewareMode: true },
appType: 'custom'
});
let editorMarkdown;
try {
const { createEditorMarkdownParser } = await vite.ssrLoadModule(
new URL('../src/lib/editor/markdownParser.ts', import.meta.url).pathname
);
editorMarkdown = createEditorMarkdownParser();
} finally {
await vite.close();
}
// Link intentionally precedes formatting marks in the schema. This matches the
// mark order produced by the app and reproduces the original per-text-node bug.
const schema = new Schema({
nodes: {
doc: { content: 'block+' },
paragraph: { content: 'text*', group: 'block' },
text: { group: 'inline' }
paragraph: { content: 'inline*', group: 'block' },
text: { group: 'inline' },
mathInline: { inline: true, group: 'inline', atom: true, attrs: { tex: { default: '' } } }
},
marks: {
link: { attrs: { href: {} } },
bold: {},
italic: {},
code: {},
wikiLink: {
attrs: {
title: { default: null },
@@ -116,3 +133,431 @@ test('preserves wiki-link aliases inside a bold span', () => {
assert.equal(serialized, '**See [[Target note|this note]] now**');
});
test('round-trips inline code backticks, delimiter padding and literal whitespace', () => {
const codeMark = schema.marks.code.create();
for (const text of [
'a`b',
'a``b',
'a````b',
'`',
'``',
'`edge`',
' a ',
' leading',
'trailing ',
' ',
' ',
'\tleading',
'\u2003leading',
'\t\u2003 interior\t\u2003 ',
'\\$x$'
]) {
const serialized = serializeInlineMarkdown(paragraph([{ text, marks: [codeMark] }]));
const tokens = editorMarkdown.parseInline(serialized, {})[0].children;
assert.deepEqual(tokens.map((token) => [token.type, token.content]), [['code_inline', text]], text);
assert.equal(editorMarkdown.render(serialized).includes('data-math-inline'), false, text);
}
assert.equal(serializeInlineMarkdown(paragraph([{ text: '`', marks: [codeMark] }])), '`` ` ``');
assert.equal(serializeInlineMarkdown(paragraph([{ text: ' a ', marks: [codeMark] }])), '` a `');
assert.equal(serializeInlineMarkdown(paragraph([{ text: ' ', marks: [codeMark] }])), '` `');
});
test('joins contiguous code across formatting changes with code taking precedence', () => {
const codeMark = schema.marks.code.create();
const serialized = serializeInlineMarkdown(paragraph([
{ text: '`a', marks: [codeMark, bold] },
{ text: '``', marks: [codeMark, italic] },
{ text: 'b`', marks: [codeMark] }
]));
assert.equal(serialized, '``` `a``b` ```');
assert.deepEqual(
editorMarkdown.parseInline(serialized, {})[0].children.map((token) => [token.type, token.content]),
[['code_inline', '`a``b`']]
);
assert.equal(editorMarkdown.render(serialized), '<p data-helix-code=""><code style="white-space: pre-wrap">`a``b`</code></p>\n');
});
test('keeps link syntax outside code and joins code across formatting changes within links', () => {
const codeMark = schema.marks.code.create();
const serialized = serializeInlineMarkdown(paragraph([
{ text: 'before ', marks: [] },
{ text: '`a', marks: [codeMark, link, bold] },
{ text: 'b`', marks: [codeMark, link, italic] },
{ text: ' after', marks: [] }
]));
assert.equal(serialized, 'before [`` `ab` ``](https://example.com) after');
assert.equal(
editorMarkdown.render(serialized),
'<p data-helix-code="">before <a href="https://example.com"><code style="white-space: pre-wrap">`ab`</code></a> after</p>\n'
);
});
test('keeps formatting that covers a whole code span', () => {
const codeMark = schema.marks.code.create();
assert.equal(serializeInlineMarkdown(paragraph([{ text: 'x.ps1', marks: [codeMark, bold] }])), '**`x.ps1`**');
assert.equal(serializeInlineMarkdown(paragraph([{ text: 'x', marks: [codeMark, italic] }, { text: ' rest', marks: [] }])), '*`x`* rest');
// Bold text running into bold code stays one bold run.
assert.equal(serializeInlineMarkdown(paragraph([{ text: 'Run ', marks: [bold] }, { text: 'make', marks: [codeMark, bold] }, { text: ' now', marks: [bold] }])), '**Run `make` now**');
// What is written loads back as bold code.
assert.match(editorMarkdown.render('**`x.ps1`**'), /<strong><code[^>]*>x\.ps1<\/code><\/strong>/);
});
test('writes spaces at the edges of bold and italic outside the delimiters (issues #240, #276)', () => {
const cases = [
[[{ text: 'bold ', marks: [bold] }, { text: 'rest', marks: [] }], '**bold** rest', '<strong>bold</strong> rest'],
[[{ text: 'pre', marks: [] }, { text: ' bold', marks: [bold] }, { text: ' rest', marks: [] }], 'pre **bold** rest', 'pre <strong>bold</strong> rest'],
[[{ text: 'word ', marks: [italic] }, { text: '.', marks: [] }], '*word* .', '<em>word</em> .'],
[[{ text: 'a ', marks: [] }, { text: 'bold ', marks: [bold] }, { text: 'it ', marks: [bold, italic] }, { text: 'end', marks: [] }], 'a **bold *it*** end', 'a <strong>bold <em>it</em></strong> end'],
[[{ text: 'one ', marks: [bold] }, { text: 'two ', marks: [italic] }, { text: 'three', marks: [] }], '**one** *two* three', '<strong>one</strong> <em>two</em> three'],
[[{ text: ' both ', marks: [bold] }], ' **both** ', ' <strong>both</strong> '],
// Spaces inside a formatted run stay inside it.
[[{ text: 'a', marks: [bold] }, { text: ' ', marks: [bold] }, { text: 'b', marks: [bold] }], '**a b**', '<strong>a b</strong>'],
// A mark over nothing but spaces is not written.
[[{ text: ' ', marks: [bold] }, { text: 'x', marks: [] }], ' x', ' x'],
[[{ text: 'link ', marks: [bold, link] }, { text: 'x', marks: [] }], '**[link](https://example.com)** x', '<strong><a href="https://example.com">link</a></strong> x'],
];
for (const [segments, markdown, html] of cases) {
assert.equal(serializeInlineMarkdown(paragraph(segments)), markdown);
assert.equal(editorMarkdown.renderInline(markdown), html, markdown);
}
});
test('drops formatting around a code span that a letter or digit touches', () => {
const codeMark = schema.marks.code.create();
// `**\`code\`**s` is not emphasis in Markdown, so the code span is written plain there.
assert.equal(serializeInlineMarkdown(paragraph([{ text: 'code', marks: [codeMark, bold] }, { text: 's', marks: [] }])), '`code`s');
assert.equal(serializeInlineMarkdown(paragraph([{ text: 'the', marks: [] }, { text: 'code', marks: [codeMark, bold] }, { text: ' x', marks: [] }])), 'the`code` x');
assert.equal(serializeInlineMarkdown(paragraph([{ text: 'Run ', marks: [bold] }, { text: 'make', marks: [codeMark, bold] }, { text: 's', marks: [] }])), '**Run** `make`s');
// Next to spaces or punctuation the formatting is kept.
assert.equal(serializeInlineMarkdown(paragraph([{ text: '(', marks: [] }, { text: 'x', marks: [codeMark, bold] }, { text: ').', marks: [] }])), '(**`x`**).');
assert.match(editorMarkdown.renderInline('(**`x`**).'), /<strong><code[^>]*>x<\/code><\/strong>/);
});
test('preserves formatting on non-code parts of a link', () => {
const codeMark = schema.marks.code.create();
const serialized = serializeInlineMarkdown(paragraph([
{ text: 'first', marks: [link, bold] },
{ text: 'code', marks: [link, codeMark] },
{ text: 'last', marks: [link, bold] }
]));
assert.equal(
editorMarkdown.render(serialized),
'<p data-helix-code=""><strong><a href="https://example.com">first</a></strong><a href="https://example.com"><code style="white-space: pre-wrap">code</code></a><strong><a href="https://example.com">last</a></strong></p>\n'
);
});
test('round-trips code blocks with embedded fences and every blank-line boundary', () => {
for (const text of [
'',
'plain',
'\nleading',
'trailing\n',
'\n\nleading\n\nmiddle\n\ntrailing\n\n',
'\t \n\u2003\n\n',
'```\nembedded\n```\n',
'``````\n~~~\n$not math$\n```'
]) {
const serialized = serializeCodeBlock(text, 'js');
const [fence] = markdown.parse(serialized, {});
const expectedFence = '`'.repeat(Math.max(3, ...[...text.matchAll(/`+/g)].map((match) => match[0].length + 1)));
assert.equal(serialized, `${expectedFence}js\n${text}\n${expectedFence}\n`, text);
assert.equal(fence.type, 'fence', text);
assert.equal(fence.info, 'js', text);
// CommonMark includes the structural newline before the closing fence.
assert.equal(fence.content, `${text}\n`, text);
assert.equal(fence.content.slice(0, -1), text);
assert.equal(editorMarkdown.render(serialized).includes('data-math-inline'), false, text);
}
assert.equal(serializeCodeBlock('code'), '```\ncode\n```\n');
});
test('escapes literal prose dollars without changing preceding literal backslashes', () => {
for (const text of [
'$x$',
'$$',
'cost $5 and $10',
'\\$x$',
'\\\\$x$',
'\\\\\\$x$',
'before \\$x\\$ after',
'$a$ and $b$',
'literal $with spaces$',
'ordinary punctuation: a+b, c=d; e/f!'
]) {
const serialized = serializeInlineMarkdown(paragraph([{ text, marks: [] }]));
const tokens = editorMarkdown.parseInline(serialized, {})[0].children;
assert.equal(tokens.every((token) => token.type === 'text'), true, text);
assert.equal(tokens.map((token) => token.content).join(''), text);
assert.equal(editorMarkdown.render(serialized).includes('data-math-inline'), false, text);
}
assert.equal(serializeInlineMarkdown(paragraph([{ text: '$x$', marks: [] }])), '\\$x\\$');
assert.equal(
serializeInlineMarkdown(paragraph([{ text: 'ordinary punctuation: a+b, c=d; e/f!', marks: [] }])),
'ordinary punctuation: a+b, c=d; e/f!'
);
});
test('keeps escaped Markdown dollars as prose through load, save and reload', () => {
for (const source of ['\\$x\\$', 'price \\$5', '\\\\\\$x\\$', '\\\\\\\\\\$x\\$']) {
const loaded = editorMarkdown.parseInline(source, {})[0].children;
assert.equal(loaded.every((token) => token.type === 'text'), true);
const text = loaded.map((token) => token.content).join('');
const serialized = serializeInlineMarkdown(paragraph([{ text, marks: [] }]));
const reloaded = editorMarkdown.parseInline(serialized, {})[0].children;
assert.equal(reloaded.every((token) => token.type === 'text'), true);
assert.equal(reloaded.map((token) => token.content).join(''), text);
}
});
test('preserves real inline math TeX and falls back to existing encoded HTML when needed', () => {
for (const tex of [
'x^2',
'\\frac{a}{b}',
'',
' ',
' leading',
'trailing ',
'\tleading',
'trailing\u2003',
'a\nb',
'a\rb',
'x$y',
'\\$',
'trailing\\'
]) {
const mathNode = schema.node('mathInline', { tex });
const serialized = serializeInlineMarkdown(schema.node('paragraph', null, [mathNode]));
const html = editorMarkdown.render(serialized);
const stored = html.match(/data-math-inline="([^"]*)"/);
assert.ok(stored, tex);
assert.equal(decodeURIComponent(stored[1]), tex);
if (tex === 'x^2' || tex === '\\frac{a}{b}') {
assert.equal(serialized, `$${tex}$`);
} else {
assert.equal(serialized, `<span data-math-inline="${encodeURIComponent(tex)}" class="math-inline"></span>`);
}
const reloaded = schema.node('paragraph', null, [
schema.node('mathInline', { tex: decodeURIComponent(stored[1]) })
]);
assert.equal(serializeInlineMarkdown(reloaded), serialized);
}
});
test('keeps literal dollars escaped within rich formatting and link labels', () => {
const serialized = serializeInlineMarkdown(paragraph([
{ text: 'before ', marks: [bold] },
{ text: '$literal$', marks: [bold, link] },
{ text: ' after', marks: [bold] }
]));
assert.equal(serialized, '**before [\\$literal\\$](https://example.com) after**');
assert.equal(
editorMarkdown.render(serialized),
'<p><strong>before <a href="https://example.com">$literal$</a> after</strong></p>\n'
);
});
test('uses encoded math storage at ambiguous dollar, digit and adjacent-atom boundaries', () => {
const mathNode = schema.node('mathInline', { tex: 'x' });
for (const children of [
[mathNode, schema.text('2')],
[mathNode, schema.text('$literal$')],
[schema.text('$literal$'), mathNode],
[mathNode, schema.node('mathInline', { tex: 'y' })]
]) {
const serialized = serializeInlineMarkdown(schema.node('paragraph', null, children));
const stored = [...editorMarkdown.render(serialized).matchAll(/data-math-inline="([^"]*)"/g)];
const expected = children.filter((child) => child.type.name === 'mathInline').map((child) => child.attrs.tex);
assert.deepEqual(stored.map((match) => decodeURIComponent(match[1])), expected);
assert.equal(serialized.startsWith('$x$'), false);
}
});
test('preserves trailing prose backslashes before real math atoms', () => {
for (const text of ['\\', '\\\\', '\\\\\\']) {
const serialized = serializeInlineMarkdown(schema.node('paragraph', null, [
schema.text(text),
schema.node('mathInline', { tex: 'x' })
]));
const tokens = editorMarkdown.parseInline(serialized, {})[0].children;
assert.equal(tokens[0].type, 'text');
assert.equal(tokens[0].content, text);
const stored = editorMarkdown.render(serialized).match(/data-math-inline="([^"]*)"/);
assert.ok(stored);
assert.equal(decodeURIComponent(stored[1]), 'x');
}
});
test('splits contiguous code at link boundaries without losing code text or link targets', () => {
const codeMark = schema.marks.code.create();
const otherLink = schema.marks.link.create({ href: 'https://other.example' });
const serialized = serializeInlineMarkdown(paragraph([
{ text: '`first', marks: [codeMark] },
{ text: 'second`', marks: [codeMark, link] },
{ text: 'third', marks: [codeMark, otherLink] },
{ text: 'last', marks: [codeMark] }
]));
assert.equal(
editorMarkdown.render(serialized),
'<p data-helix-code=""><code style="white-space: pre-wrap">`first</code><a href="https://example.com"><code style="white-space: pre-wrap">second`</code></a><a href="https://other.example"><code style="white-space: pre-wrap">third</code></a><code style="white-space: pre-wrap">last</code></p>\n'
);
});
const block = (text) => ({ text, isImage: false });
const image = { text: '![x](a.png)\n', isImage: true };
const emptyParagraph = { text: '', isImage: false, isEmpty: true };
test('joined blocks are separated by exactly one blank line, including after lists', () => {
assert.equal(joinMarkdownBlocks([block('one\n'), block('two\n')]), 'one\n\ntwo\n');
assert.equal(joinMarkdownBlocks([block('- a\n- b\n\n'), block('text\n')]), '- a\n- b\n\ntext\n');
assert.equal(joinMarkdownBlocks([block('- a\n\n'), block('1. b\n\n'), block('text\n')]), '- a\n\n1. b\n\ntext\n');
assert.equal(joinMarkdownBlocks([block('text\n'), block('- a\n\n')]), 'text\n\n- a\n');
});
test('empty paragraphs stay as marker lines between blocks and are dropped at the end', () => {
assert.equal(
joinMarkdownBlocks([block('a\n'), emptyParagraph, emptyParagraph, block('b\n'), emptyParagraph, emptyParagraph]),
'a\n\n<!-- -->\n<!-- -->\nb\n'
);
assert.equal(joinMarkdownBlocks([emptyParagraph]), '\n');
});
test('an empty paragraph before an image is written as a blank line that parses back to it', () => {
const gap = '<p></p>\n<p><img';
for (const before of [block('# T\n'), block('para\n'), block('- a\n\n'), block('```\ncode\n```\n')]) {
const joined = joinMarkdownBlocks([before, emptyParagraph, image, block('text\n')]);
assert.equal(joined, `${before.text.replace(/\n*$/, '\n')}\n![x](a.png)\n\ntext\n`, before.text);
assert.doesNotMatch(joined, /<!--/);
assert.ok(editorMarkdown.render(joined).includes(gap), joined);
// Saving what was just loaded must not change it.
assert.equal(joinMarkdownBlocks([before, emptyParagraph, image, block('text\n')]), joined);
}
// A second empty paragraph keeps its marker and still leaves a blank line before the image.
const twice = joinMarkdownBlocks([block('para\n'), emptyParagraph, emptyParagraph, image]);
assert.equal(twice, 'para\n\n<!-- -->\n\n![x](a.png)\n');
assert.equal(editorMarkdown.render(twice).match(/<p><\/p>/g)?.length, 2);
});
test('an image placed directly after a block stays directly after it', () => {
const joined = joinMarkdownBlocks([block('# T\n'), image]);
assert.equal(joined, '# T\n![x](a.png)\n');
assert.doesNotMatch(editorMarkdown.render(joined), /<p><\/p>/);
});
test('joining never collapses blank lines inside code blocks', () => {
const code = serializeCodeBlock('a\n\n\n\nb');
assert.equal(joinMarkdownBlocks([block(code), block('after\n')]), code + '\nafter\n');
assert.ok(joinMarkdownBlocks([block(code), block('after\n')]).includes('a\n\n\n\nb'));
});
const mathPayloads = (markdown) => (editorMarkdown.parseInline(markdown, {})[0]?.children ?? [])
.filter((token) => token.type === 'helix_math_inline').map((token) => token.content);
const keepDollars = (children) => serializeInlineMarkdownKeepingDollars(schema.node('paragraph', null, children), undefined, mathPayloads);
const mathAtom = (tex) => schema.node('mathInline', { tex });
// What a reader of the saved Markdown sees: plain text and formulas, in order.
const readBack = (markdown) => (editorMarkdown.parseInline(markdown, {})[0]?.children ?? []).map((token) =>
token.type === 'helix_math_inline' ? `\u0000${token.content}\u0000` : token.type === 'text' ? token.content : '').join('');
const authored = (children) => children.map((child) => child.type.name === 'mathInline' ? `\u0000${child.attrs.tex}\u0000` : child.text).join('');
test('dollar signs that cannot form a formula are saved as typed', () => {
for (const text of ['Cost $5 and $6 total', 'Price is $5.', 'Only $', '$ spaced $', 'a$b', '5$ and 6$', 'two $5 or $10']) {
assert.equal(keepDollars([schema.text(text)]), text, text);
}
});
test('dollar signs that would form a formula stay escaped, so literal text is never turned into math', () => {
for (const text of ['$x$', 'cost $x$ here', '$a+b$', 'x $y$ z $w$', '$$', 'a $$ b']) {
const saved = keepDollars([schema.text(text)]);
assert.deepEqual(mathPayloads(saved), [], `${text} -> ${saved}`);
assert.equal(readBack(saved), text, `${text} -> ${saved}`);
}
});
test('real formulas keep their dollars and are not re-paired with neighbouring text', () => {
const nodes = [schema.text('cost $5 and '), mathAtom('x'), schema.text(' then $6')];
const saved = keepDollars(nodes);
assert.deepEqual(mathPayloads(saved), ['x']);
assert.equal(readBack(saved), authored(nodes));
const pairing = [schema.text('$a'), mathAtom('x'), schema.text('b')];
const pairingSaved = keepDollars(pairing);
assert.deepEqual(mathPayloads(pairingSaved), ['x'], pairingSaved);
assert.equal(readBack(pairingSaved), authored(pairing));
});
test('backslashes before an unescaped dollar are doubled so the dollar stays literal', () => {
for (const text of ['a\\$5', 'a\\\\$5', 'path\\ $5 and $6']) {
const saved = keepDollars([schema.text(text)]);
assert.equal(readBack(saved), text, `${text} -> ${saved}`);
}
});
test('fuzz: saving text and formulas keeps exactly what was written, whatever the dollars do', () => {
const alphabet = ['a', 'b', '1', '5', ' ', '$', '$', '\\', '*', '_', '`', 'x', '.', '\n'.slice(0, 0) || ' '];
let seed = 12345;
const random = (n) => { seed = (seed * 1103515245 + 12345) & 0x7fffffff; return seed % n; };
for (let round = 0; round < 4000; round++) {
const children = [];
for (let part = 0, count = 1 + random(4); part < count; part++) {
if (random(5) === 0) children.push(mathAtom(['x', 'a+b', '5', 'y z'][random(4)]));
else {
const text = Array.from({ length: 1 + random(8) }, () => alphabet[random(alphabet.length)]).join('');
if (text.length) children.push(schema.text(text));
}
}
// Adjacent text nodes would merge; adjacent atoms are covered by the serializer's own encoding rules.
const merged = children.reduce((list, child) => {
const last = list[list.length - 1];
if (last?.isText && child.isText) list[list.length - 1] = schema.text(last.text + child.text); else list.push(child);
return list;
}, []);
if (!merged.length) continue;
const saved = keepDollars(merged);
const baseline = serializeInlineMarkdown(schema.node('paragraph', null, merged));
assert.equal(readBack(saved), readBack(baseline), JSON.stringify({ saved, baseline, merged: authored(merged) }));
}
});
const item = (text, isList = false) => ({ text, isList });
test('blocks after a list item\'s first paragraph stay inside the item, separated by a blank line', () => {
assert.equal(joinListItemBlocks([item('only')]), 'only\n');
// A nested list arrives ending in a blank line; it must not leave an indented blank line behind (issue #277).
const list = (text) => ({ text, isList: true });
assert.equal(joinListItemBlocks([item('parent'), list('- a\n- b\n\n')]), 'parent\n - a\n - b\n');
assert.equal(joinListItemBlocks([item('parent'), list('- a\n - deep\n\n')]), 'parent\n - a\n - deep\n');
assert.equal(joinListItemBlocks([item('first'), item('second\n')]), 'first\n\n second\n');
assert.equal(
joinListItemBlocks([item('step'), item('```sh\nrun\n\n\nagain\n```\n'), item('after\n')]),
'step\n\n ```sh\n run\n\n\n again\n ```\n\n after\n',
);
assert.equal(joinListItemBlocks([item('quoted'), item('> a\n>\n> b\n')]), 'quoted\n\n > a\n >\n > b\n');
assert.equal(joinListItemBlocks([item('first'), item('\n'), item(' \n'), item('real\n')]), 'first\n\n real\n');
});
test('nested lists keep their direct four-space indentation under the item', () => {
assert.equal(joinListItemBlocks([item('a'), item('- b\n', true)]), 'a\n - b\n');
assert.equal(joinListItemBlocks([item('a'), item('- b\n', true), item('```\nc\n```\n')]), 'a\n - b\n\n ```\n c\n ```\n');
});
test('joined list items parse back with their code, paragraphs and quotes still inside the item', () => {
for (const marker of ['- ', '1. ', '10. ', '- [ ] ']) {
const text = marker + joinListItemBlocks([item('step'), item('```js\nconst a = 1;\n\nconst b = 2;\n```\n'), item('more text\n'), item('> quoted\n')]);
const html = editorMarkdown.render(text + '- next\n');
const listItem = html.match(/<li[^>]*>[\s\S]*?<\/li>/)[0];
assert.match(listItem, /<pre><code[^>]*>const a = 1;\n\nconst b = 2;<\/code><\/pre>/, marker);
assert.match(listItem, /<p>more text<\/p>/, marker);
assert.match(listItem, /<blockquote>\s*<p>quoted<\/p>/, marker);
assert.equal(html.match(/<li[ >]/g).length, 2, `${marker}: the following item is a separate item`);
}
});
test('joining a very large note takes linear time', () => {
const blocks = Array.from({ length: 30000 }, (_, index) => block(`Paragraph ${index} with some words in it.\n`));
for (let index = 0; index < blocks.length; index += 200) blocks[index] = block('- a\n- b\n\n');
const started = performance.now();
const joined = joinMarkdownBlocks(blocks);
assert.ok(performance.now() - started < 500, 'building the note must not re-read the growing text for every block');
assert.ok(joined.startsWith('- a\n- b\n\nParagraph 1 with'));
assert.equal(joined.endsWith('Paragraph 29999 with some words in it.\n'), true);
assert.ok(!joined.includes('\n\n\n'));
});
+162
View File
@@ -0,0 +1,162 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { createContext, Script } from 'node:vm';
import { parse } from 'svelte/compiler';
import { transformWithEsbuild } from 'vite';
import { RevisionSaveQueue } from '../src/lib/utils/revision-save.ts';
import { beginNoteNavigation, isCurrentNoteNavigation } from '../src/lib/utils/note-navigation.ts';
const source = await readFile(new URL('../src/lib/components/NoteList.svelte', import.meta.url), 'utf8');
const ast = parse(source, { modern: true });
const declaration = ast.instance.content.body.find(node =>
node.type === 'ExportNamedDeclaration' && node.declaration?.id?.name === 'handleCreateNote'
).declaration;
const { code } = await transformWithEsbuild(source.slice(declaration.start, declaration.end), 'new-note.ts', {
loader: 'ts', format: 'esm', target: 'esnext'
});
const createScript = new Script(`${code}\nhandleCreateNote;`);
function deferred() {
let resolve;
let reject;
const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
return { promise, resolve, reject };
}
function fixture(overrides = {}) {
const vault = '/vault';
const entry = { path: '/vault/Untitled.md', relative_path: 'Untitled.md', preview: '',
meta: { id: 'created-id', title: 'Untitled', tags: [], pinned: false,
created: '2026-10-05T10:00:00Z', modified: '2026-10-05T10:00:00Z' } };
const created = { path: entry.path, meta: entry.meta, content: '',
raw: '---\nid: created-id\ntitle: Untitled\ntags: []\npinned: false\ncreated: 2026-10-05T10:00:00Z\nmodified: 2026-10-05T10:00:00Z\n---\n\n' };
const selected = [];
const focused = [];
const errors = [];
const context = createContext({
ownerVault: vault,
ownerIsCurrent: () => context.$appConfig.active_vault === vault,
beginNavigation: beginNoteNavigation,
navigationIsCurrent: token => context.ownerIsCurrent() && isCurrentNoteNavigation(token),
$appConfig: { active_vault: vault }, $viewMode: 'all', $sortMode: 'modified',
$activeNotebook: null, $activeNotePath: null, $activeNote: null, $editorDirty: false, $notes: [],
noteCache: new Map(), onBeforeNoteSwitch: async () => true,
createNote: async (origin, notebook, title) => {
assert.equal(origin, vault);
assert.equal(notebook, null);
assert.equal(title, 'Untitled');
return entry;
},
readNote: async (origin, path) => {
assert.equal(origin, vault);
assert.equal(path, entry.path);
return created;
},
onNoteMoved() {},
onNoteSelected: (path, content) => selected.push({ path, content }),
onNoteCreated: token => focused.push(token),
console: { error: (...args) => errors.push(args) },
...overrides
});
return { context, entry, created, selected, focused, errors, create: createScript.runInContext(context) };
}
for (const mode of ['rich', 'source']) {
test(`the first ${mode} edit saves before any watcher event using the created file's exact baseline`, async () => {
const f = fixture();
const saves = new RevisionSaveQueue();
let saving;
f.context.onNoteSelected = (path, content) => {
assert.equal(path, f.entry.path);
saves.startSession(f.context.$activeNote.raw);
saves.markChanged();
f.context.$editorDirty = true;
saving = saves.save(saves.capture(), async expectedRaw => {
assert.equal(expectedRaw, f.created.raw, 'first save must accept the file created on disk');
return `${expectedRaw}First ${mode} edit\n`;
});
};
await f.create();
assert.equal(await saving, true);
assert.equal(saves.dirty, false);
assert.equal(f.context.$activeNote.raw, f.created.raw);
assert.equal(f.focused.length, 1);
});
}
test('creation loads current disk content and metadata instead of fabricating an empty note', async () => {
const f = fixture();
const current = { ...f.created, meta: { ...f.created.meta, title: 'External title', tags: ['external'] },
content: 'External content', raw: '---\ntitle: External title\n---\nExternal content\n' };
f.context.readNote = async () => current;
await f.create();
assert.equal(f.context.$activeNote, current);
assert.equal(f.selected[0].content, current.content);
});
test('a delayed new-note read cannot replace a newer selected draft or steal title focus', async () => {
const read = deferred();
let reads = 0;
const f = fixture({ readNote: () => { reads++; return read.promise; } });
const creating = f.create();
await new Promise(resolve => setImmediate(resolve));
const newer = { path: '/vault/Other.md', content: 'Newer unsaved draft', raw: 'Other baseline' };
beginNoteNavigation();
f.context.$activeNote = newer;
f.context.$activeNotePath = newer.path;
f.context.$editorDirty = true;
read.resolve(f.created);
await creating;
assert.equal(reads, 1);
assert.equal(f.context.$activeNote, newer);
assert.equal(f.context.$editorDirty, true);
assert.equal(f.selected.length, 0);
assert.equal(f.focused.length, 0);
});
test('a delayed new-note read cannot publish into another vault', async () => {
const read = deferred();
let reads = 0;
const f = fixture({ readNote: () => { reads++; return read.promise; } });
const creating = f.create();
await new Promise(resolve => setImmediate(resolve));
f.context.$appConfig = { active_vault: '/other-vault' };
f.context.$activeNote = null;
f.context.$activeNotePath = null;
read.resolve(f.created);
await creating;
assert.equal(reads, 1);
assert.equal(f.context.$activeNote, null);
assert.equal(f.selected.length, 0);
assert.equal(f.focused.length, 0);
});
test('a failed read keeps the existing draft and never opens a fabricated new note', async () => {
const old = { path: '/vault/Old.md', content: 'Draft', raw: 'Old baseline' };
const f = fixture({ $activeNote: old, $activeNotePath: old.path,
readNote: async () => { throw new Error('Read failed'); } });
await f.create();
assert.equal(f.context.$activeNote, old);
assert.equal(f.selected.length, 0);
assert.equal(f.focused.length, 0);
assert.equal(f.errors.length, 1);
});
test('typing while the new-note read is pending must pass another save flush before selection', async () => {
const read = deferred();
let flushes = 0;
const old = { path: '/vault/Old.md', content: 'New draft', raw: 'Old baseline' };
const f = fixture({ $activeNote: old, $activeNotePath: old.path,
readNote: () => read.promise, onBeforeNoteSwitch: async () => ++flushes === 1 });
const creating = f.create();
await new Promise(resolve => setImmediate(resolve));
f.context.$editorDirty = true;
read.resolve(f.created);
await creating;
assert.equal(flushes, 2);
assert.equal(f.context.$activeNote, old);
assert.equal(f.context.$editorDirty, true);
assert.equal(f.selected.length, 0);
assert.equal(f.focused.length, 0);
});
+25 -1
View File
@@ -19,7 +19,9 @@ const {
isPathInDirectory,
isTrashedNote,
resolvePathFromFile,
resolveVaultFilePath
resolveVaultFilePath,
resolveFileReference,
noteAssetRoot
} = await import(
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
);
@@ -118,6 +120,28 @@ test('resolves vault files consistently across platforms', () => {
);
});
test('document URI suffixes are excluded from filesystem paths without double-decoding filename escapes', () => {
assert.deepEqual(resolveFileReference('.helixnotes/attachments/report%2520%29.pdf#page=2', '/vault/Notes/Note.md', '/vault'), {
path: '/vault/.helixnotes/attachments/report%20).pdf', suffix: '#page=2'
});
assert.deepEqual(resolveFileReference('../files/report%23%3F.pdf?mode=fit#page=2', '/vault/Notes/Note.md', '/vault'), {
path: '/vault/files/report#?.pdf', suffix: '?mode=fit#page=2'
});
});
test('older imported managed links stay inside their vault after moving to another notebook depth', () => {
for (const note of ['/vault/Projects/Nested/Note.md', '/vault/Note.md', '/vault/Archive/Note.md']) {
assert.equal(resolveFileReference('../../.helixnotes/attachments/report.pdf#page=2', note, '/vault').path, '/vault/.helixnotes/attachments/report.pdf');
}
});
test('external source roots use configured ownership without borrowing the active vault', () => {
assert.equal(noteAssetRoot('/source/Notes/Note.md', '/destination', ['/source']), '/source');
assert.equal(noteAssetRoot('/external/Note.md', '/destination', ['/source']), null);
assert.equal(noteAssetRoot('/destination-extra/Note.md', '/destination'), null);
assert.equal(noteAssetRoot('C:\\SOURCE\\Note.md', 'C:\\Destination', ['c:/Source']), 'c:/Source');
});
test('restored Windows and UNC notes remain vault-owned across separator and case differences', () => {
assert.equal(isPathInDirectory('C:\\NOTES\\Alpha.md', 'c:\\Notes\\'), true);
assert.equal(isPathInDirectory('C:/Notes/Folder/Alpha.md', 'C:\\Notes'), true);
+250
View File
@@ -0,0 +1,250 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { ChangeSet, EditorState } from '@codemirror/state';
import { Schema } from '@tiptap/pm/model';
import { Mapping, StepMap, Transform } from '@tiptap/pm/transform';
import {
activeHeadingIndex,
documentHeadings,
mapDocumentHeadings,
mapSourceHeadings,
sourceHeadings,
} from '../src/lib/editor/outline.ts';
const schema = new Schema({
nodes: {
doc: { content: 'block+' },
paragraph: { group: 'block', content: 'inline*' },
heading: { group: 'block', content: 'inline*', attrs: { level: { default: 1 } } },
blockquote: { group: 'block', content: 'block+' },
bullet_list: { group: 'block', content: 'list_item+' },
list_item: { content: 'block+' },
code_block: { group: 'block', content: 'text*', code: true },
text: { group: 'inline' },
},
marks: { strong: {}, em: {} },
});
const paragraph = (text) => schema.node('paragraph', null, text ? schema.text(text) : null);
const heading = (text, level = 1) => schema.node('heading', { level }, text ? schema.text(text) : null);
const stateFor = (source) => EditorState.create({ doc: source });
const extract = (source) => sourceHeadings(stateFor(source));
const sourceEntry = (source, raw, level, text, offset = source.indexOf(raw)) => ({
level, text, pos: offset, end: offset + raw.length,
});
test('document headings are semantic descendants with exact node extents and plain marked text', () => {
const intro = paragraph('lead');
const formatted = schema.node('heading', { level: 2 }, [
schema.text('日本語😀', [schema.mark('strong')]), schema.text(' same', [schema.mark('em')]),
]);
const quotedHeading = heading('same', 3);
const quote = schema.node('blockquote', null, quotedHeading);
const listedHeading = heading('same', 6);
const list = schema.node('bullet_list', null, schema.node('list_item', null, [paragraph('item'), listedHeading]));
const empty = heading('', 1);
const code = schema.node('code_block', null, schema.text('# not a heading'));
const doc = schema.node('doc', null, [intro, formatted, quote, list, empty, code, paragraph('# also not a heading')]);
const formattedPos = intro.nodeSize;
const quotePos = formattedPos + formatted.nodeSize;
const listPos = quotePos + quote.nodeSize;
const listedPos = listPos + 2 + paragraph('item').nodeSize;
const emptyPos = listPos + list.nodeSize;
assert.deepEqual(documentHeadings(doc), [
{ level: 2, text: '日本語😀 same', pos: formattedPos, end: formattedPos + formatted.nodeSize },
{ level: 3, text: 'same', pos: quotePos + 1, end: quotePos + 1 + quotedHeading.nodeSize },
{ level: 6, text: 'same', pos: listedPos, end: listedPos + listedHeading.nodeSize },
{ level: 1, text: '', pos: emptyPos, end: emptyPos + empty.nodeSize },
]);
});
test('source ATX levels use UTF-16 offsets and full heading extents', () => {
const source = 'intro 😀\n\n' + [1, 2, 3, 4, 5, 6].map((level) => `${'#'.repeat(level)} 日本語😀 ${level}`).join('\n');
assert.deepEqual(extract(source), [1, 2, 3, 4, 5, 6].map((level) => {
const raw = `${'#'.repeat(level)} 日本語😀 ${level}`;
return sourceEntry(source, raw, level, `日本語😀 ${level}`);
}));
});
test('source labels render inline formatting, entities, links, image alt text and escaped markers', () => {
const source = '# **Bold** *日本語😀* [Link](https://example.test) ![Alt *text*](image.png) `a_*b` &amp; \\*literal\\* <em>HTML</em> ==mark== H~2~ x^2^ ###';
assert.deepEqual(extract(source), [{
level: 1, text: 'Bold 日本語😀 Link Alt text a_*b & *literal* HTML mark H2 x2', pos: 0, end: source.length,
}]);
});
test('reference links and images use their visible labels rather than Markdown syntax', () => {
const source = '# [Reference][ref] and ![Image][img]\n\n[ref]: https://example.org\n[img]: image.png\n';
assert.equal(extract(source)[0].text, 'Reference and Image');
});
test('empty labels and duplicate labels remain distinct navigable headings', () => {
const source = '#\n## \n### ###\n# same\n# same';
const raws = ['#', '## ', '### ###', '# same', '# same'];
let pos = 0;
const expected = raws.map((raw, index) => {
const entry = sourceEntry(source, raw, [1, 2, 3, 1, 1][index], index < 3 ? '' : 'same', pos);
pos += raw.length + 1;
return entry;
});
assert.deepEqual(extract(source), expected);
});
test('fenced and indented code, escaped markers, and invalid ATX markers are excluded', () => {
const source = [
'# Real', '', '```md', '# fenced', 'Setext', '------', '```', '',
'~~~', '## tilde fenced', '~~~', '', ' # indented', ' underline', ' =====', '',
'\\# escaped', '', '#no-space', '', '####### too many', '',
'> ```', '> # quoted code', '> ```', '', '## Last',
].join('\n');
assert.deepEqual(extract(source), [
sourceEntry(source, '# Real', 1, 'Real'), sourceEntry(source, '## Last', 2, 'Last'),
]);
});
test('closed display math never creates phantom ATX or Setext headings and preserves following offsets', () => {
const source = '$$\nx\n---\n# math heading\n$$\n\n# Real\n\n$$\n## More math\n$$\n\n## Last';
assert.deepEqual(extract(source), [
sourceEntry(source, '# Real', 1, 'Real'), sourceEntry(source, '## Last', 2, 'Last'),
]);
});
test('math delimiters in fenced code and unclosed display math do not hide real Markdown headings', () => {
const source = '```tex\n$$\n# code\n```\n\n# Real\n\n$$\n# Unclosed';
assert.deepEqual(extract(source), [
sourceEntry(source, '# Real', 1, 'Real'), sourceEntry(source, '# Unclosed', 1, 'Unclosed'),
]);
});
test('Setext headings and headings nested in quotes and lists retain semantic starts', () => {
const source = [
'Title 日本語😀', '====', '', 'Sub *title*', '----', '',
'> ## Quote', '', '- ### Listed', '',
'> Multi **line**', '> continued', '> ====', '',
'- List setext', ' -----',
].join('\n');
assert.deepEqual(extract(source), [
sourceEntry(source, 'Title 日本語😀\n====', 1, 'Title 日本語😀'),
sourceEntry(source, 'Sub *title*\n----', 2, 'Sub title'),
sourceEntry(source, '## Quote', 2, 'Quote'),
sourceEntry(source, '### Listed', 3, 'Listed'),
sourceEntry(source, 'Multi **line**\n> continued\n> ====', 1, 'Multi line continued'),
sourceEntry(source, 'List setext\n -----', 2, 'List setext'),
]);
});
test('source extraction fully parses headings beyond an unconfigured or partial language tree', () => {
const source = 'ordinary text\n\n'.repeat(2000) + '# Far heading';
assert.deepEqual(extract(source), [sourceEntry(source, '# Far heading', 1, 'Far heading')]);
});
test('raw HTML blocks do not create Markdown outline entries or expose heading-looking text', () => {
const source = '<h1>HTML title</h1>\n\n<div>\n# HTML block content\n</div>\n\n# Markdown';
assert.deepEqual(extract(source), [sourceEntry(source, '# Markdown', 1, 'Markdown')]);
});
test('active heading uses the latest start, including boundaries and body content', () => {
const headings = [
{ level: 1, text: 'same', pos: 5, end: 10 },
{ level: 2, text: 'same', pos: 20, end: 25 },
{ level: 1, text: '', pos: 35, end: 36 },
];
for (const [pos, index] of [[0, -1], [4, -1], [5, 0], [10, 0], [19, 0], [20, 1], [34, 1], [35, 2], [100, 2]]) {
assert.equal(activeHeadingIndex(headings, pos), index);
}
assert.equal(activeHeadingIndex([], 0), -1);
assert.equal(activeHeadingIndex([{ ...headings[0], pos: 0 }], 0), 0);
});
test('source anchors map insertions before/at the heading start without absorbing end insertions', () => {
const state = stateFor('lead\n# One\n\n## Two');
const original = sourceHeadings(state);
const before = state.update({ changes: { from: original[0].pos, insert: 'prefix\n' } });
const shifted = mapSourceHeadings(original, before.changes.desc);
assert.deepEqual(shifted, sourceHeadings(before.state));
assert.deepEqual(original, sourceHeadings(state));
const atEnd = before.state.update({ changes: { from: shifted[0].end, insert: '\nnew body' } });
assert.deepEqual(mapSourceHeadings(shifted, atEnd.changes.desc), sourceHeadings(atEnd.state));
const inLabel = atEnd.state.update({ changes: { from: shifted[0].pos + 2, insert: 'New ' } });
const mapped = mapSourceHeadings(mapSourceHeadings(shifted, atEnd.changes.desc), inLabel.changes.desc);
assert.equal(mapped[0].pos, sourceHeadings(inLabel.state)[0].pos);
assert.equal(mapped[0].end, sourceHeadings(inLabel.state)[0].end);
assert.equal(mapped[0].text, 'One'); // Labels refresh with the debounced extraction.
});
test('source anchors discard deletion/replacement instead of jumping to a duplicate or unrelated heading', () => {
const state = stateFor('# same\n\n# same\n\n# last');
const original = sourceHeadings(state);
for (const insert of ['', '# unrelated']) {
const transaction = state.update({ changes: { from: original[0].pos, to: original[0].end, insert } });
const mapped = mapSourceHeadings(original, transaction.changes.desc);
assert.deepEqual(mapped, sourceHeadings(transaction.state).slice(insert ? 1 : 0));
}
const markerDeleted = state.update({ changes: { from: 0, to: 1 } });
assert.deepEqual(mapSourceHeadings(original, markerDeleted.changes.desc), sourceHeadings(markerDeleted.state));
const covering = state.update({ changes: { from: 0, to: original[1].end, insert: '# replacement' } });
assert.deepEqual(mapSourceHeadings(original, covering.changes.desc), sourceHeadings(covering.state).slice(1));
});
test('source mapping handles simultaneous and composed edits while preserving surviving anchors', () => {
const state = stateFor('# first\n\n# second\n\n# third');
const original = sourceHeadings(state);
const first = state.update({ changes: [
{ from: 0, insert: 'intro\n\n' },
{ from: original[1].pos, to: original[1].end, insert: 'body' },
] });
const second = first.state.update({ changes: { from: 0, insert: '😀\n' } });
const composed = first.changes.desc.composeDesc(second.changes.desc);
assert.deepEqual(mapSourceHeadings(original, composed), sourceHeadings(second.state));
assert.deepEqual(mapSourceHeadings(original, ChangeSet.empty(state.doc.length)), original);
});
test('document anchors map transactions before debounce and exclude insertions at the end', () => {
const doc = schema.node('doc', null, [paragraph('lead'), heading('one'), heading('two', 2)]);
const original = documentHeadings(doc);
const transform = new Transform(doc).insert(original[0].pos, paragraph('intro'));
let mapped = mapDocumentHeadings(original, transform.mapping);
assert.deepEqual(mapped, documentHeadings(transform.doc));
const inserted = new Transform(transform.doc).insert(mapped[0].end, paragraph('body'));
mapped = mapDocumentHeadings(mapped, inserted.mapping);
assert.deepEqual(mapped, documentHeadings(inserted.doc));
const edited = new Transform(inserted.doc).insert(mapped[0].pos + 1, schema.text('new '));
const editedMapped = mapDocumentHeadings(mapped, edited.mapping);
assert.equal(editedMapped[0].pos, documentHeadings(edited.doc)[0].pos);
assert.equal(editedMapped[0].end, documentHeadings(edited.doc)[0].end);
assert.deepEqual(original, documentHeadings(doc));
assert.deepEqual(mapDocumentHeadings(original, new Mapping()), original);
});
test('document deletion and replacement never reuse an obsolete heading opening token', () => {
const doc = schema.node('doc', null, [heading('same'), heading('same'), heading('last')]);
const original = documentHeadings(doc);
for (const replacement of [null, paragraph('body'), heading('unrelated')]) {
const transform = new Transform(doc);
if (replacement) transform.replaceWith(original[0].pos, original[0].end, replacement);
else transform.delete(original[0].pos, original[0].end);
const fresh = documentHeadings(transform.doc);
assert.deepEqual(mapDocumentHeadings(original, transform.mapping), replacement?.type.name === 'heading' ? fresh.slice(1) : fresh);
}
const transform = new Transform(doc).insert(0, paragraph('intro'));
const shifted = documentHeadings(transform.doc);
transform.replaceWith(shifted[1].pos, shifted[1].end, paragraph('body'));
assert.deepEqual(mapDocumentHeadings(original, transform.mapping), documentHeadings(transform.doc));
});
test('document mappings respect their selected step range', () => {
const headings = [{ level: 1, text: 'one', pos: 5, end: 10 }];
const mapping = new Mapping([new StepMap([0, 0, 3]), new StepMap([0, 0, 2])]);
assert.deepEqual(mapDocumentHeadings(headings, mapping.slice(1)), [{ ...headings[0], pos: 7, end: 12 }]);
});
test('math delimiters in tilde fences cannot mask later real headings', () => {
const source = '~~~tex\n$$\n$x$\n~~~\n\n# Real\n\n$$\nunclosed';
assert.deepEqual(extract(source), [sourceEntry(source, '# Real', 1, 'Real')]);
});
test('masking display math retains list and quote context for following headings', () => {
const source = '- Outer\n - $$\n x\n ---\n $$\n\n ## Nested\n\n> $$\n> y\n> ---\n> $$\n> ## Quoted';
assert.deepEqual(extract(source), [
sourceEntry(source, '## Nested', 2, 'Nested'), sourceEntry(source, '## Quoted', 2, 'Quoted'),
]);
});
+109
View File
@@ -0,0 +1,109 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { createContext, Script } from 'node:vm';
import { parse } from 'svelte/compiler';
import { transformWithEsbuild } from 'vite';
const source = await readFile(new URL('../src/lib/components/Editor.svelte', import.meta.url), 'utf8');
const declaration = parse(source, { modern: true }).instance.content.body.find(node =>
node.type === 'FunctionDeclaration' && node.id.name === 'viewerImportTo'
);
const { code } = await transformWithEsbuild(source.slice(declaration.start, declaration.end), 'viewer-import.ts', {
loader: 'ts', format: 'esm', target: 'esnext'
});
const importScript = new Script(`${code}\nviewerImportTo;`);
function fixture(path = '/external/External.md') {
const original = { path, meta: { title: 'External' },
content: '![Image](images/photo.png)', raw: '---\ncustom: keep\n---\n![Image](images/photo.png)' };
const imported = { ...original, path: '/vault/External.md',
content: '![Image](.helixnotes/attachments/unique-photo.png)',
raw: '---\ncustom: keep\n---\n![Image](.helixnotes/attachments/unique-photo.png)' };
let current = true;
const calls = [];
const loaded = [];
const messages = [];
const files = new Map([[path, original]]);
const context = createContext({
beginEditorNoteNavigation: () => () => current,
$viewerNote: { path, content: original.content }, $activeNote: original,
$appConfig: { active_vault: '/vault' }, $readOnly: true, $focusMode: true,
$activeNotePath: path, $editorDirty: false, viewerImportBusy: false, viewerImportPickerOpen: true,
readNote: async (_vault, target) => {
if (!files.has(target)) throw new Error('Not found');
return files.get(target);
},
copyFileTo: async (origin, target) => files.set(target, { ...files.get(origin), path: target }),
externalNoteReferences: () => [{ from: 24, to: 40, href: 'images/photo.png', html: false }],
importNote: async (...args) => { calls.push(args); files.set(imported.path, imported); return imported; },
loadNote: (target, content) => loaded.push({ target, content }),
viewerFlash: message => messages.push(message), console: { error() {} }
});
return { context, original, imported, calls, loaded, messages,
cancel: () => { current = false; }, run: importScript.runInContext(context) };
}
test('notebook destinations use portable relative paths rather than duplicating the vault path', async () => {
const ast = parse(source, { modern: true });
const declaration = ast.instance.content.body.flatMap(node => node.declarations ?? []).find(node => node.id.name === 'viewerFlatNotebooks');
const expression = declaration.init.arguments[0];
const transformed = await transformWithEsbuild(source.slice(expression.start, expression.end), 'viewer-notebooks.ts', { loader: 'ts', format: 'esm' });
const list = new Script(`(${transformed.code.replace(/;\s*$/, '')})()`).runInNewContext({
viewerImportPickerOpen: true,
$notebooks: [{ path: 'C:\\Vault\\Projects', relative_path: 'Projects', name: 'Projects', children: [
{ path: 'C:\\Vault\\Projects\\Nested', relative_path: 'Projects\\Nested', name: 'Nested', children: [] }
] }]
});
assert.deepEqual(Array.from(list, entry => entry.path), ['Projects', 'Projects/Nested']);
});
test('Windows viewer imports use the native filename instead of treating a full path as a filename', async () => {
const f = fixture('C:\\Users\\writer\\External.md');
await f.run('');
assert.equal(f.context.$activeNotePath, '/vault/External.md');
assert.equal(f.context.$viewerNote, null);
});
test('viewer import uses the accepted raw note and semantic attachment references in one native operation', async () => {
const f = fixture();
await f.run('Projects');
assert.equal(f.calls.length, 1, 'copying Markdown alone must not bypass attachment import');
const [vault, path, notebook, raw, references] = f.calls[0];
assert.equal(vault, '/vault');
assert.equal(path, f.original.path);
assert.equal(notebook, 'Projects');
assert.equal(raw, f.original.raw);
assert.equal(references[0].href, 'images/photo.png');
assert.equal(f.loaded[0].content, f.imported.content);
assert.equal(f.original.content, '![Image](images/photo.png)');
});
test('failed native import leaves the original read-only viewer selected and allows retry', async () => {
const f = fixture();
f.context.importNote = async () => { throw new Error('Attachment permission denied'); };
await f.run('');
assert.equal(f.context.$activeNote, f.original);
assert.equal(f.context.$viewerNote.path, f.original.path);
assert.equal(f.context.$readOnly, true);
assert.equal(f.context.viewerImportBusy, false);
assert.match(f.messages[0], /Attachment permission denied/);
});
test('an import completion cannot steal selection after the viewer is closed or navigation changes', async () => {
const f = fixture();
let finish;
f.context.importNote = () => new Promise(resolve => { finish = resolve; });
const importing = f.run('');
await new Promise(resolve => setImmediate(resolve));
f.cancel();
const newer = { path: '/vault/Newer.md', content: 'Unsaved draft' };
f.context.$activeNote = newer;
f.context.$activeNotePath = newer.path;
f.context.$viewerNote = null;
if (finish) finish(f.imported);
await importing;
assert.equal(typeof finish, 'function', 'the viewer must delegate to the native import');
assert.equal(f.context.$activeNote, newer);
assert.equal(f.loaded.length, 0);
});