@commte/mdbrowse 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ja.md CHANGED
@@ -59,7 +59,7 @@ mdb
59
59
 
60
60
  プレビュー用のタブが開き、小さな背景プロセスが動きだす。以後どの Markdown を保存しても、どのプロジェクトのものでも、そのタブに出る。止めるときは `mdb --stop`、再開はまた `mdb`
61
61
 
62
- ログアウトや再起動で消える。macOS なら、勝手に戻ってくるようにもできる
62
+ ログアウトや再起動で消えるので、そのままだと次に使うときにもう一度 `mdb` を叩くことになる。macOS なら、勝手に戻ってくるようにもできる
63
63
 
64
64
  ```sh
65
65
  mdb --install-agent
@@ -186,6 +186,14 @@ vim.api.nvim_create_autocmd("BufWritePost", {
186
186
  mdb sample.md
187
187
  ```
188
188
 
189
+ ## テスト
190
+
191
+ ```sh
192
+ npm test
193
+ ```
194
+
195
+ `test/cli.sh` はコマンド側を動かす。変換、相対パスの書き換え、異常系、監視、背景プロセスまで通す。必要なのは pandoc だけ。`test/browser.test.js` は `assets/head.html` からスクリプトを取り出して jsdom で動かし、更新の検知、目次の判定、バーの設定を見る。CI では macOS と Linux の両方で回していて、通らないものは公開されない
196
+
189
197
  ## 仕組み
190
198
 
191
199
  ```
package/README.md CHANGED
@@ -59,7 +59,7 @@ mdb
59
59
 
60
60
  That opens the preview tab and starts a small background process. Save any Markdown file from here on and it shows up in that tab, whichever project it lives in. `mdb --stop` ends it; `mdb` starts it again.
61
61
 
62
- The process is gone after a logout or a reboot. On macOS you can have it come back on its own:
62
+ The process is gone after a logout or a reboot, so otherwise you run `mdb` again next time. On macOS you can have it come back on its own:
63
63
 
64
64
  ```sh
65
65
  mdb --install-agent
@@ -186,6 +186,14 @@ Everything else visual lives in `~/.config/mdbrowse/head.html`: a settings block
186
186
  mdb sample.md
187
187
  ```
188
188
 
189
+ ## Tests
190
+
191
+ ```sh
192
+ npm test
193
+ ```
194
+
195
+ `test/cli.sh` drives the command itself — rendering, path rewriting, exit codes, watching, the background sync — and needs nothing but pandoc. `test/browser.test.js` pulls the script out of `assets/head.html` and runs it under jsdom, covering the reload loop, the table of contents rules and the settings bar. Both run on macOS and Linux in CI, and a release only goes out if they pass.
196
+
189
197
  ## How it works
190
198
 
191
199
  ```
package/assets/head.html CHANGED
@@ -351,6 +351,6 @@
351
351
  if (printing || (hoverable && bar.matches(':hover'))) return;
352
352
  if (Date.now() - lastScroll < 900) return;
353
353
  checkStamp();
354
- }, 1000);
354
+ }, 250);
355
355
  });
356
356
  </script>
package/bin/mdbrowse CHANGED
@@ -3,7 +3,7 @@
3
3
  # browser tab can stay open and just swap its contents.
4
4
  set -euo pipefail
5
5
 
6
- VERSION="0.3.0"
6
+ VERSION="0.3.2"
7
7
  PROG="$(basename "$0")"
8
8
  OUT="${MDBROWSE_OUT:-/tmp/mdbrowse.html}"
9
9
  case "$OUT" in
@@ -141,6 +141,17 @@ stop_sync() {
141
141
  if [ "$stopped" -eq 1 ]; then echo "$PROG: stopped"; else echo "$PROG: not running"; fi
142
142
  }
143
143
 
144
+ # ログイン時の自動起動があることを、初回だけ1行で知らせる。
145
+ # 毎回出すと邪魔になるので、一度出したら記録して黙る
146
+ hint_agent() {
147
+ [ "$(uname -s)" = "Darwin" ] || return 0
148
+ agent_installed && return 0
149
+ [ -f "$RUNTIME_DIR/agent-hint" ] && return 0
150
+ mkdir -p "$RUNTIME_DIR" 2>/dev/null || true
151
+ : > "$RUNTIME_DIR/agent-hint" 2>/dev/null || true
152
+ echo "$PROG: it stops at logout — 'mdb --install-agent' starts it at every login"
153
+ }
154
+
144
155
  start_sync() {
145
156
  if running_pid >/dev/null; then
146
157
  echo "$PROG: already in sync (mdb --stop to end it)"
@@ -156,6 +167,7 @@ start_sync() {
156
167
  done
157
168
  if running_pid >/dev/null; then
158
169
  echo "$PROG: in sync — save any .md and it shows up here (mdb --stop to end it)"
170
+ hint_agent
159
171
  else
160
172
  echo "$PROG: could not start the background sync (see $LOGFILE)" >&2
161
173
  return 1
@@ -459,16 +471,18 @@ if [ "$daemon" -eq 1 ]; then
459
471
  # 変換の失敗は画面に出ないので、ログに残す
460
472
  log() { printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" >> "$LOGFILE"; }
461
473
 
462
- # Spotlight を使えない場合は木を歩く。大きな木では間隔を伸ばす
463
- scan_gap=2
474
+ # 表示中のファイルは 0.25 秒ごとに見る。1周 = 0.25 秒
475
+ TICK=0.25
476
+ # Spotlight への問い合わせは 2 秒ごと(8周に1回)
477
+ scan_gap=8
464
478
  if [ "$use_spotlight" -eq 0 ]; then
465
479
  t0="$(perl -MTime::HiRes=time -e 'printf "%d", time*1000')"
466
480
  newest_md "$root" >/dev/null
467
481
  t1="$(perl -MTime::HiRes=time -e 'printf "%d", time*1000')"
468
- scan_gap=$(( ( (t1 - t0) * 5 + 999) / 1000 ))
469
- [ "$scan_gap" -lt 2 ] && scan_gap=2
470
- [ "$scan_gap" -gt 10 ] && scan_gap=10
471
- log "watching $root every ${scan_gap}s (no Spotlight)"
482
+ scan_gap=$(( ( (t1 - t0) * 5 + 249) / 250 ))
483
+ [ "$scan_gap" -lt 8 ] && scan_gap=8
484
+ [ "$scan_gap" -gt 40 ] && scan_gap=40
485
+ log "watching $root every $(( scan_gap / 4 ))s (no Spotlight)"
472
486
  fi
473
487
 
474
488
  src="$(pick)"
@@ -476,7 +490,7 @@ if [ "$daemon" -eq 1 ]; then
476
490
  last="$src $(fingerprint "$src") $(fingerprint "$HEAD")"
477
491
  # 開いているファイルの保存は毎秒見る。別ファイルへの切り替えの検出は2秒ごと
478
492
  tick=0
479
- while sleep 1; do
493
+ while sleep "$TICK"; do
480
494
  tick=$(( tick + 1 ))
481
495
  if [ $(( tick % scan_gap )) -eq 0 ]; then
482
496
  cand="$(pick)"
@@ -508,7 +522,7 @@ render
508
522
  if [ "$watch" -eq 1 ]; then
509
523
  # ディレクトリを見る場合、木が大きいと走査が重い。1回の走査時間から
510
524
  # 待ち時間を決めて、CPU を食い潰さないようにする
511
- interval=1
525
+ interval=0.25
512
526
  if [ -n "$root" ]; then
513
527
  t0="$(perl -MTime::HiRes=time -e 'printf "%d", time*1000')"
514
528
  newest_md "$root" >/dev/null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commte/mdbrowse",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Preview the Markdown file you are editing in a real browser, with your own CSS, from any editor that can run a shell command. No server, no port.",
5
5
  "bin": {
6
6
  "mdbrowse": "bin/mdbrowse",
@@ -15,6 +15,11 @@
15
15
  "README.ja.md",
16
16
  "LICENSE"
17
17
  ],
18
+ "scripts": {
19
+ "test": "bash test/cli.sh && vitest run",
20
+ "test:cli": "bash test/cli.sh",
21
+ "test:browser": "vitest run"
22
+ },
18
23
  "keywords": [
19
24
  "markdown",
20
25
  "preview",
@@ -43,5 +48,9 @@
43
48
  },
44
49
  "dependencies": {
45
50
  "shiki": "^4.4.3"
51
+ },
52
+ "devDependencies": {
53
+ "jsdom": "^30.0.1",
54
+ "vitest": "^5.0.0"
46
55
  }
47
56
  }