@commte/mdbrowse 0.1.1 → 0.2.0
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 +23 -16
- package/README.md +23 -16
- package/bin/mdbrowse +226 -31
- package/package.json +1 -1
package/README.ja.md
CHANGED
|
@@ -2,22 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
# mdbrowse
|
|
4
4
|
|
|
5
|
-
編集中の Markdown を、自分の CSS
|
|
5
|
+
編集中の Markdown を、自分の CSS でブラウザに表示する。エディタは何でもいい
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
`mdb` を1回実行するとタブが開き、以後はどのプロジェクトのどの Markdown を保存しても、そのタブに出る。設定は要らない。サーバーもポートも無い
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|
|
|
11
11
|
## 何のために作ったか
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
エディタ内蔵のプレビューは、エディタの中で描画されるので見た目に手を入れられない。サーバーを立てるツールなら自由になるが、常駐とポートを引き受けることになる。これは pandoc の上に乗せたシェルスクリプトと、スタイルを収めた HTML が1枚あるだけで、固定パスに書き出してタブが自分で読み直す
|
|
14
14
|
|
|
15
15
|
出力先が常に同じなので、ファイルを切り替えてもタブは増えない。すでに開いているタブの中身が入れ替わる
|
|
16
16
|
|
|
17
17
|
## 必要なもの
|
|
18
18
|
|
|
19
19
|
- [pandoc](https://pandoc.org/) macOS は `brew install pandoc`、Debian/Ubuntu は `apt install pandoc`
|
|
20
|
-
- 現在開いているファイルのパスを渡してシェルコマンドを実行できるエディタ
|
|
21
20
|
|
|
22
21
|
## インストール
|
|
23
22
|
|
|
@@ -52,28 +51,32 @@ cd mdbrowse
|
|
|
52
51
|
|
|
53
52
|
</details>
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
あとは1回実行する
|
|
56
55
|
|
|
57
56
|
```sh
|
|
58
|
-
mdb
|
|
57
|
+
mdb
|
|
59
58
|
```
|
|
60
59
|
|
|
61
|
-
|
|
60
|
+
プレビュー用のタブが開き、小さな背景プロセスが動きだす。以後どの Markdown を保存しても、どのプロジェクトのものでも、そのタブに出る。止めるときは `mdb --stop`、再開はまた `mdb`
|
|
62
61
|
|
|
63
62
|
## 使い方
|
|
64
63
|
|
|
65
64
|
```sh
|
|
66
|
-
mdb
|
|
67
|
-
mdb --
|
|
68
|
-
mdb --
|
|
65
|
+
mdb # タブを開いて、以後ずっと追従させる
|
|
66
|
+
mdb --stop # 背景の追従を止める
|
|
67
|
+
mdb --status # いま何が出ているかを表示する
|
|
68
|
+
mdb file.md # そのファイルを1回だけ変換する
|
|
69
|
+
mdb -w file.md # 1つのファイルだけを前面で監視する(Ctrl-C で終了)
|
|
70
|
+
mdb --path # 出力先の HTML のパスを表示する
|
|
69
71
|
```
|
|
70
72
|
|
|
71
|
-
|
|
73
|
+
### どうやって保存したファイルを見つけているか
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
Spotlight(`mdfind`)に「直近に保存された Markdown」を尋ねて、それを変換している。ディスクを歩き回らないので軽い。隠しディレクトリ、`node_modules`、`~/Library` の下は無視する。Spotlight が使えない環境では、`mdb` を実行したディレクトリを見る動きに落ちる
|
|
76
|
+
|
|
77
|
+
いま開いているファイルは毎秒そのまま見ているので、続けて保存したぶんはすぐ出る。別のファイルに移ったときだけ、Spotlight が気づくまで数秒かかる
|
|
78
|
+
|
|
79
|
+
最後に保存されたものを追うので、別のプログラムが Markdown を書くとプレビューが持っていかれる。1つのファイルに固定したいときは `mdb -w そのファイル.md` を使う。指定したものだけを見て、`Ctrl-C` で止まる
|
|
77
80
|
|
|
78
81
|
## ページ内の操作
|
|
79
82
|
|
|
@@ -85,6 +88,8 @@ mdb --path # 出力先の HTML のパスを表示する
|
|
|
85
88
|
|
|
86
89
|
## エディタの設定
|
|
87
90
|
|
|
91
|
+
`mdb` が保存を追いかけるので、以下は要らない。背景で何も動かさず、キーを押して出したい場合はこちらを設定する。どれも1回だけ変換する `mdb <file>` を呼んでいる
|
|
92
|
+
|
|
88
93
|
### Zed
|
|
89
94
|
|
|
90
95
|
`install.sh` が `~/.config/zed/tasks.json` を作る。既存のファイルがある場合は上書きせず、貼り付ける内容を表示する。あとは `~/.config/zed/keymap.json` にキーを追加する
|
|
@@ -169,7 +174,7 @@ mdb sample.md
|
|
|
169
174
|
## 仕組み
|
|
170
175
|
|
|
171
176
|
```
|
|
172
|
-
|
|
177
|
+
ファイルを保存 → mdb(またはエディタのショートカット) → pandoc → /tmp/mdbrowse.html
|
|
173
178
|
→ /tmp/mdbrowse-stamp.js
|
|
174
179
|
↑
|
|
175
180
|
ブラウザがスタンプを見て、変化したときだけ再読み込み
|
|
@@ -181,6 +186,8 @@ YAML の frontmatter は本文に出さない。ファイル側の `title` が
|
|
|
181
186
|
|
|
182
187
|
元ファイルからの相対パス(画像や隣のファイルへのリンク)は、変換時に絶対 `file://` へ書き換える。HTML が `/tmp` にあっても画像が表示されるのはこのため。書き換えるのは `img` や `a` などタグの属性だけなので、本文に `src="foo.png"` と書いても表示はそのまま。絶対パス、スキーム付きのもの(`http(s)`、`data:`、`mailto:` など)、`//` 始まり、ページ内アンカーには触らない。パスに `&` や `#`、空白が入っていても壊れない
|
|
183
188
|
|
|
189
|
+
監視は同じ変換を繰り返しているだけ。背景プロセスは、開いているファイルの更新時刻とサイズを1秒ごとに、直近に保存された Markdown を2秒ごとに見て、変わったら変換し直す。待ち受けるものは無くポートも持たない。`mdb --stop` で終わり、`ps` に見えるシェルのプロセスが1つあるだけ
|
|
190
|
+
|
|
184
191
|
ページは静的なので、待ち受けているものは何も無く、終了させる必要もない
|
|
185
192
|
|
|
186
193
|
## ライセンス
|
package/README.md
CHANGED
|
@@ -2,22 +2,21 @@ English | [日本語](README.ja.md)
|
|
|
2
2
|
|
|
3
3
|
# mdbrowse
|
|
4
4
|
|
|
5
|
-
Preview the Markdown file you are editing in a real browser, with your own CSS, from any editor
|
|
5
|
+
Preview the Markdown file you are editing in a real browser, with your own CSS, from any editor.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Run `mdb` once. It opens a tab, and from then on whatever Markdown file you save — in any project, from any editor — appears in it. No configuration, no server, no port.
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|
|
|
11
11
|
## Why
|
|
12
12
|
|
|
13
|
-
Editors render Markdown previews inside their own window and give you little control over the result. Server-based previewers give you control but ask you to keep a process and a port around. This
|
|
13
|
+
Editors render Markdown previews inside their own window and give you little control over the result. Server-based previewers give you control but ask you to keep a process and a port around. This is a shell script on top of `pandoc` plus one HTML file holding the styles: it renders to a fixed path and the tab reloads itself.
|
|
14
14
|
|
|
15
15
|
Because the output path never changes, switching between files does not open new tabs. The tab you already have simply shows the new file.
|
|
16
16
|
|
|
17
17
|
## Requirements
|
|
18
18
|
|
|
19
19
|
- [pandoc](https://pandoc.org/) — macOS: `brew install pandoc`, Debian/Ubuntu: `apt install pandoc`
|
|
20
|
-
- An editor that can run a shell command with the path of the active file
|
|
21
20
|
|
|
22
21
|
## Install
|
|
23
22
|
|
|
@@ -52,28 +51,32 @@ This installs `mdb` (and the longer `mdbrowse`) into `~/.local/bin`, and the sty
|
|
|
52
51
|
|
|
53
52
|
</details>
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
Then run it once:
|
|
56
55
|
|
|
57
56
|
```sh
|
|
58
|
-
mdb
|
|
57
|
+
mdb
|
|
59
58
|
```
|
|
60
59
|
|
|
61
|
-
|
|
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.
|
|
62
61
|
|
|
63
62
|
## Usage
|
|
64
63
|
|
|
65
64
|
```sh
|
|
66
|
-
mdb
|
|
67
|
-
mdb --
|
|
68
|
-
mdb --
|
|
65
|
+
mdb # open the tab and keep it in sync
|
|
66
|
+
mdb --stop # stop the background sync
|
|
67
|
+
mdb --status # show what is being previewed
|
|
68
|
+
mdb file.md # render one file, once
|
|
69
|
+
mdb -w file.md # follow one file in the foreground (Ctrl-C to stop)
|
|
70
|
+
mdb --path # print the output HTML path
|
|
69
71
|
```
|
|
70
72
|
|
|
71
|
-
|
|
73
|
+
### How the sync finds your file
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
It asks Spotlight (`mdfind`) which Markdown file was saved most recently, twice a second-and-a-bit, and renders that one. Walking your disk is never involved, so it stays cheap. Files under hidden directories, `node_modules` and `~/Library` are ignored. Without Spotlight it falls back to watching the directory you started `mdb` in.
|
|
76
|
+
|
|
77
|
+
The file you are editing right now is checked every second directly, so repeated saves show up immediately; moving to a different file takes a couple of seconds longer, while Spotlight notices it.
|
|
78
|
+
|
|
79
|
+
Because it follows whatever was saved last, another program writing a Markdown file can pull the preview away. If you want the tab pinned to one file, run `mdb -w that-file.md` instead — that watches only what you name, and stops when you press `Ctrl-C`.
|
|
77
80
|
|
|
78
81
|
## In-page controls
|
|
79
82
|
|
|
@@ -85,6 +88,8 @@ The table of contents is built from the headings of the current file and follows
|
|
|
85
88
|
|
|
86
89
|
## Editor setup
|
|
87
90
|
|
|
91
|
+
None of this is needed — `mdb` already follows what you save. Set one of these up if you would rather press a key and have nothing running in the background. They all call `mdb <file>`, the render-once mode.
|
|
92
|
+
|
|
88
93
|
### Zed
|
|
89
94
|
|
|
90
95
|
`install.sh` writes `~/.config/zed/tasks.json` for you — it will not overwrite an existing one; it prints the entries to paste instead. Then bind a key in `~/.config/zed/keymap.json`:
|
|
@@ -169,7 +174,7 @@ mdb sample.md
|
|
|
169
174
|
## How it works
|
|
170
175
|
|
|
171
176
|
```
|
|
172
|
-
|
|
177
|
+
save a file → mdb (or an editor shortcut) → pandoc → /tmp/mdbrowse.html
|
|
173
178
|
→ /tmp/mdbrowse-stamp.js
|
|
174
179
|
↑
|
|
175
180
|
browser polls the stamp, reloads only on change
|
|
@@ -179,6 +184,8 @@ YAML front matter is consumed rather than printed: the file's own `title` does n
|
|
|
179
184
|
|
|
180
185
|
Each render also writes a one-line stamp file. The page polls that stamp instead of reloading blindly, so it refreshes only when you actually preview something new — no periodic flicker on pages with images. Polling pauses while you scroll, while you print, and while the pointer is on the bar. Image dimensions are remembered per session, so a refresh does not shift the layout while images load.
|
|
181
186
|
|
|
187
|
+
Watching is the same render in a loop. The background process compares the file's timestamp and size once a second, asks Spotlight for the newest Markdown file every other second, and renders again when either changes. It listens on nothing and has no port; `mdb --stop` ends it, and it is a single shell process you can see in `ps`.
|
|
188
|
+
|
|
182
189
|
The page is static, so nothing is listening and nothing needs to be shut down.
|
|
183
190
|
|
|
184
191
|
## License
|
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.
|
|
6
|
+
VERSION="0.2.0"
|
|
7
7
|
PROG="$(basename "$0")"
|
|
8
8
|
OUT="${MDBROWSE_OUT:-/tmp/mdbrowse.html}"
|
|
9
9
|
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/mdbrowse"
|
|
@@ -36,11 +36,22 @@ usage() {
|
|
|
36
36
|
mdb — render Markdown to a fixed HTML file for browser preview
|
|
37
37
|
|
|
38
38
|
Usage:
|
|
39
|
-
mdb
|
|
40
|
-
mdb
|
|
41
|
-
mdb --
|
|
42
|
-
mdb --
|
|
43
|
-
mdb
|
|
39
|
+
mdb open the preview tab and keep it in sync from then on
|
|
40
|
+
mdb <file.md> render that file once
|
|
41
|
+
mdb --stop stop the background sync
|
|
42
|
+
mdb --status show what is being previewed
|
|
43
|
+
mdb -w <file|dir> watch in the foreground instead (Ctrl-C to stop)
|
|
44
|
+
mdb --eject copy the bundled stylesheet into your config directory
|
|
45
|
+
mdb --path print the output HTML path
|
|
46
|
+
mdb --version print the version
|
|
47
|
+
|
|
48
|
+
With no arguments, mdb opens the preview tab and starts a small background
|
|
49
|
+
process. From then on, whatever Markdown file you save — in any project, from
|
|
50
|
+
any editor — shows up in that tab. Nothing else to set up. `mdb --stop` ends it.
|
|
51
|
+
|
|
52
|
+
Finding the file you just saved uses Spotlight (mdfind). Files under hidden
|
|
53
|
+
directories, node_modules and ~/Library are ignored. Without Spotlight, mdb
|
|
54
|
+
falls back to watching the directory you started it in.
|
|
44
55
|
|
|
45
56
|
The same command is also installed under its full name, `mdbrowse`.
|
|
46
57
|
|
|
@@ -61,10 +72,57 @@ open_url() {
|
|
|
61
72
|
fi
|
|
62
73
|
}
|
|
63
74
|
|
|
75
|
+
open_tab() {
|
|
76
|
+
[ -f "$OUT" ] || printf '<!doctype html><meta charset="utf-8"><p>No preview yet.' > "$OUT"
|
|
77
|
+
[ -f "${OUT%.html}-stamp.js" ] || printf 'window.__mdbrowseStamp="0";\n' > "${OUT%.html}-stamp.js"
|
|
78
|
+
open_url
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
PIDFILE="${OUT%.html}-sync.pid"
|
|
82
|
+
|
|
83
|
+
running_pid() {
|
|
84
|
+
[ -f "$PIDFILE" ] || return 1
|
|
85
|
+
local pid; pid="$(cat "$PIDFILE" 2>/dev/null)"
|
|
86
|
+
[ -n "$pid" ] || return 1
|
|
87
|
+
kill -0 "$pid" 2>/dev/null || return 1
|
|
88
|
+
echo "$pid"
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
stop_sync() {
|
|
92
|
+
local pid
|
|
93
|
+
if pid="$(running_pid)"; then
|
|
94
|
+
kill "$pid" 2>/dev/null || true
|
|
95
|
+
rm -f "$PIDFILE"
|
|
96
|
+
echo "$PROG: stopped"
|
|
97
|
+
else
|
|
98
|
+
rm -f "$PIDFILE"
|
|
99
|
+
echo "$PROG: not running"
|
|
100
|
+
fi
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
start_sync() {
|
|
104
|
+
if running_pid >/dev/null; then
|
|
105
|
+
echo "$PROG: already in sync (mdb --stop to end it)"
|
|
106
|
+
return 0
|
|
107
|
+
fi
|
|
108
|
+
nohup "$0" --sync-daemon "$PWD" >/dev/null 2>&1 &
|
|
109
|
+
echo $! > "$PIDFILE"
|
|
110
|
+
echo "$PROG: in sync — save any .md and it shows up here (mdb --stop to end it)"
|
|
111
|
+
}
|
|
112
|
+
|
|
64
113
|
case "${1:-}" in
|
|
65
114
|
-h|--help) usage; exit 0 ;;
|
|
66
115
|
-v|--version) echo "mdbrowse $VERSION"; exit 0 ;;
|
|
67
116
|
--path) echo "$OUT"; exit 0 ;;
|
|
117
|
+
--stop) stop_sync; exit 0 ;;
|
|
118
|
+
--status)
|
|
119
|
+
if running_pid >/dev/null; then
|
|
120
|
+
echo "$PROG: in sync (pid $(running_pid))"
|
|
121
|
+
else
|
|
122
|
+
echo "$PROG: not running"
|
|
123
|
+
fi
|
|
124
|
+
[ -f "$OUT" ] && echo "showing: $(grep -o '<title>[^<]*' "$OUT" 2>/dev/null | sed 's/<title>//')"
|
|
125
|
+
exit 0 ;;
|
|
68
126
|
--eject)
|
|
69
127
|
mkdir -p "$CONFIG_DIR"
|
|
70
128
|
if [ -f "$CONFIG_DIR/head.html" ] && [ "${2:-}" != "--force" ]; then
|
|
@@ -74,20 +132,54 @@ case "${1:-}" in
|
|
|
74
132
|
cp "$BUNDLED_HEAD" "$CONFIG_DIR/head.html"
|
|
75
133
|
echo "$CONFIG_DIR/head.html"
|
|
76
134
|
exit 0 ;;
|
|
77
|
-
--open)
|
|
78
|
-
[ -f "$OUT" ] || printf '<!doctype html><meta charset="utf-8"><p>No preview yet.' > "$OUT"
|
|
79
|
-
[ -f "${OUT%.html}-stamp.js" ] || printf 'window.__mdbrowseStamp="0";\n' > "${OUT%.html}-stamp.js"
|
|
80
|
-
open_url; exit 0 ;;
|
|
81
|
-
"") usage; exit 2 ;;
|
|
82
135
|
esac
|
|
83
136
|
|
|
84
|
-
|
|
137
|
+
# 残りは「オプション+ファイル」
|
|
138
|
+
watch=0
|
|
139
|
+
do_open=0
|
|
140
|
+
daemon=0
|
|
141
|
+
sync_cwd=""
|
|
142
|
+
src=""
|
|
143
|
+
while [ $# -gt 0 ]; do
|
|
144
|
+
case "$1" in
|
|
145
|
+
-w|--watch) watch=1 ;;
|
|
146
|
+
-o|--open) do_open=1 ;;
|
|
147
|
+
--sync-daemon) daemon=1; shift; sync_cwd="${1:-$PWD}" ;;
|
|
148
|
+
--) shift; [ $# -gt 0 ] && src="$1"; break ;;
|
|
149
|
+
-*) echo "$PROG: unknown option: $1" >&2; exit 2 ;;
|
|
150
|
+
*) src="$1" ;;
|
|
151
|
+
esac
|
|
152
|
+
shift
|
|
153
|
+
done
|
|
154
|
+
|
|
155
|
+
# 引数なし(または --open だけ)が既定の使い方。タブを開いて、以後は勝手に追従する
|
|
156
|
+
if [ -z "$src" ] && [ "$watch" -eq 0 ] && [ "$daemon" -eq 0 ]; then
|
|
157
|
+
if ! command -v pandoc >/dev/null 2>&1; then
|
|
158
|
+
echo "$PROG: pandoc not found. Install it first (macOS: brew install pandoc)" >&2
|
|
159
|
+
exit 127
|
|
160
|
+
fi
|
|
161
|
+
open_tab
|
|
162
|
+
start_sync
|
|
163
|
+
exit 0
|
|
164
|
+
fi
|
|
165
|
+
|
|
166
|
+
# -w だけならカレントディレクトリを見る
|
|
167
|
+
[ -z "$src" ] && [ "$watch" -eq 1 ] && src="."
|
|
85
168
|
|
|
86
169
|
if ! command -v pandoc >/dev/null 2>&1; then
|
|
87
170
|
echo "$PROG: pandoc not found. Install it first (macOS: brew install pandoc)" >&2
|
|
88
171
|
exit 127
|
|
89
172
|
fi
|
|
90
|
-
|
|
173
|
+
root=""
|
|
174
|
+
if [ "$daemon" -eq 1 ]; then
|
|
175
|
+
:
|
|
176
|
+
elif [ -d "$src" ]; then
|
|
177
|
+
if [ "$watch" -eq 0 ]; then
|
|
178
|
+
echo "$PROG: $src is a directory (pass -w to watch it)" >&2
|
|
179
|
+
exit 2
|
|
180
|
+
fi
|
|
181
|
+
root="$src"
|
|
182
|
+
elif [ ! -f "$src" ]; then
|
|
91
183
|
echo "$PROG: no such file: $src" >&2
|
|
92
184
|
exit 66
|
|
93
185
|
fi
|
|
@@ -108,26 +200,27 @@ else
|
|
|
108
200
|
if [ "$use_shiki" -eq 1 ]; then hl=(--no-highlight); else hl=(--highlight-style breezedark); fi
|
|
109
201
|
fi
|
|
110
202
|
|
|
111
|
-
|
|
112
|
-
|
|
203
|
+
render() {
|
|
204
|
+
local args=(--standalone --from gfm "${hl[@]}" --variable "pagetitle=$(basename "$src")" --output "$OUT")
|
|
205
|
+
[ -f "$HEAD" ] && args+=(--include-in-header "$HEAD")
|
|
113
206
|
|
|
114
|
-
pandoc "$src" "${args[@]}"
|
|
207
|
+
pandoc "$src" "${args[@]}"
|
|
115
208
|
|
|
116
|
-
# コードブロックを Shiki で色付けし直す。失敗しても変換自体は成立させる
|
|
117
|
-
if [ "$use_shiki" -eq 1 ]; then
|
|
118
|
-
|
|
119
|
-
fi
|
|
209
|
+
# コードブロックを Shiki で色付けし直す。失敗しても変換自体は成立させる
|
|
210
|
+
if [ "$use_shiki" -eq 1 ]; then
|
|
211
|
+
node "$HIGHLIGHTER" "$OUT" || true
|
|
212
|
+
fi
|
|
120
213
|
|
|
121
|
-
# frontmatter の title を pandoc が拾うと本文の先頭に見出しが増えるので落とす。
|
|
122
|
-
# ブラウザのタブ名には pagetitle を使っている
|
|
123
|
-
perl -0pi -e 's#<header id="title-block-header">.*?</header>\n?##s' "$OUT"
|
|
214
|
+
# frontmatter の title を pandoc が拾うと本文の先頭に見出しが増えるので落とす。
|
|
215
|
+
# ブラウザのタブ名には pagetitle を使っている
|
|
216
|
+
perl -0pi -e 's#<header id="title-block-header">.*?</header>\n?##s' "$OUT"
|
|
124
217
|
|
|
125
|
-
# 出力先が /tmp なので、元ファイルからの相対パス(画像・ローカルリンク)が
|
|
126
|
-
# そのままでは解決できない。ソースのあるディレクトリ基準の file:// に書き換える。
|
|
127
|
-
# 書き換えるのはタグの属性だけ。本文中の src="..." のようなインラインコードは触らない。
|
|
128
|
-
# スキーム付き(http(s) data: mailto: など)/絶対パス/ページ内アンカーはそのまま残す
|
|
129
|
-
src_dir="$(cd "$(dirname "$src")" && pwd)"
|
|
130
|
-
MDBROWSE_SRC_DIR="$src_dir" perl -0pi -e '
|
|
218
|
+
# 出力先が /tmp なので、元ファイルからの相対パス(画像・ローカルリンク)が
|
|
219
|
+
# そのままでは解決できない。ソースのあるディレクトリ基準の file:// に書き換える。
|
|
220
|
+
# 書き換えるのはタグの属性だけ。本文中の src="..." のようなインラインコードは触らない。
|
|
221
|
+
# スキーム付き(http(s) data: mailto: など)/絶対パス/ページ内アンカーはそのまま残す
|
|
222
|
+
local src_dir="$(cd "$(dirname "$src")" && pwd)"
|
|
223
|
+
MDBROWSE_SRC_DIR="$src_dir" perl -0pi -e '
|
|
131
224
|
BEGIN {
|
|
132
225
|
$base = $ENV{MDBROWSE_SRC_DIR};
|
|
133
226
|
# file:// の中で意味を持つ文字は先に逃がす(% は必ず最初)
|
|
@@ -144,5 +237,107 @@ MDBROWSE_SRC_DIR="$src_dir" perl -0pi -e '
|
|
|
144
237
|
}gse;
|
|
145
238
|
' "$OUT"
|
|
146
239
|
|
|
147
|
-
# 更新の目印。ブラウザはこれを見て、中身が変わったときだけ読み直す
|
|
148
|
-
printf 'window.__mdbrowseStamp="%s";\n' "$(date +%s)-$RANDOM" > "${OUT%.html}-stamp.js"
|
|
240
|
+
# 更新の目印。ブラウザはこれを見て、中身が変わったときだけ読み直す
|
|
241
|
+
printf 'window.__mdbrowseStamp="%s";\n' "$(date +%s)-$RANDOM" > "${OUT%.html}-stamp.js"
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
# ファイルの更新時刻とサイズ。同じ秒内の2回目の保存も拾えるようにサイズも見る
|
|
245
|
+
if stat -f %m . >/dev/null 2>&1; then
|
|
246
|
+
fingerprint() { stat -f '%m %z' "$1" 2>/dev/null; }
|
|
247
|
+
stat_time_name() { xargs -0 stat -f '%m %N' 2>/dev/null; }
|
|
248
|
+
else
|
|
249
|
+
fingerprint() { stat -c '%Y %s' "$1" 2>/dev/null; }
|
|
250
|
+
stat_time_name() { xargs -0 stat -c '%Y %n' 2>/dev/null; }
|
|
251
|
+
fi
|
|
252
|
+
|
|
253
|
+
# ディレクトリの中でいちばん最近保存された Markdown
|
|
254
|
+
newest_md() {
|
|
255
|
+
find "$1" \( -name .git -o -name node_modules -o -name .venv \) -prune \
|
|
256
|
+
-o -type f -name '*.md' -print0 2>/dev/null \
|
|
257
|
+
| stat_time_name | sort -rn | head -1 | cut -d' ' -f2-
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
# Spotlight で「家じゅうで直近に保存された Markdown」を引く。木を歩かないので速い
|
|
261
|
+
newest_global() {
|
|
262
|
+
mdfind 'kMDItemFSName == "*.md"c && kMDItemContentModificationDate >= $time.now(-1800)' 2>/dev/null \
|
|
263
|
+
| grep -v -e '/\.' -e '/node_modules/' -e '/Library/' \
|
|
264
|
+
| tr '\n' '\0' | stat_time_name | sort -rn | head -1 | cut -d' ' -f2-
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
# 背景で動く本体。保存された Markdown を追いかけて変換し続ける
|
|
268
|
+
if [ "$daemon" -eq 1 ]; then
|
|
269
|
+
trap 'rm -f "$PIDFILE"' EXIT
|
|
270
|
+
use_spotlight=0
|
|
271
|
+
command -v mdfind >/dev/null 2>&1 && use_spotlight=1
|
|
272
|
+
[ "$use_spotlight" -eq 0 ] && root="$sync_cwd"
|
|
273
|
+
|
|
274
|
+
pick() {
|
|
275
|
+
if [ "$use_spotlight" -eq 1 ]; then newest_global; else newest_md "$root"; fi
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
src="$(pick)"
|
|
279
|
+
if [ -n "$src" ] && [ -f "$src" ]; then render || true; fi
|
|
280
|
+
last="$src $(fingerprint "$src") $(fingerprint "$HEAD")"
|
|
281
|
+
# 開いているファイルの保存は毎秒見る。別ファイルへの切り替えの検出は2秒ごと
|
|
282
|
+
tick=0
|
|
283
|
+
while sleep 1; do
|
|
284
|
+
tick=$(( tick + 1 ))
|
|
285
|
+
if [ $(( tick % 2 )) -eq 0 ]; then
|
|
286
|
+
cand="$(pick)"
|
|
287
|
+
[ -n "$cand" ] && src="$cand"
|
|
288
|
+
fi
|
|
289
|
+
[ -f "$src" ] || continue
|
|
290
|
+
now="$src $(fingerprint "$src") $(fingerprint "$HEAD")"
|
|
291
|
+
[ "$now" = "$last" ] && continue
|
|
292
|
+
last="$now"
|
|
293
|
+
render || true
|
|
294
|
+
done
|
|
295
|
+
exit 0
|
|
296
|
+
fi
|
|
297
|
+
|
|
298
|
+
if [ -n "$root" ]; then
|
|
299
|
+
src="$(newest_md "$root")"
|
|
300
|
+
if [ -z "$src" ]; then
|
|
301
|
+
echo "$PROG: no Markdown file under $root" >&2
|
|
302
|
+
exit 66
|
|
303
|
+
fi
|
|
304
|
+
fi
|
|
305
|
+
|
|
306
|
+
[ "$do_open" -eq 1 ] && open_tab
|
|
307
|
+
|
|
308
|
+
render
|
|
309
|
+
|
|
310
|
+
# 監視。保存のたびに変換し直す。スタイルシートを直したときも作り直す
|
|
311
|
+
if [ "$watch" -eq 1 ]; then
|
|
312
|
+
# ディレクトリを見る場合、木が大きいと走査が重い。1回の走査時間から
|
|
313
|
+
# 待ち時間を決めて、CPU を食い潰さないようにする
|
|
314
|
+
interval=1
|
|
315
|
+
if [ -n "$root" ]; then
|
|
316
|
+
t0="$(perl -MTime::HiRes=time -e 'printf "%d", time*1000')"
|
|
317
|
+
newest_md "$root" >/dev/null
|
|
318
|
+
t1="$(perl -MTime::HiRes=time -e 'printf "%d", time*1000')"
|
|
319
|
+
scan=$(( t1 - t0 ))
|
|
320
|
+
interval=$(( (scan * 5 + 999) / 1000 ))
|
|
321
|
+
[ "$interval" -lt 1 ] && interval=1
|
|
322
|
+
[ "$interval" -gt 10 ] && interval=10
|
|
323
|
+
if [ "$interval" -gt 1 ]; then
|
|
324
|
+
echo "$PROG: watching $root — save any .md under it (checking every ${interval}s; a smaller directory reacts faster) (Ctrl-C to stop)" >&2
|
|
325
|
+
else
|
|
326
|
+
echo "$PROG: watching $root — save any .md under it (Ctrl-C to stop)" >&2
|
|
327
|
+
fi
|
|
328
|
+
else
|
|
329
|
+
echo "$PROG: watching $src (Ctrl-C to stop)" >&2
|
|
330
|
+
fi
|
|
331
|
+
last="$src $(fingerprint "$src") $(fingerprint "$HEAD")"
|
|
332
|
+
while sleep "$interval"; do
|
|
333
|
+
if [ -n "$root" ]; then
|
|
334
|
+
cand="$(newest_md "$root")"
|
|
335
|
+
[ -n "$cand" ] && src="$cand"
|
|
336
|
+
fi
|
|
337
|
+
now="$src $(fingerprint "$src") $(fingerprint "$HEAD")"
|
|
338
|
+
[ "$now" = "$last" ] && continue
|
|
339
|
+
last="$now"
|
|
340
|
+
[ -f "$src" ] || continue
|
|
341
|
+
render || echo "$PROG: render failed" >&2
|
|
342
|
+
done
|
|
343
|
+
fi
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commte/mdbrowse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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",
|