memd-cli 1.4.1 → 1.5.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/main.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // @ts-nocheck
3
3
  import { marked } from 'marked';
4
4
  import { markedTerminal } from 'marked-terminal';
5
- import { renderMermaidAscii } from 'beautiful-mermaid';
5
+ import { renderMermaidASCII } from 'beautiful-mermaid';
6
6
  import { highlight, supportsLanguage, plain } from 'cli-highlight';
7
7
  import chalk from 'chalk';
8
8
  import { program } from 'commander';
@@ -179,15 +179,13 @@ function convertMermaidToAscii(markdown, options = {}) {
179
179
 
180
180
  return markdown.replace(mermaidRegex, function (_, code) {
181
181
  try {
182
- // Pass width option to beautiful-mermaid if provided
183
182
  const mermaidOptions = {};
184
- if (options.width) {
185
- mermaidOptions.maxWidth = options.width;
186
- }
187
183
  if (options.ascii) {
188
184
  mermaidOptions.useAscii = true;
189
185
  }
190
- const asciiArt = renderMermaidAscii(code.trim(), mermaidOptions);
186
+ // Set color mode based on --no-color flag
187
+ mermaidOptions.colorMode = options.color === false ? 'none' : 'none';
188
+ const asciiArt = renderMermaidASCII(code.trim(), mermaidOptions);
191
189
  return '```text\n' + asciiArt + '\n```';
192
190
  } catch (error) {
193
191
  // Warn user about conversion failure (helps with debugging)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memd-cli",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "main": "main.js",
6
6
  "bin": {
@@ -10,7 +10,7 @@
10
10
  "test": "vitest run"
11
11
  },
12
12
  "dependencies": {
13
- "beautiful-mermaid": "^0.1.3",
13
+ "beautiful-mermaid": "^1.1.2",
14
14
  "chalk": "^5.6.2",
15
15
  "cli-highlight": "^2.1.11",
16
16
  "commander": "^14.0.3",
package/test/memd.test.js CHANGED
@@ -22,7 +22,7 @@ async function run(args, { waitFor = null } = {}) {
22
22
  describe('memd CLI', () => {
23
23
  it('--version', async () => {
24
24
  const output = await run(['-v'])
25
- expect(output).toContain('1.3.0')
25
+ expect(output).toContain('1.5.0')
26
26
  })
27
27
 
28
28
  it('--help', async () => {
@@ -100,6 +100,38 @@ describe('memd CLI', () => {
100
100
  expect(output).toContain('nonexistent.md')
101
101
  })
102
102
 
103
+ it('renders test-br.md (<br> tag line breaks)', async () => {
104
+ const output = await run(
105
+ ['--no-pager', '--no-color', '--width', '80', 'test/test-br.md'],
106
+ { waitFor: t => t.includes('All three variants') },
107
+ )
108
+ // Each node should have its label split across two lines
109
+ expect(output).toContain('Line1')
110
+ expect(output).toContain('Line2')
111
+ expect(output).toContain('Hello')
112
+ expect(output).toContain('World')
113
+ expect(output).toContain('Foo')
114
+ expect(output).toContain('Bar')
115
+ // <br> tags should NOT appear in the diagram portion (extract diagram area)
116
+ const diagramStart = output.indexOf('Line1')
117
+ const diagramEnd = output.indexOf('Bar') + 3
118
+ const diagramSection = output.slice(diagramStart, diagramEnd)
119
+ expect(diagramSection).not.toMatch(/<br\s*\/?>/)
120
+ })
121
+
122
+ it('renders test-cjk.md (Japanese labels)', async () => {
123
+ const output = await run(
124
+ ['--no-pager', '--no-color', '--width', '80', 'test/test-cjk.md'],
125
+ { waitFor: t => t.includes('Japanese labels') },
126
+ )
127
+ expect(output).toContain('開始')
128
+ expect(output).toContain('判定')
129
+ expect(output).toContain('実行')
130
+ expect(output).toContain('終了')
131
+ expect(output).toContain('はい')
132
+ expect(output).toContain('いいえ')
133
+ })
134
+
103
135
  it('reads markdown from stdin via shell', async () => {
104
136
  const session = await launchTerminal({
105
137
  command: 'sh',
@@ -0,0 +1,11 @@
1
+ # BR Tag Test
2
+
3
+ Testing `<br>` tag support in mermaid diagrams:
4
+
5
+ ```mermaid
6
+ flowchart TD
7
+ A[Line1<br>Line2] --> B[Hello<br/>World]
8
+ B --> C[Foo<br />Bar]
9
+ ```
10
+
11
+ All three variants should render as line breaks.
@@ -0,0 +1,13 @@
1
+ # CJK Test
2
+
3
+ Testing Japanese text in mermaid diagrams:
4
+
5
+ ```mermaid
6
+ flowchart TD
7
+ A[開始] --> B{判定}
8
+ B -->|はい| C[実行]
9
+ B -->|いいえ| D[終了]
10
+ C --> D
11
+ ```
12
+
13
+ Japanese labels should render correctly.
package/plan.md DELETED
@@ -1,283 +0,0 @@
1
- # memd TUI Outline Sidebar 設計案
2
-
3
- ## 背景
4
-
5
- memd は現在 `less` (または `$PAGER`) をフルスクリーンページャーとして使用している。
6
- Markdown の見出し構造をサイドバーにアウトラインとして表示し、ナビゲーション可能にする機能を追加する。
7
-
8
- ## TUI ライブラリ選定
9
-
10
- | ライブラリ | 判定 | 理由 |
11
- |---|---|---|
12
- | blessed-contrib | 不採用 | 放棄済み (最終更新4年前)、未修正の Prototype Pollution 脆弱性 |
13
- | Ink | 保留 | 最も活発だが React 依存が重い。軽量 CLI には過剰 |
14
- | terminal-kit | 採用 | Node 18/20/22 対応、純 JS、既知脆弱性修正済み、ドキュメント良好 |
15
-
16
- リスク: メンテナ1名、更新頻度低下傾向、79件のオープンイシュー積み残し。
17
- terminal-kit 依存は `lib/tui.js` に閉じ込め、将来の差し替えに備える。
18
-
19
- ## ディレクトリ構成
20
-
21
- ```
22
- memd/
23
- main.js # エントリーポイント (CLI 解析 + モード分岐のみ)
24
- lib/
25
- markdown.js # Markdown 処理 (既存ロジックの抽出)
26
- outline.js # 見出し抽出・アウトラインデータ生成
27
- pager.js # 既存の less/pager ロジック
28
- tui.js # terminal-kit TUI モード (新規)
29
- test/
30
- test1.md
31
- test2.md
32
- package.json
33
- ```
34
-
35
- ## モジュール設計
36
-
37
- ### main.js -- エントリーポイント
38
-
39
- CLI 解析 (commander) とモード分岐のみ。
40
-
41
- ```
42
- CLI 解析
43
- |
44
- +-- --outline あり & TTY --> tui.js (TUI モード)
45
- +-- パイプ / --no-pager / 短い出力 --> stdout 直接出力
46
- +-- 長い出力 --> pager.js (less モード)
47
- ```
48
-
49
- 追加オプション:
50
-
51
- ```js
52
- .option('--outline', 'show outline sidebar (TUI mode)')
53
- ```
54
-
55
- 分岐ロジック:
56
-
57
- ```js
58
- if (options.outline && process.stdout.isTTY) {
59
- launchTui(markdown, renderedText, options);
60
- } else if (shouldUsePager(renderedText, options)) {
61
- spawnPager(renderedText, options);
62
- } else {
63
- process.stdout.write(renderedText);
64
- }
65
- ```
66
-
67
- ### lib/markdown.js -- Markdown 処理
68
-
69
- 既存の `main.js` から抽出する pure function 群。
70
-
71
- ```js
72
- export function readMarkdownFile(filePath)
73
- // 現在の readMarkdownFile と同一
74
-
75
- export function configureMarked(options)
76
- // marked.use(...) の設定を集約
77
-
78
- export function convertMermaidToAscii(markdown, options)
79
- // 現在と同一
80
-
81
- export function renderToString(markdown, options)
82
- // 現在と同一
83
-
84
- export function readStdin()
85
- // 現在と同一
86
- ```
87
-
88
- ### lib/outline.js -- アウトライン抽出
89
-
90
- terminal-kit に依存しない pure function。テスト容易。
91
-
92
- ```js
93
- /**
94
- * Markdown ソースから見出しを抽出
95
- * @param {string} markdown - 生の Markdown テキスト
96
- * @returns {Array<{level: number, text: string, line: number}>}
97
- *
98
- * 例: [
99
- * { level: 1, text: "Hello", line: 0 },
100
- * { level: 2, text: "Section A", line: 15 },
101
- * { level: 3, text: "Subsection A-1", line: 28 },
102
- * ]
103
- */
104
- export function extractHeadings(markdown)
105
-
106
- /**
107
- * レンダリング済みテキスト内で各見出しが何行目に対応するか算出
108
- * (スクロール位置ジャンプに使用)
109
- * @param {string} renderedText - marked で変換済みの ANSI 文字列
110
- * @param {Array} headings - extractHeadings の結果
111
- * @returns {Array<{...heading, renderedLine: number}>}
112
- */
113
- export function mapHeadingsToRenderedLines(renderedText, headings)
114
- ```
115
-
116
- ### lib/pager.js -- Pager 処理
117
-
118
- ```js
119
- export function shouldUsePager(text, options)
120
- // 現在と同一
121
-
122
- export function spawnPager(text, options)
123
- // 現在と同一
124
- ```
125
-
126
- ### lib/tui.js -- TUI モード (新規コア)
127
-
128
- terminal-kit 依存はこのファイルに閉じ込める。
129
- `--outline` 指定時のみ動的 import で読み込み、通常利用時のロード時間に影響しない。
130
-
131
- ```js
132
- import termkit from 'terminal-kit';
133
-
134
- /**
135
- * TUI モードを起動
136
- * @param {string} markdown - 生の Markdown ソース
137
- * @param {string} renderedText - レンダリング済みテキスト
138
- * @param {object} options - CLI オプション
139
- */
140
- export function launchTui(markdown, renderedText, options)
141
- ```
142
-
143
- 内部関数:
144
-
145
- ```
146
- launchTui(markdown, renderedText, options)
147
- |
148
- +-- createLayout(term)
149
- | ターミナル幅を取得し、左右のカラム幅を決定
150
- | 左: サイドバー (幅の 25%, 最小 20, 最大 40)
151
- | 右: コンテンツ (残り)
152
- | 戻り値: { sidebarWidth, contentWidth, height }
153
- |
154
- +-- renderSidebar(term, headings, selectedIndex, scrollOffset, layout)
155
- | アウトラインを左カラムに描画
156
- | 選択中の見出しをハイライト
157
- | インデント: level に応じて 2*(level-1) スペース
158
- |
159
- +-- renderContent(term, lines, scrollOffset, layout)
160
- | レンダリング済みテキストを右カラムに描画
161
- | contentWidth に合わせて行を切り詰め
162
- |
163
- +-- renderStatusBar(term, state, layout)
164
- | 最下行にステータス表示
165
- | "[q]quit [j/k]navigate [Enter]jump [Tab]toggle sidebar"
166
- |
167
- +-- setupKeyBindings(term, state)
168
- キー入力ハンドラ登録
169
- ```
170
-
171
- キーバインド:
172
-
173
- | キー | 動作 |
174
- |---|---|
175
- | j / Down | 次の見出し |
176
- | k / Up | 前の見出し |
177
- | Enter | 選択した見出しの位置にコンテンツをスクロール |
178
- | g / G | 先頭 / 末尾 |
179
- | Ctrl+D / Ctrl+U | 半ページスクロール |
180
- | Tab | サイドバーフォーカス切替 |
181
- | q / Ctrl+C | 終了 |
182
-
183
- ## 画面レイアウト
184
-
185
- ```
186
- +-- Outline (25%) --+-- Content (75%) --------------------------+
187
- | # Hello | # Hello |
188
- | ## Section A | |
189
- | ## Section B | This is **markdown** printed in the |
190
- | > ## Section C < | `terminal`. |
191
- | ### C-1 | |
192
- | ### C-2 | ## Section C |
193
- | ## Section D | |
194
- | | Lorem ipsum dolor sit amet... |
195
- | | ... |
196
- +-------------------+--------------------------------------------+
197
- [q]quit [j/k]move [Enter]jump [Tab]focus Section C 3/6
198
- ```
199
-
200
- ## データフロー
201
-
202
- ```
203
- markdown (raw)
204
- |
205
- +------------+------------+
206
- | |
207
- extractHeadings() configureMarked()
208
- | renderToString()
209
- | |
210
- headings[] renderedText
211
- | |
212
- mapHeadingsToRenderedLines() |
213
- | |
214
- headings[] with |
215
- renderedLine |
216
- | |
217
- +-------+ +--------------+
218
- | |
219
- launchTui()
220
- or spawnPager()
221
- or stdout.write()
222
- ```
223
-
224
- ## 設計上のポイント
225
-
226
- 1. **既存モードへの影響ゼロ** -- `--outline` なしなら従来通り `less` または stdout。TUI は `--outline` 時のみ動的ロードする
227
- 2. **モジュール分割は最小限** -- `tui.js` + `outline.js` の 2 ファイル追加のみ。`main.js` の既存ロジック (markdown 処理, pager) はそのまま残す。小さな CLI に不要な分割はしない
228
- 3. **outline.js は pure function** -- TUI ライブラリに依存しない。単体テスト可能
229
- 4. **TUI 依存は tui.js に閉じ込める** -- 将来別ライブラリに差し替える場合も tui.js だけ書き換えればよい
230
-
231
- ## レビュー指摘事項 (2026-03-07)
232
-
233
- ### terminal-kit 選定リスクの再評価
234
-
235
- メンテナ1名 + 79件のオープンイシュー積み残しは「リスク」ではなく「既に問題化」に近い。
236
- memd の TUI は比較的シンプル (2カラムレイアウト + キー入力) なので、以下の代替案も検討する:
237
-
238
- - **案A: terminal-kit 採用** (現行案) -- 機能は豊富だがメンテナンス不安
239
- - **案B: 自前 ANSI 実装** -- `process.stdout.write` + ANSI エスケープ + `readline` rawMode で実現。依存ゼロ。memd の TUI 要件 (2カラム描画, キー入力, カーソル制御) は Node.js 標準 API で十分カバー可能
240
-
241
- ### mapHeadingsToRenderedLines の実装方針
242
-
243
- ANSI エスケープ付きレンダリング済みテキストから見出し位置を特定する必要がある。
244
- marked-terminal の出力には以下の複雑性がある:
245
-
246
- - ANSI カラーコード
247
- - reflowText による折り返し
248
- - テーブル・コードブロックの展開
249
-
250
- 実装方針: レンダリング後テキストを `strip-ansi` して各行を走査し、見出しテキスト (extractHeadings の結果) と前方一致でマッチさせる。
251
- 完全一致が困難な場合は、marked のレンダリング時にカスタム renderer で見出し行にマーカー文字列を埋め込み、レンダリング後にマーカーで検索する方式をフォールバックとする。
252
-
253
- ### コンテンツ描画時の ANSI 対応
254
-
255
- - 行の切り詰め (truncate) 時に ANSI エスケープシーケンスの途中で切断すると表示が崩れる
256
- - `strip-ansi` + `string-width` を利用して表示幅を正確に算出し、ANSI シーケンスを壊さない切り詰め処理が必要
257
- - TUI モードでは marked-terminal の `width` オプションをコンテンツカラム幅に合わせて設定する (全幅ではなく右カラム幅)
258
-
259
- ### ターミナルリサイズ対応
260
-
261
- `process.stdout.on('resize', ...)` (SIGWINCH) でリサイズイベントを検知し、レイアウトを再計算して再描画する。
262
-
263
- ### Tab フォーカス切替時の UX 定義
264
-
265
- - サイドバーフォーカス時: j/k で見出し間を移動、Enter でコンテンツをジャンプ
266
- - コンテンツフォーカス時: j/k で 1 行単位スクロール、g/G で先頭/末尾
267
-
268
- ## モジュール分割方針 (修正)
269
-
270
- `main.js` の既存ロジック (readMarkdownFile, convertMermaidToAscii, renderToString, readStdin, shouldUsePager, spawnPager) はそのまま残す。
271
- 新規追加は以下の 2 ファイルのみ:
272
-
273
- ```
274
- memd/
275
- main.js # エントリーポイント (既存 + --outline 分岐追加)
276
- lib/
277
- outline.js # 見出し抽出・アウトラインデータ生成 (pure function, 新規)
278
- tui.js # TUI モード (新規)
279
- test/
280
- test1.md
281
- test2.md
282
- package.json
283
- ```
package/planb.md DELETED
@@ -1,186 +0,0 @@
1
- # 案B: 自前 ANSI 実装による TUI モード設計
2
-
3
- ## 方針
4
-
5
- terminal-kit を使わず、Node.js 標準 API + ANSI エスケープシーケンスのみで TUI を実装する。
6
- memd の TUI 要件 (2カラム描画 + キー入力 + スクロール) は限定的であり、外部依存なしで実現可能。
7
-
8
- ## 使用する Node.js 標準 API
9
-
10
- | 用途 | API |
11
- |---|---|
12
- | 画面描画 | `process.stdout.write` + ANSI エスケープシーケンス |
13
- | キー入力 | `readline.emitKeypressEvents` + `process.stdin.setRawMode(true)` |
14
- | 画面サイズ | `process.stdout.columns` / `process.stdout.rows` |
15
- | リサイズ検知 | `process.stdout.on('resize', ...)` |
16
-
17
- 外部依存は `strip-ansi` と `string-width` のみ (いずれも marked-terminal の transitive dep として既存)。
18
-
19
- ## ANSI エスケープの基本操作
20
-
21
- ```js
22
- const ESC = '\x1b[';
23
- const cursor = {
24
- moveTo: (x, y) => `${ESC}${y + 1};${x + 1}H`,
25
- hide: `${ESC}?25l`,
26
- show: `${ESC}?25h`,
27
- clearScreen: `${ESC}2J`,
28
- clearLine: `${ESC}2K`,
29
- };
30
- const style = {
31
- reverse: `${ESC}7m`,
32
- reset: `${ESC}0m`,
33
- dim: `${ESC}2m`,
34
- bold: `${ESC}1m`,
35
- };
36
- ```
37
-
38
- ## モジュール構成
39
-
40
- ```
41
- memd/
42
- main.js # エントリーポイント (既存 + --outline 分岐追加)
43
- lib/
44
- outline.js # 見出し抽出・アウトラインデータ生成 (pure function, 新規)
45
- tui.js # TUI モード (新規, 自前 ANSI 実装)
46
- test/
47
- test1.md
48
- test2.md
49
- package.json
50
- ```
51
-
52
- ## tui.js の内部構造
53
-
54
- ```
55
- launchTui(markdown, renderedText, headings, options)
56
- |
57
- +-- state = {
58
- | sidebarFocused: true,
59
- | selectedHeading: 0,
60
- | contentScroll: 0,
61
- | sidebarScroll: 0,
62
- | layout: computeLayout(),
63
- | }
64
- |
65
- +-- computeLayout()
66
- | cols = process.stdout.columns
67
- | rows = process.stdout.rows
68
- | sidebarWidth = clamp(Math.floor(cols * 0.25), 20, 40)
69
- | contentWidth = cols - sidebarWidth - 1 // 1 for separator
70
- | height = rows - 1 // 1 for status bar
71
- |
72
- +-- render(state)
73
- | buf = ''
74
- | buf += cursor.hide
75
- | buf += renderSidebar(state)
76
- | buf += renderSeparator(state)
77
- | buf += renderContent(state)
78
- | buf += renderStatusBar(state)
79
- | process.stdout.write(buf) // 1回の write でフリッカー軽減
80
- |
81
- +-- renderSidebar(state)
82
- | 各見出しを sidebarWidth 内に収めて描画
83
- | 選択行は reverse video で表示
84
- | level に応じて 2*(level-1) スペースでインデント
85
- |
86
- +-- renderSeparator(state)
87
- | 縦線 '|' を各行の sidebarWidth 位置に描画
88
- |
89
- +-- renderContent(state)
90
- | renderedText を行分割済み配列として保持
91
- | contentScroll から height 行分を描画
92
- | 各行を contentWidth でスライス (ANSI 対応)
93
- |
94
- +-- renderStatusBar(state)
95
- | 最下行にキーバインドヘルプ + 現在位置表示
96
- |
97
- +-- handleKeypress(key, state)
98
- keypress イベントから state を更新し render() を呼ぶ
99
- ```
100
-
101
- ## キー入力の実装
102
-
103
- ```js
104
- import readline from 'readline';
105
-
106
- readline.emitKeypressEvents(process.stdin);
107
- process.stdin.setRawMode(true);
108
- process.stdin.on('keypress', (str, key) => {
109
- if (key.name === 'q' || (key.ctrl && key.name === 'c')) {
110
- cleanup();
111
- process.exit(0);
112
- }
113
- handleKeypress(key, state);
114
- render(state);
115
- });
116
- ```
117
-
118
- ### キーバインド
119
-
120
- | キー | サイドバーフォーカス時 | コンテンツフォーカス時 |
121
- |---|---|---|
122
- | j / Down | 次の見出し | 1行下スクロール |
123
- | k / Up | 前の見出し | 1行上スクロール |
124
- | Enter | 選択した見出しの位置にコンテンツをスクロール | -- |
125
- | g / G | 先頭 / 末尾 | 先頭 / 末尾 |
126
- | Ctrl+D / Ctrl+U | -- | 半ページスクロール |
127
- | Tab | コンテンツへフォーカス移動 | サイドバーへフォーカス移動 |
128
- | q / Ctrl+C | 終了 | 終了 |
129
-
130
- ## フリッカー対策
131
-
132
- - 毎フレーム `clearScreen` せず、各行を `moveTo` + `clearLine` + 描画で上書き
133
- - 全描画内容を文字列バッファに溜めてから1回の `process.stdout.write(buf)` で出力
134
-
135
- ## ANSI 対応の行スライス
136
-
137
- コンテンツ行を `contentWidth` に切り詰める際の処理:
138
-
139
- ```js
140
- function sliceAnsiLine(line, maxWidth) {
141
- // 1. ANSI エスケープを位置情報付きで分離
142
- // 2. 表示文字の幅を string-width で計測しながら maxWidth まで切り詰め
143
- // 3. 切り詰め後に開いたままの ANSI シーケンスを reset で閉じる
144
- }
145
- ```
146
-
147
- ## cleanup 処理
148
-
149
- ```js
150
- function cleanup() {
151
- process.stdout.write(cursor.show);
152
- process.stdout.write(cursor.clearScreen);
153
- process.stdout.write(cursor.moveTo(0, 0));
154
- process.stdin.setRawMode(false);
155
- }
156
- // 異常終了時もクリーンアップ
157
- process.on('exit', cleanup);
158
- process.on('SIGINT', () => { cleanup(); process.exit(0); });
159
- ```
160
-
161
- ## ターミナルリサイズ対応
162
-
163
- ```js
164
- process.stdout.on('resize', () => {
165
- state.layout = computeLayout();
166
- render(state);
167
- });
168
- ```
169
-
170
- ## mapHeadingsToRenderedLines の実装方針
171
-
172
- ANSI 付きレンダリング済みテキストから見出し位置を特定する:
173
-
174
- 1. レンダリング後テキストを `strip-ansi` して各行を走査
175
- 2. `extractHeadings` の結果と前方一致でマッチさせる
176
- 3. フォールバック: marked のカスタム renderer で見出し行にマーカー文字列を埋め込み、レンダリング後にマーカーで検索
177
-
178
- ## 案A (terminal-kit) との比較
179
-
180
- | | 案A (terminal-kit) | 案B (自前 ANSI) |
181
- |---|---|---|
182
- | 外部依存 | +1 (terminal-kit, ~2MB) | +0 (strip-ansi, string-width は既存 transitive dep) |
183
- | 実装量 | 少ない (API 呼ぶだけ) | 中程度 (~200行の描画コード) |
184
- | メンテリスク | メンテナ1名、更新頻度低下 | 自前なので自己責任 |
185
- | 機能カバー | 豊富 (使わない機能も多い) | 必要最小限 |
186
- | インストール速度 | native module なし、影響軽微 | 影響なし |