@first-ch/tools-mcp 0.36.0 → 0.38.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.
@@ -0,0 +1,1060 @@
1
+ /* 社内文書の重複・孤立・表記ゆれ検査(wiki_doc_lint)
2
+ * 検査コア(lintDocs / buildReport / parseDoc / parseDictionary ほか)は
3
+ * FirstCHTools の site/wiki-doc-lint/app.js と同一
4
+ * (2箇所ルール: site側が正本・片方を直したらもう片方も同じ内容で反映する)。
5
+ * 両ファイルからコアの開始/終了コメントに挟まれた範囲を sed で切り出し
6
+ * (site側は字下げ2文字を落とす・こちらは行頭の export を落とす)、
7
+ * diff が空になることで同期を機械的に確認できる。
8
+ *
9
+ * Notion・Backlog Wiki・Confluence などから書き出した Markdown/HTML/テキストを読み、
10
+ * 内容がほぼ同じページ・どこからもリンクされていないページ・表記ゆれや旧称・リンク切れを返す。
11
+ * 読むだけでファイルは書き換えない。完全ローカル処理・ネットワーク送信なし。 */
12
+
13
+ import { readFile, readdir, stat } from 'node:fs/promises';
14
+ import { isAbsolute, join, relative, sep } from 'node:path';
15
+
16
+ /* ==================== ここから検査コア ==================== */
17
+
18
+ export const LIMITS = { files: 3000, fileBytes: 5 * 1024 * 1024, totalBytes: 60 * 1024 * 1024 };
19
+ export const DOC_EXT_RE = /\.(md|markdown|mdown|mkd|txt|html?|xhtml)$/i;
20
+ // 入口として扱うページ名(孤立に数えない)。Notion の32桁のIDを外し、小文字にしてから比べる
21
+ export const ENTRY_STEMS = ['index', 'readme', 'home', 'top', '_index', 'ホーム', 'トップ', 'トップページ', '目次'];
22
+ export const THRESHOLDS = [0.6, 0.7, 0.8, 0.9];
23
+ // 重複の判定に使う最小の長さ(記号と空白を除いた文字数)。これより短いページは比べない
24
+ export const MIN_DUP_CHARS = 40;
25
+ export const SHINGLE = 4;
26
+ export const SIG_BINS = 64;
27
+ export const MAX_HITS_PER_TERM = 300;
28
+ export const MAX_PAIRS = 300;
29
+
30
+ export const DEFAULT_DICT = [
31
+ '# 1行に1つ、「揃える表記 ← ゆれ・旧称」の形で書きます(ゆれが複数あるときは読点かカンマで区切る)。',
32
+ '# 「旧称 → 新しい名前」の向きでも書けます。行頭の # はコメントです。',
33
+ '# 例: 経費ナビ ← 精算くん(サービス名の変更)',
34
+ 'サーバー ← サーバ',
35
+ 'ユーザー ← ユーザ',
36
+ 'コンピューター ← コンピュータ',
37
+ 'インターフェース ← インタフェース, インターフェイス',
38
+ '問い合わせ ← 問合せ, 問合わせ, 問い合せ',
39
+ '打ち合わせ ← 打合せ, 打ち合せ, 打合わせ',
40
+ 'JavaScript ← Javascript',
41
+ 'GitHub ← Github',
42
+ ].join('\n');
43
+
44
+ export const DEFAULT_DICT_EN = [
45
+ '# One rule per line: "preferred ← variant" (separate several variants with commas).',
46
+ '# "old name → new name" also works. Lines starting with # are comments.',
47
+ '# Example: Expense Hub ← ExpenseBot (a renamed service)',
48
+ 'email ← e-mail, E-mail',
49
+ 'website ← web site, Web site',
50
+ 'JavaScript ← Javascript',
51
+ 'GitHub ← Github',
52
+ 'サーバー ← サーバ',
53
+ 'ユーザー ← ユーザ',
54
+ '問い合わせ ← 問合せ, 問合わせ, 問い合せ',
55
+ ].join('\n');
56
+
57
+ export const WDL_MSG = {
58
+ ja: {
59
+ title: '社内文書チェックの結果',
60
+ target: (s) => `対象: ${s.docs}ページ(Markdown ${s.md}・HTML ${s.html}・テキスト ${s.txt})`,
61
+ sumDup: (n) => `重複の疑い: ${n}組`,
62
+ sumOrphan: (n) => `孤立ページ: ${n}件`,
63
+ sumTerm: (k, n) => `表記ゆれ・旧称: ${k}種類・${n}か所`,
64
+ sumBroken: (n) => `リンク切れ: ${n}件`,
65
+ hDup: (th) => `重複の疑い(類似度${Math.round(th * 100)}%以上)`,
66
+ hOrphan: '孤立ページ(ほかのどのページからもリンクされていない)',
67
+ hTerm: '表記ゆれ・旧称',
68
+ hBroken: 'リンク切れ',
69
+ none: 'ありません。',
70
+ dupHead: (a, b, p) => `${a} ⇔ ${b}(類似度 ${p}%)`,
71
+ orphanItem: (path, title, fix) => `\`${path}\`(${title})— ${fix}`,
72
+ onlyIn: (name) => `「${name}」だけにある行`,
73
+ quote: (x) => `「${x}」`,
74
+ dupFix: (keepT, keep, dropT, drop, why) =>
75
+ `「${keepT}」(${keep})を残します(${why})。「${dropT}」(${drop})だけにある記述を移してから削除するか、冒頭に「最新版は「${keepT}」を参照」と書いて残します。`,
76
+ whyLinks: (a, b) => `被リンク ${a}件と${b}件`,
77
+ whyLen: (a, b) => `本文 ${a}字と${b}字`,
78
+ whyPath: '被リンク数と本文の長さが同じため、パスの順',
79
+ orphanFix: (pt, p, line) => `「${pt}」(${p})に次の行を足してリンクします: ${line} 不要なページなら削除かアーカイブを検討します。`,
80
+ orphanFixNone: 'どのページからも辿れません。目次になるページ(README など)からリンクするか、不要なら削除かアーカイブを検討します。',
81
+ brokenFix: (t) => `リンク先「${t}」が読み込んだファイルの中にありません。ページ名の変更・削除・移動を確認し、リンクを直すか外します。`,
82
+ termHead: (pref, vars, src) => `${vars.join('・')} → ${pref}(${src === 'dict' ? '辞書' : '長音の自動検出・多い方'})`,
83
+ termCounts: (counts) => Object.keys(counts).map((f) => `${f} ${counts[f]}か所`).join('/'),
84
+ line: (n) => `${n}行目`,
85
+ more: (n) => `ほか${n}件は省略しました。`,
86
+ entries: (list) => `入口として孤立に数えなかったページ: ${list.join('、')}`,
87
+ short: (n) => `本文が短く重複の判定から外したページ: ${n}件`,
88
+ dictErr: (l, t) => `辞書の${l}行目を読めませんでした(「揃える表記 ← ゆれ」の形で書きます): ${t}`,
89
+ },
90
+ en: {
91
+ title: 'Wiki document check',
92
+ target: (s) => `Checked: ${s.docs} pages (Markdown ${s.md}, HTML ${s.html}, text ${s.txt})`,
93
+ sumDup: (n) => `Likely duplicates: ${n} pair${n === 1 ? '' : 's'}`,
94
+ sumOrphan: (n) => `Orphan pages: ${n}`,
95
+ sumTerm: (k, n) => `Inconsistent or outdated terms: ${k} kind${k === 1 ? '' : 's'}, ${n} place${n === 1 ? '' : 's'}`,
96
+ sumBroken: (n) => `Broken links: ${n}`,
97
+ hDup: (th) => `Likely duplicates (similarity ${Math.round(th * 100)}% or more)`,
98
+ hOrphan: 'Orphan pages (no other page links to them)',
99
+ hTerm: 'Inconsistent and outdated terms',
100
+ hBroken: 'Broken links',
101
+ none: 'None.',
102
+ dupHead: (a, b, p) => `${a} ⇔ ${b} (similarity ${p}%)`,
103
+ orphanItem: (path, title, fix) => `\`${path}\` (${title}) — ${fix}`,
104
+ onlyIn: (name) => `Lines found only in "${name}"`,
105
+ quote: (x) => `"${x}"`,
106
+ dupFix: (keepT, keep, dropT, drop, why) =>
107
+ `Keep "${keepT}" (${keep}) (${why}). Move anything that only appears in "${dropT}" (${drop}) across, then delete it, or leave it with a first line saying "See ${keepT} for the current version".`,
108
+ whyLinks: (a, b) => `${a} vs ${b} incoming links`,
109
+ whyLen: (a, b) => `${a} vs ${b} characters of text`,
110
+ whyPath: 'same links and length, so by path order',
111
+ orphanFix: (pt, p, line) => `Add this line to "${pt}" (${p}) to link it: ${line} If the page is no longer needed, delete or archive it.`,
112
+ orphanFixNone: 'Nothing leads to this page. Link it from a contents page such as a README, or delete or archive it if it is no longer needed.',
113
+ brokenFix: (t) => `"${t}" is not among the files you loaded. Check whether the page was renamed, moved or deleted, then fix or remove the link.`,
114
+ termHead: (pref, vars, src) => `${vars.join(', ')} → ${pref} (${src === 'dict' ? 'dictionary' : 'long-vowel mark, auto-detected, majority form'})`,
115
+ termCounts: (counts) => Object.keys(counts).map((f) => `${f} ×${counts[f]}`).join(' / '),
116
+ line: (n) => `line ${n}`,
117
+ more: (n) => `${n} more not shown.`,
118
+ entries: (list) => `Treated as entry pages and not counted as orphans: ${list.join(', ')}`,
119
+ short: (n) => `Pages too short to compare for duplicates: ${n}`,
120
+ dictErr: (l, t) => `Could not read line ${l} of the dictionary (write it as "preferred ← variant"): ${t}`,
121
+ },
122
+ };
123
+
124
+ export function wdlMsg(lang) {
125
+ return WDL_MSG[lang === 'en' ? 'en' : 'ja'];
126
+ }
127
+
128
+ export function isDocName(path) {
129
+ const p = String(path).replace(/\\/g, '/');
130
+ if (p.split('/').some((s) => s.length > 1 && s[0] === '.' && s !== '..')) return false;
131
+ return DOC_EXT_RE.test(p);
132
+ }
133
+
134
+ export function docType(path) {
135
+ const m = /\.([a-z]+)$/i.exec(path);
136
+ const e = m ? m[1].toLowerCase() : '';
137
+ if (e === 'txt') return 'txt';
138
+ if (e === 'html' || e === 'htm' || e === 'xhtml') return 'html';
139
+ return 'md';
140
+ }
141
+
142
+ /** バイト列を文字列へ。BOM → UTF-8(厳密) → Shift_JIS の順に試す */
143
+ export function decodeBytes(bytes) {
144
+ const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
145
+ if (u8[0] === 0xef && u8[1] === 0xbb && u8[2] === 0xbf) return { text: new TextDecoder('utf-8').decode(u8.subarray(3)), encoding: 'utf-8' };
146
+ if (u8[0] === 0xff && u8[1] === 0xfe) return { text: new TextDecoder('utf-16le').decode(u8.subarray(2)), encoding: 'utf-16le' };
147
+ if (u8[0] === 0xfe && u8[1] === 0xff) return { text: new TextDecoder('utf-16be').decode(u8.subarray(2)), encoding: 'utf-16be' };
148
+ try {
149
+ return { text: new TextDecoder('utf-8', { fatal: true }).decode(u8), encoding: 'utf-8' };
150
+ } catch (e) {
151
+ // UTF-8 として読めないものは Shift_JIS とみなす(古い Wiki の書き出しに残っている)
152
+ }
153
+ try {
154
+ return { text: new TextDecoder('shift_jis').decode(u8), encoding: 'shift_jis' };
155
+ } catch (e) {
156
+ return { text: new TextDecoder('utf-8').decode(u8), encoding: 'utf-8' };
157
+ }
158
+ }
159
+
160
+ /* ---------- パス ---------- */
161
+
162
+ export function normPath(p) {
163
+ const out = [];
164
+ String(p).replace(/\\/g, '/').split('/').forEach((s) => {
165
+ if (!s || s === '.') return;
166
+ if (s === '..') out.pop();
167
+ else out.push(s);
168
+ });
169
+ return out.join('/');
170
+ }
171
+
172
+ export function dirOf(p) {
173
+ const i = p.lastIndexOf('/');
174
+ return i < 0 ? '' : p.slice(0, i);
175
+ }
176
+
177
+ export function baseOf(p) {
178
+ return p.slice(p.lastIndexOf('/') + 1);
179
+ }
180
+
181
+ export function stemOf(p) {
182
+ return baseOf(p).replace(/\.[^.]+$/, '');
183
+ }
184
+
185
+ // Notion の書き出しはファイル名・URLの末尾に32桁のIDを付ける
186
+ export function stripNotionId(s) {
187
+ return String(s).replace(/[\s_-]+[0-9a-f]{32}$/i, '') || String(s);
188
+ }
189
+
190
+ // ページ名の照合用。全角半角・大文字小文字・空白/ハイフン/アンダースコアの違いを無視する
191
+ export function nameKey(s) {
192
+ return stripNotionId(String(s).normalize('NFKC').trim())
193
+ .toLowerCase()
194
+ .replace(/[\s_\-+]+/g, ' ')
195
+ .trim();
196
+ }
197
+
198
+ export function safeDecode(s) {
199
+ try {
200
+ return decodeURIComponent(s);
201
+ } catch (e) {
202
+ return s;
203
+ }
204
+ }
205
+
206
+ /** fromDir から toPath への相対パス(Markdown/HTML のリンクにそのまま書ける形) */
207
+ export function relPath(fromDir, toPath, type) {
208
+ const a = fromDir ? fromDir.split('/') : [];
209
+ const b = toPath.split('/');
210
+ let i = 0;
211
+ while (i < a.length && i < b.length - 1 && a[i] === b[i]) i++;
212
+ const segs = [];
213
+ for (let k = i; k < a.length; k++) segs.push('..');
214
+ for (let k = i; k < b.length; k++) segs.push(b[k]);
215
+ const enc = (s) => (type === 'html' ? s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;') : s.replace(/%/g, '%25').replace(/ /g, '%20').replace(/\(/g, '%28').replace(/\)/g, '%29'));
216
+ return segs.map(enc).join('/');
217
+ }
218
+
219
+ /* ---------- 行 ---------- */
220
+
221
+ export function lineStarts(text) {
222
+ const a = [0];
223
+ for (let i = text.indexOf('\n'); i !== -1; i = text.indexOf('\n', i + 1)) a.push(i + 1);
224
+ return a;
225
+ }
226
+
227
+ export function lineIndexAt(starts, pos) {
228
+ let lo = 0;
229
+ let hi = starts.length - 1;
230
+ while (lo < hi) {
231
+ const mid = (lo + hi + 1) >> 1;
232
+ if (starts[mid] <= pos) lo = mid;
233
+ else hi = mid - 1;
234
+ }
235
+ return lo;
236
+ }
237
+
238
+ /* ---------- 読み取り(コード・タグ・URLを同じ長さの空白で隠し、位置をずらさない) ---------- */
239
+
240
+ export function blank(s) {
241
+ return s.replace(/[^\n]/g, ' ');
242
+ }
243
+
244
+ export const URL_RE = /https?:\/\/[^\s<>()[\]"'`、。「」()]+/g;
245
+
246
+ export function decodeEntities(s) {
247
+ const map = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
248
+ return s.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (m, e) => {
249
+ if (e[0] === '#') {
250
+ const n = e[1] === 'x' || e[1] === 'X' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
251
+ return n > 0 && n <= 0x10ffff ? String.fromCodePoint(n) : m;
252
+ }
253
+ const k = e.toLowerCase();
254
+ return Object.prototype.hasOwnProperty.call(map, k) ? map[k] : m;
255
+ });
256
+ }
257
+
258
+ export function plainInline(s) {
259
+ return decodeEntities(s.replace(/<[^>]*>/g, ''))
260
+ .replace(/\s+/g, ' ')
261
+ .trim();
262
+ }
263
+
264
+ // Markdown の1段目: 前付け(front matter)・コードブロック・インラインコード・コメントを隠す
265
+ export function maskMarkdownCode(text) {
266
+ const lines = text.split('\n');
267
+ let fence = null;
268
+ let inFront = false;
269
+ for (let i = 0; i < lines.length; i++) {
270
+ const l = lines[i];
271
+ if (i === 0 && /^---\s*$/.test(l)) {
272
+ inFront = true;
273
+ lines[i] = blank(l);
274
+ continue;
275
+ }
276
+ if (inFront) {
277
+ if (/^(---|\.\.\.)\s*$/.test(l)) inFront = false;
278
+ lines[i] = blank(l);
279
+ continue;
280
+ }
281
+ if (fence) {
282
+ const m = /^ {0,3}(`{3,}|~{3,})\s*$/.exec(l);
283
+ if (m && m[1][0] === fence.ch && m[1].length >= fence.len) fence = null;
284
+ lines[i] = blank(l);
285
+ continue;
286
+ }
287
+ const m = /^ {0,3}(`{3,}|~{3,})/.exec(l);
288
+ if (m && !(m[1][0] === '`' && l.indexOf('`', m[0].length) !== -1)) {
289
+ fence = { ch: m[1][0], len: m[1].length };
290
+ lines[i] = blank(l);
291
+ }
292
+ }
293
+ return lines
294
+ .join('\n')
295
+ .replace(/<!--[\s\S]*?-->/g, blank)
296
+ .replace(/``[^\n]+?``|`[^`\n]+`/g, blank);
297
+ }
298
+
299
+ export function frontMatterTitle(text) {
300
+ if (!/^---\s*\n/.test(text)) return '';
301
+ const end = text.search(/\n(---|\.\.\.)\s*(\n|$)/);
302
+ if (end < 0) return '';
303
+ const m = /^title:\s*(.+)$/m.exec(text.slice(0, end));
304
+ return m ? m[1].trim().replace(/^(["'])(.*)\1$/, '$2').trim() : '';
305
+ }
306
+
307
+ export const MD_LINK_RE = /(!?)\[((?:[^[\]\n]|\[[^[\]\n]*\])*)\]\(\s*(?:<([^>\n]*)>|([^)\s]+(?:\([^)\s]*\)[^)\s]*)*))(?:\s+(?:"[^"\n]*"|'[^'\n]*'))?\s*\)/g;
308
+ export const WIKI_LINK_RE = /\[\[([^[\]\n|#]*)(?:#[^[\]\n|]*)?(?:\|[^[\]\n]*)?\]\]/g;
309
+ export const REF_DEF_RE = /^ {0,3}\[(?!\^)[^\]\n]+\]:[ \t]*<?([^\s>]+)>?/gm;
310
+ export const A_HREF_RE = /<a\b[^>]*?\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>"']+))/gi;
311
+
312
+ export function collectHtmlLinks(s, links) {
313
+ A_HREF_RE.lastIndex = 0;
314
+ let m;
315
+ while ((m = A_HREF_RE.exec(s))) {
316
+ const t = decodeEntities(m[1] !== undefined ? m[1] : m[2] !== undefined ? m[2] : m[3]);
317
+ links.push({ kind: 'html', target: t, pos: m.index });
318
+ }
319
+ }
320
+
321
+ export function collectUrls(s, links) {
322
+ URL_RE.lastIndex = 0;
323
+ let m;
324
+ while ((m = URL_RE.exec(s))) links.push({ kind: 'url', target: m[0], pos: m.index });
325
+ }
326
+
327
+ /** 1ファイルを読み、タイトル・リンク・検査用の本文(コード等を隠したもの)を取り出す */
328
+ export function parseDoc(path, text) {
329
+ const p = normPath(path);
330
+ const type = docType(p);
331
+ const src = String(text == null ? '' : text).replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n');
332
+ const links = [];
333
+ let title = '';
334
+ let masked;
335
+ if (type === 'html') {
336
+ const s1 = src.replace(/<script\b[\s\S]*?<\/script\s*>|<style\b[\s\S]*?<\/style\s*>|<!--[\s\S]*?-->/gi, blank);
337
+ collectHtmlLinks(s1, links);
338
+ const h1 = /<h1\b[^>]*>([\s\S]*?)<\/h1\s*>/i.exec(s1);
339
+ const tt = /<title\b[^>]*>([\s\S]*?)<\/title\s*>/i.exec(s1);
340
+ collectUrls(s1.replace(/<[^>]*>/g, blank), links);
341
+ title = plainInline(h1 ? h1[1] : tt ? tt[1] : '');
342
+ // Confluence の書き出しは「スペース名 : ページ名」
343
+ const cm = /^[^:]{1,80}\s:\s(.+)$/.exec(title);
344
+ if (cm) title = cm[1].trim();
345
+ masked = s1
346
+ .replace(/<(pre|code|head)\b[\s\S]*?<\/\1\s*>/gi, blank)
347
+ .replace(/<[^>]*>/g, blank)
348
+ .replace(URL_RE, blank);
349
+ } else if (type === 'md') {
350
+ const s1 = maskMarkdownCode(src);
351
+ MD_LINK_RE.lastIndex = 0;
352
+ let m;
353
+ while ((m = MD_LINK_RE.exec(s1))) {
354
+ if (m[1]) continue;
355
+ links.push({ kind: 'md', target: m[3] !== undefined ? m[3] : m[4], pos: m.index });
356
+ }
357
+ WIKI_LINK_RE.lastIndex = 0;
358
+ while ((m = WIKI_LINK_RE.exec(s1))) links.push({ kind: 'wiki', target: m[1], pos: m.index });
359
+ REF_DEF_RE.lastIndex = 0;
360
+ while ((m = REF_DEF_RE.exec(s1))) links.push({ kind: 'ref', target: m[1], pos: m.index });
361
+ collectHtmlLinks(s1, links);
362
+ collectUrls(s1.replace(/\]\([^)\n]*\)|<[^>\n]*>/g, blank), links);
363
+ title = frontMatterTitle(src);
364
+ if (!title) {
365
+ const h = /^ {0,3}#[ \t]+(.+?)[ \t#]*$/m.exec(s1);
366
+ if (h) title = plainInline(h[1].replace(/[*_`]/g, '').replace(/\[([^\]]*)\]\([^)]*\)/g, '$1'));
367
+ }
368
+ masked = s1
369
+ .replace(/<\/?[A-Za-z][^>\n]*>/g, blank)
370
+ .replace(/\]\(\s*(?:<[^>\n]*>|[^)\s]+(?:\([^)\s]*\)[^)\s]*)*)(?:\s+(?:"[^"\n]*"|'[^'\n]*'))?\s*\)/g, (x) => ']' + blank(x.slice(1)))
371
+ .replace(/^( {0,3}\[[^\]\n]+\]:)([^\n]*)/gm, (x, a, b) => a + blank(b))
372
+ .replace(URL_RE, blank);
373
+ } else {
374
+ collectUrls(src, links);
375
+ masked = src.replace(URL_RE, blank);
376
+ }
377
+ if (!title) title = stripNotionId(stemOf(p));
378
+ return { path: p, type, text: src, masked, title, links, starts: lineStarts(src) };
379
+ }
380
+
381
+ /* ---------- 辞書 ---------- */
382
+
383
+ export function parseDictionary(src) {
384
+ const rules = [];
385
+ const errors = [];
386
+ const seen = new Set();
387
+ String(src == null ? '' : src)
388
+ .split(/\r\n?|\n/)
389
+ .forEach((raw, i) => {
390
+ const line = raw.replace(/(^|\s)#.*$/, '').trim();
391
+ if (!line) return;
392
+ let m = /^(.+?)\s*(?:←|<-|<=)\s*(.+)$/.exec(line);
393
+ let preferred;
394
+ let vars;
395
+ if (m) {
396
+ preferred = m[1];
397
+ vars = m[2];
398
+ } else if ((m = /^(.+?)\s*(?:→|->|=>)\s*(.+)$/.exec(line))) {
399
+ preferred = m[2];
400
+ vars = m[1];
401
+ } else {
402
+ errors.push({ line: i + 1, text: raw.trim() });
403
+ return;
404
+ }
405
+ preferred = preferred.trim();
406
+ const variants = [];
407
+ vars.split(/[,、,;;]/).forEach((v) => {
408
+ const t = v.trim();
409
+ if (t && t !== preferred && variants.indexOf(t) === -1 && !seen.has(t)) variants.push(t);
410
+ });
411
+ if (!preferred || /[,、,;;←→]|<-|->/.test(preferred) || !variants.length) {
412
+ errors.push({ line: i + 1, text: raw.trim() });
413
+ return;
414
+ }
415
+ variants.forEach((v) => seen.add(v));
416
+ rules.push({ preferred, variants, line: i + 1 });
417
+ });
418
+ return { rules, errors };
419
+ }
420
+
421
+ /* ---------- 表記ゆれ ---------- */
422
+
423
+ export const KATA_CH = /[\u30a1-\u30fa\u30fc]/;
424
+ export const WORD_CH = /[A-Za-z0-9_]/;
425
+ export const KATA_RUN_RE = /[\u30a1-\u30fa\u30fc\u30fd\u30fe]+/g;
426
+
427
+ export function escapeRe(s) {
428
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
429
+ }
430
+
431
+ // 語の途中を拾わない(英数字の語の中・カタカナ語の続き〔ユーザ→ユーザビリティ〕)
432
+ export function boundaryOk(t, s, e, f) {
433
+ const before = s > 0 ? t[s - 1] : '';
434
+ const after = e < t.length ? t[e] : '';
435
+ if (WORD_CH.test(f[0]) && before && WORD_CH.test(before)) return false;
436
+ if (WORD_CH.test(f[f.length - 1]) && after && WORD_CH.test(after)) return false;
437
+ if (KATA_CH.test(f[f.length - 1]) && after && KATA_CH.test(after)) return false;
438
+ return true;
439
+ }
440
+
441
+ export function scanRule(rule, docs) {
442
+ const forms = [rule.preferred].concat(rule.variants).sort((a, b) => b.length - a.length);
443
+ const re = new RegExp(forms.map(escapeRe).join('|'), 'g');
444
+ const counts = {};
445
+ [rule.preferred].concat(rule.variants).forEach((f) => (counts[f] = 0));
446
+ const hits = [];
447
+ docs.forEach((d, di) => {
448
+ const t = d.masked;
449
+ re.lastIndex = 0;
450
+ let m;
451
+ while ((m = re.exec(t))) {
452
+ const f = m[0];
453
+ if (!boundaryOk(t, m.index, m.index + f.length, f)) {
454
+ re.lastIndex = m.index + 1;
455
+ continue;
456
+ }
457
+ counts[f]++;
458
+ if (f !== rule.preferred) hits.push({ doc: di, pos: m.index, form: f });
459
+ }
460
+ });
461
+ return { counts, hits };
462
+ }
463
+
464
+ // 辞書に無いカタカナ語の長音ゆれ(サーバ/サーバー)。語の末尾の「ー」の有無だけを見る
465
+ export function autoChoonGroups(docs, dictForms) {
466
+ const count = new Map();
467
+ docs.forEach((d) => {
468
+ KATA_RUN_RE.lastIndex = 0;
469
+ let m;
470
+ while ((m = KATA_RUN_RE.exec(d.masked))) count.set(m[0], (count.get(m[0]) || 0) + 1);
471
+ });
472
+ const groups = [];
473
+ count.forEach((nLong, long) => {
474
+ if (long.slice(-1) !== 'ー' || long.slice(-2) === 'ーー') return;
475
+ const short = long.slice(0, -1);
476
+ if (short.length < 3 || !count.has(short)) return;
477
+ if (dictForms.has(long) || dictForms.has(short)) return;
478
+ const nShort = count.get(short);
479
+ const preferred = nLong >= nShort ? long : short;
480
+ const variant = preferred === long ? short : long;
481
+ const counts = {};
482
+ counts[preferred] = preferred === long ? nLong : nShort;
483
+ counts[variant] = variant === long ? nLong : nShort;
484
+ groups.push({ preferred, variants: [variant], counts });
485
+ });
486
+ const byVariant = new Map(groups.map((g) => [g.variants[0], g]));
487
+ groups.forEach((g) => (g.hits = []));
488
+ if (groups.length) {
489
+ docs.forEach((d, di) => {
490
+ KATA_RUN_RE.lastIndex = 0;
491
+ let m;
492
+ while ((m = KATA_RUN_RE.exec(d.masked))) {
493
+ const g = byVariant.get(m[0]);
494
+ if (g) g.hits.push({ doc: di, pos: m.index, form: m[0] });
495
+ }
496
+ });
497
+ }
498
+ return groups;
499
+ }
500
+
501
+ /* ---------- 重複(4文字単位の断片の集合で Jaccard 係数を求める) ---------- */
502
+
503
+ export function normForDup(masked) {
504
+ return masked
505
+ .normalize('NFKC')
506
+ .toLowerCase()
507
+ .replace(/[^\p{L}\p{N}]+/gu, '');
508
+ }
509
+
510
+ export function shingleHashes(norm) {
511
+ const n = norm.length - SHINGLE + 1;
512
+ if (n <= 0) return new Uint32Array(0);
513
+ const a = new Uint32Array(n);
514
+ for (let i = 0; i < n; i++) {
515
+ let h = 0x811c9dc5;
516
+ for (let j = i; j < i + SHINGLE; j++) {
517
+ h ^= norm.charCodeAt(j);
518
+ h = Math.imul(h, 0x01000193);
519
+ }
520
+ h ^= h >>> 16;
521
+ h = Math.imul(h, 0x85ebca6b);
522
+ h ^= h >>> 13;
523
+ h = Math.imul(h, 0xc2b2ae35);
524
+ h ^= h >>> 16;
525
+ a[i] = h >>> 0;
526
+ }
527
+ a.sort();
528
+ let k = 0;
529
+ for (let i = 0; i < n; i++) if (i === 0 || a[i] !== a[k - 1]) a[k++] = a[i];
530
+ return a.subarray(0, k);
531
+ }
532
+
533
+ // 1回のハッシュで作る MinHash(区画ごとの最小値・空の区画は右隣から回して埋める)
534
+ export function signatureOf(set) {
535
+ const raw = new Float64Array(SIG_BINS).fill(Infinity);
536
+ for (let i = 0; i < set.length; i++) {
537
+ const h = set[i];
538
+ const b = h >>> 26;
539
+ const v = h & 0x3ffffff;
540
+ if (v < raw[b]) raw[b] = v;
541
+ }
542
+ // 26ビットの値+回した距離(最大63)×2^26 は32ビットに収まる
543
+ const sig = new Uint32Array(SIG_BINS);
544
+ for (let i = 0; i < SIG_BINS; i++) {
545
+ let d = 0;
546
+ while (d < SIG_BINS && raw[(i + d) % SIG_BINS] === Infinity) d++;
547
+ sig[i] = d >= SIG_BINS ? 0xffffffff : raw[(i + d) % SIG_BINS] + d * 0x4000000;
548
+ }
549
+ return sig;
550
+ }
551
+
552
+ export function jaccard(a, b) {
553
+ let i = 0;
554
+ let j = 0;
555
+ let inter = 0;
556
+ while (i < a.length && j < b.length) {
557
+ if (a[i] === b[j]) {
558
+ inter++;
559
+ i++;
560
+ j++;
561
+ } else if (a[i] < b[j]) i++;
562
+ else j++;
563
+ }
564
+ const uni = a.length + b.length - inter;
565
+ return uni ? inter / uni : 0;
566
+ }
567
+
568
+ export function lineKeys(masked) {
569
+ const out = new Map();
570
+ masked.split('\n').forEach((l) => {
571
+ const t = l.trim();
572
+ const k = t.normalize('NFKC').toLowerCase().replace(/[\s#>*_\-|`]+/g, '');
573
+ if (k.length >= 4 && !out.has(k)) out.set(k, t.replace(/\s+/g, ' '));
574
+ });
575
+ return out;
576
+ }
577
+
578
+ export function clip(s, n) {
579
+ return s.length > n ? s.slice(0, n - 1) + '…' : s;
580
+ }
581
+
582
+ /* ---------- リンクの解決 ---------- */
583
+
584
+ export function buildIndex(docs) {
585
+ const byPath = new Map();
586
+ const byNoExt = new Map();
587
+ const byName = new Map();
588
+ const add = (map, k, i) => {
589
+ if (!k) return;
590
+ const a = map.get(k);
591
+ if (!a) map.set(k, [i]);
592
+ else if (a.indexOf(i) === -1) a.push(i);
593
+ };
594
+ docs.forEach((d, i) => {
595
+ const lp = d.path.toLowerCase();
596
+ if (!byPath.has(lp)) byPath.set(lp, i);
597
+ add(byNoExt, lp.replace(/\.[^./]+$/, ''), i);
598
+ add(byName, nameKey(stemOf(d.path)), i);
599
+ add(byName, nameKey(d.title), i);
600
+ });
601
+ return { byPath, byNoExt, byName };
602
+ }
603
+
604
+ export function pickNear(cands, docs, fromDir) {
605
+ if (!cands || !cands.length) return -1;
606
+ if (cands.length === 1) return cands[0];
607
+ const same = cands.filter((i) => dirOf(docs[i].path) === fromDir);
608
+ return same.length ? same[0] : cands[0];
609
+ }
610
+
611
+ /** リンク1本の行き先。{ to: 文書番号 } / { broken: true } / null(外部・添付・アンカーなど対象外) */
612
+ export function resolveLink(link, from, docs, idx) {
613
+ const fromDir = dirOf(docs[from].path);
614
+ let t = String(link.target || '').trim();
615
+ if (!t) return null;
616
+ if (link.kind === 'wiki') {
617
+ const hit = pickNear(idx.byName.get(nameKey(t)), docs, fromDir);
618
+ if (hit >= 0) return { to: hit };
619
+ const p = normPath(fromDir + '/' + t).toLowerCase();
620
+ const hit2 = idx.byPath.has(p) ? idx.byPath.get(p) : pickNear(idx.byNoExt.get(p), docs, fromDir);
621
+ return hit2 >= 0 ? { to: hit2 } : { broken: true };
622
+ }
623
+ if (/^(mailto|tel|javascript|data|ftp|file|sms):/i.test(t)) return null;
624
+ const external = /^[a-z][a-z0-9+.-]*:/i.test(t) || t.slice(0, 2) === '//';
625
+ t = t.replace(/[?#].*$/, '');
626
+ if (!t) return null;
627
+ if (external) {
628
+ // Notion(…/Page-Title-<32桁>)・Confluence(…/Page+Title)のURLは末尾のページ名で照合する。外れても切れとはしない
629
+ const segs = t.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]*/i, '').replace(/^\/\/[^/]*/, '').split('/').filter(Boolean);
630
+ if (!segs.length) return null;
631
+ const last = safeDecode(segs[segs.length - 1].replace(/\+/g, ' ')).replace(/\.(html?|md)$/i, '');
632
+ const hit = pickNear(idx.byName.get(nameKey(last)), docs, fromDir);
633
+ return hit >= 0 ? { to: hit } : null;
634
+ }
635
+ const p = safeDecode(t);
636
+ const target = (p[0] === '/' ? normPath(p) : normPath(fromDir + '/' + p)).toLowerCase();
637
+ if (idx.byPath.has(target)) return { to: idx.byPath.get(target) };
638
+ const hasExt = /\.[a-z0-9]{1,8}$/i.test(baseOf(target));
639
+ const docExt = DOC_EXT_RE.test(target);
640
+ if (hasExt && !docExt) return null;
641
+ const noExt = docExt ? target.replace(/\.[^./]+$/, '') : target;
642
+ let hit = pickNear(idx.byNoExt.get(noExt), docs, fromDir);
643
+ if (hit < 0) hit = pickNear(idx.byName.get(nameKey(stemOf(target))), docs, fromDir);
644
+ if (hit >= 0) return { to: hit };
645
+ return docExt ? { broken: true } : null;
646
+ }
647
+
648
+ /* ---------- まとめ ---------- */
649
+
650
+ // 該当箇所の前後(HTML はタグを隠した本文から取り、空白をまとめる)
651
+ export function snippetAround(text, starts, pos, len, replacement, collapse) {
652
+ const li = lineIndexAt(starts, pos);
653
+ const ls = starts[li];
654
+ const le = li + 1 < starts.length ? starts[li + 1] - 1 : text.length;
655
+ const line = text.slice(ls, le);
656
+ const col = pos - ls;
657
+ const sq = (x) => (collapse ? x.replace(/\s+/g, ' ') : x);
658
+ let pre = sq(line.slice(0, col));
659
+ let post = sq(line.slice(col + len));
660
+ if (pre.length > 30) pre = '…' + pre.slice(-30);
661
+ if (post.length > 30) post = post.slice(0, 30) + '…';
662
+ const word = line.slice(col, col + len);
663
+ return { line: li + 1, col: col + 1, before: (pre + word + post).trim(), after: (pre + replacement + post).trim() };
664
+ }
665
+
666
+ /**
667
+ * 社内文書の検査。files は [{ path, text }]。
668
+ * opts: threshold(0.5〜1・既定0.8)/ dictionary(辞書の本文・既定 DEFAULT_DICT)/ autoChoon(既定 true)/
669
+ * entryPages(index・README などを孤立に数えない・既定 true)/ lang('ja'|'en')
670
+ */
671
+ export function lintDocs(files, opts) {
672
+ const o = opts || {};
673
+ const lang = o.lang === 'en' ? 'en' : 'ja';
674
+ const M = wdlMsg(lang);
675
+ let threshold = o.threshold === undefined ? 0.8 : Number(o.threshold);
676
+ if (!(threshold >= 0.5 && threshold <= 1)) threshold = 0.8;
677
+ const list = Array.isArray(files) ? files : [];
678
+ if (list.length > LIMITS.files) {
679
+ const err = new Error(lang === 'en' ? `Too many files (up to ${LIMITS.files})` : `ファイルが多すぎます(${LIMITS.files}件まで)`);
680
+ err.code = 'WDL_TOO_MANY';
681
+ throw err;
682
+ }
683
+ const docs = [];
684
+ const seenPath = new Set();
685
+ list.forEach((f) => {
686
+ if (!f || typeof f.path !== 'string') return;
687
+ const d = parseDoc(f.path, f.text);
688
+ if (!d.path || seenPath.has(d.path.toLowerCase())) return;
689
+ seenPath.add(d.path.toLowerCase());
690
+ docs.push(d);
691
+ });
692
+ docs.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
693
+ const idx = buildIndex(docs);
694
+
695
+ // リンク
696
+ const inbound = docs.map(() => new Set());
697
+ const outbound = docs.map(() => new Set());
698
+ const broken = [];
699
+ docs.forEach((d, i) => {
700
+ d.links.forEach((l) => {
701
+ const r = resolveLink(l, i, docs, idx);
702
+ if (!r) return;
703
+ if (r.broken) {
704
+ const li = lineIndexAt(d.starts, l.pos);
705
+ broken.push({ path: d.path, line: li + 1, target: l.target, fix: M.brokenFix(l.target) });
706
+ } else if (r.to !== i) {
707
+ inbound[r.to].add(i);
708
+ outbound[i].add(r.to);
709
+ }
710
+ });
711
+ });
712
+
713
+ // 孤立
714
+ const entryOn = o.entryPages !== false;
715
+ const isEntry = (d) => entryOn && ENTRY_STEMS.indexOf(nameKey(stemOf(d.path))) !== -1;
716
+ const entries = docs.filter(isEntry).map((d) => d.path);
717
+ const orphans = [];
718
+ if (docs.length >= 2) {
719
+ docs.forEach((d, i) => {
720
+ if (inbound[i].size || isEntry(d)) return;
721
+ const parent = orphanParent(d, i, docs);
722
+ let addLine = null;
723
+ let fix = M.orphanFixNone;
724
+ if (parent >= 0) {
725
+ const pd = docs[parent];
726
+ const rel = relPath(dirOf(pd.path), d.path, pd.type);
727
+ addLine = pd.type === 'html' ? `<li><a href="${rel}">${d.title.replace(/&/g, '&amp;').replace(/</g, '&lt;')}</a></li>` : pd.type === 'md' ? `- [${d.title.replace(/([[\]])/g, '\\$1')}](${rel})` : d.path;
728
+ fix = M.orphanFix(pd.title, pd.path, addLine);
729
+ }
730
+ orphans.push({ path: d.path, title: d.title, outLinks: outbound[i].size, parent: parent >= 0 ? docs[parent].path : null, addLine, fix });
731
+ });
732
+ }
733
+
734
+ // 重複
735
+ const short = [];
736
+ const sets = docs.map((d) => {
737
+ const norm = normForDup(d.masked);
738
+ d.dupLen = norm.length;
739
+ if (norm.length < MIN_DUP_CHARS) {
740
+ short.push(d.path);
741
+ return null;
742
+ }
743
+ return shingleHashes(norm);
744
+ });
745
+ const sigs = sets.map((s) => (s ? signatureOf(s) : null));
746
+ const pairs = [];
747
+ // 署名での推定値が「しきい値−0.25」に届かない組は正確な計算を省く(届かないと決まった時点で打ち切る)
748
+ const need = Math.max(1, Math.ceil((threshold - 0.25) * SIG_BINS));
749
+ const miss = SIG_BINS - need;
750
+ for (let i = 0; i < docs.length; i++) {
751
+ const a = sigs[i];
752
+ if (!a) continue;
753
+ for (let j = i + 1; j < docs.length; j++) {
754
+ const b = sigs[j];
755
+ if (!b) continue;
756
+ let ng = 0;
757
+ let k = 0;
758
+ for (; k < SIG_BINS; k++) if (a[k] !== b[k] && ++ng > miss) break;
759
+ if (k < SIG_BINS) continue;
760
+ const sim = jaccard(sets[i], sets[j]);
761
+ if (sim >= threshold) pairs.push({ i, j, sim });
762
+ }
763
+ }
764
+ pairs.sort((x, y) => y.sim - x.sim || (docs[x.i].path < docs[y.i].path ? -1 : 1));
765
+ const duplicates = pairs.slice(0, MAX_PAIRS).map(({ i, j, sim }) => {
766
+ const A = docs[i];
767
+ const B = docs[j];
768
+ let keep = i;
769
+ let why;
770
+ if (inbound[i].size !== inbound[j].size) {
771
+ keep = inbound[i].size > inbound[j].size ? i : j;
772
+ why = M.whyLinks(inbound[keep].size, inbound[keep === i ? j : i].size);
773
+ } else if (A.dupLen !== B.dupLen) {
774
+ keep = A.dupLen > B.dupLen ? i : j;
775
+ why = M.whyLen(docs[keep].dupLen, docs[keep === i ? j : i].dupLen);
776
+ } else why = M.whyPath;
777
+ const drop = keep === i ? j : i;
778
+ const ka = lineKeys(A.masked);
779
+ const kb = lineKeys(B.masked);
780
+ const only = (x, y) => {
781
+ const out = [];
782
+ x.forEach((v, k) => {
783
+ if (!y.has(k) && out.length < 3) out.push(clip(v, 80));
784
+ });
785
+ return out;
786
+ };
787
+ return {
788
+ a: A.path,
789
+ b: B.path,
790
+ aTitle: A.title,
791
+ bTitle: B.title,
792
+ similarity: Math.round(sim * 1000) / 1000,
793
+ keep: docs[keep].path,
794
+ drop: docs[drop].path,
795
+ onlyA: only(ka, kb),
796
+ onlyB: only(kb, ka),
797
+ fix: M.dupFix(docs[keep].title, docs[keep].path, docs[drop].title, docs[drop].path, why),
798
+ };
799
+ });
800
+
801
+ // 表記ゆれ
802
+ const dict = parseDictionary(o.dictionary === undefined ? (lang === 'en' ? DEFAULT_DICT_EN : DEFAULT_DICT) : o.dictionary);
803
+ const terms = [];
804
+ const dictForms = new Set();
805
+ dict.rules.forEach((r) => {
806
+ dictForms.add(r.preferred);
807
+ r.variants.forEach((v) => dictForms.add(v));
808
+ const { counts, hits } = scanRule(r, docs);
809
+ if (hits.length) terms.push({ preferred: r.preferred, variants: r.variants.filter((v) => counts[v] > 0), source: 'dict', counts: pickCounts(counts), rawHits: hits });
810
+ });
811
+ if (o.autoChoon !== false) {
812
+ autoChoonGroups(docs, dictForms).forEach((g) => terms.push({ preferred: g.preferred, variants: g.variants, source: 'auto', counts: g.counts, rawHits: g.hits }));
813
+ }
814
+ let termHits = 0;
815
+ terms.forEach((t) => {
816
+ termHits += t.rawHits.length;
817
+ t.total = t.rawHits.length;
818
+ t.hits = t.rawHits.slice(0, MAX_HITS_PER_TERM).map((h) => {
819
+ const d = docs[h.doc];
820
+ const sn = d.type === 'html' ? snippetAround(d.masked, d.starts, h.pos, h.form.length, t.preferred, true) : snippetAround(d.text, d.starts, h.pos, h.form.length, t.preferred, false);
821
+ return { path: d.path, line: sn.line, col: sn.col, form: h.form, before: sn.before, after: sn.after };
822
+ });
823
+ delete t.rawHits;
824
+ });
825
+ terms.sort((a, b) => (a.source === b.source ? b.total - a.total : a.source === 'dict' ? -1 : 1));
826
+
827
+ let links = 0;
828
+ inbound.forEach((s) => (links += s.size));
829
+ return {
830
+ lang,
831
+ threshold,
832
+ stats: {
833
+ docs: docs.length,
834
+ md: docs.filter((d) => d.type === 'md').length,
835
+ html: docs.filter((d) => d.type === 'html').length,
836
+ txt: docs.filter((d) => d.type === 'txt').length,
837
+ links,
838
+ broken: broken.length,
839
+ duplicatePairs: pairs.length,
840
+ orphans: orphans.length,
841
+ termKinds: terms.length,
842
+ termHits,
843
+ },
844
+ duplicates,
845
+ orphans,
846
+ broken,
847
+ terms,
848
+ entries,
849
+ short,
850
+ dictErrors: dict.errors,
851
+ };
852
+ }
853
+
854
+ export function pickCounts(counts) {
855
+ const out = {};
856
+ Object.keys(counts).forEach((k) => {
857
+ if (counts[k] > 0) out[k] = counts[k];
858
+ });
859
+ return out;
860
+ }
861
+
862
+ // 孤立ページのリンク元の候補: Notion 型の親(同名のページ+同名のフォルダー)→ 同じフォルダー以上の目次ページ
863
+ export function orphanParent(d, i, docs) {
864
+ const dir = dirOf(d.path);
865
+ if (dir) {
866
+ const up = dirOf(dir);
867
+ const key = nameKey(baseOf(dir));
868
+ for (let k = 0; k < docs.length; k++) {
869
+ if (k !== i && dirOf(docs[k].path) === up && nameKey(stemOf(docs[k].path)) === key) return k;
870
+ }
871
+ }
872
+ let cur = dir;
873
+ for (;;) {
874
+ for (let k = 0; k < docs.length; k++) {
875
+ if (k !== i && dirOf(docs[k].path) === cur && ENTRY_STEMS.indexOf(nameKey(stemOf(docs[k].path))) !== -1) return k;
876
+ }
877
+ if (!cur) break;
878
+ cur = dirOf(cur);
879
+ }
880
+ return -1;
881
+ }
882
+
883
+ /** 結果を Markdown のレポート(修正案つき)にする。limit は各節に載せる件数の上限 */
884
+ export function buildReport(r, limitOpt) {
885
+ const M = wdlMsg(r.lang);
886
+ const limit = limitOpt || 200;
887
+ const L = [];
888
+ const s = r.stats;
889
+ L.push(`# ${M.title}`, '', M.target(s), '');
890
+ L.push(`- ${M.sumDup(s.duplicatePairs)}`, `- ${M.sumOrphan(s.orphans)}`, `- ${M.sumTerm(s.termKinds, s.termHits)}`, `- ${M.sumBroken(s.broken)}`, '');
891
+ r.dictErrors.forEach((e) => L.push(`> ${M.dictErr(e.line, e.text)}`));
892
+ if (r.dictErrors.length) L.push('');
893
+ const more = (n) => {
894
+ if (n > 0) L.push('', M.more(n));
895
+ };
896
+
897
+ L.push(`## 1. ${M.hDup(r.threshold)}`, '');
898
+ if (!r.duplicates.length) L.push(M.none);
899
+ r.duplicates.slice(0, limit).forEach((p) => {
900
+ L.push(`### ${M.dupHead(p.a, p.b, Math.round(p.similarity * 100))}`);
901
+ L.push(`- ${p.fix}`);
902
+ if (p.onlyA.length) L.push(`- ${M.onlyIn(p.a)}: ${p.onlyA.map(M.quote).join(' ')}`);
903
+ if (p.onlyB.length) L.push(`- ${M.onlyIn(p.b)}: ${p.onlyB.map(M.quote).join(' ')}`);
904
+ L.push('');
905
+ });
906
+ more(s.duplicatePairs - Math.min(limit, r.duplicates.length));
907
+
908
+ L.push('', `## 2. ${M.hOrphan}`, '');
909
+ if (!r.orphans.length) L.push(M.none);
910
+ r.orphans.slice(0, limit).forEach((x) => L.push(`- ${M.orphanItem(x.path, x.title, x.fix)}`));
911
+ more(r.orphans.length - limit);
912
+ if (r.entries.length) L.push('', M.entries(r.entries.slice(0, 20)));
913
+
914
+ L.push('', `## 3. ${M.hTerm}`, '');
915
+ if (!r.terms.length) L.push(M.none);
916
+ let shown = 0;
917
+ r.terms.forEach((t) => {
918
+ L.push(`### ${M.termHead(t.preferred, t.variants, t.source)}`, '', M.termCounts(t.counts), '');
919
+ t.hits.forEach((h) => {
920
+ if (shown++ >= limit * 5) return;
921
+ L.push(`- \`${h.path}\` ${M.line(h.line)}: ${h.before} → ${h.after}`);
922
+ });
923
+ if (t.total > t.hits.length) L.push(`- ${M.more(t.total - t.hits.length)}`);
924
+ L.push('');
925
+ });
926
+
927
+ L.push(`## 4. ${M.hBroken}`, '');
928
+ if (!r.broken.length) L.push(M.none);
929
+ r.broken.slice(0, limit).forEach((b) => L.push(`- \`${b.path}\` ${M.line(b.line)}: ${b.fix}`));
930
+ more(r.broken.length - limit);
931
+ if (r.short.length) L.push('', M.short(r.short.length));
932
+ return L.join('\n').replace(/\n{3,}/g, '\n\n') + '\n';
933
+ }
934
+
935
+ /* ==================== ここまで検査コア ==================== */
936
+
937
+ export class WikiDocLintError extends Error {}
938
+
939
+ const LANGS = ['ja', 'en'];
940
+
941
+ // ディレクトリを辿って文書のファイルだけを集める(隠しフォルダー・node_modules は飛ばす)
942
+ async function collectFiles(root, out, skipped) {
943
+ const st = await stat(root);
944
+ if (st.isFile()) {
945
+ out.push({ abs: root, rel: root.split(sep).pop(), size: st.size, base: null });
946
+ return;
947
+ }
948
+ if (!st.isDirectory()) return;
949
+ const walk = async (dir) => {
950
+ const ents = await readdir(dir, { withFileTypes: true });
951
+ ents.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
952
+ for (const e of ents) {
953
+ if (e.name[0] === '.' || e.name === 'node_modules') continue;
954
+ const abs = join(dir, e.name);
955
+ if (e.isDirectory()) await walk(abs);
956
+ else if (e.isFile()) {
957
+ const rel = relative(root, abs).split(sep).join('/');
958
+ if (!isDocName(rel)) {
959
+ skipped.count++;
960
+ continue;
961
+ }
962
+ out.push({ abs, rel, size: (await stat(abs)).size });
963
+ if (out.length > LIMITS.files) throw new WikiDocLintError(`ファイルが多すぎます(${LIMITS.files}件まで): ${root}`);
964
+ }
965
+ }
966
+ };
967
+ await walk(root);
968
+ }
969
+
970
+ /**
971
+ * MCP の入口。paths(絶対パスのファイル・フォルダー)か files([{ path, text }])のどちらかを渡す。
972
+ * format: 'both'(既定・結果のJSONとレポート)/ 'json' / 'markdown'
973
+ */
974
+ export async function wikiDocLintTool(opts = {}) {
975
+ const lang = opts.lang === undefined ? 'ja' : opts.lang;
976
+ if (LANGS.indexOf(lang) === -1) throw new WikiDocLintError(`lang は ja か en: ${opts.lang}`);
977
+ const hasPaths = opts.paths !== undefined;
978
+ const hasFiles = opts.files !== undefined;
979
+ if (hasPaths === hasFiles) throw new WikiDocLintError('paths(ファイル・フォルダーの絶対パス)か files({ path, text } の配列)のどちらか一方を渡してください');
980
+ const format = opts.format === undefined ? 'both' : opts.format;
981
+ if (['both', 'json', 'markdown'].indexOf(format) === -1) throw new WikiDocLintError(`format は both / json / markdown のどれか: ${opts.format}`);
982
+ if (opts.threshold !== undefined && !(typeof opts.threshold === 'number' && opts.threshold >= 0.5 && opts.threshold <= 1)) {
983
+ throw new WikiDocLintError(`threshold は 0.5〜1 の数: ${opts.threshold}`);
984
+ }
985
+ if (opts.dictionary !== undefined && typeof opts.dictionary !== 'string') throw new WikiDocLintError('dictionary は文字列(1行に1つ「揃える表記 ← ゆれ」)');
986
+
987
+ const files = [];
988
+ let skipped = 0;
989
+ const encodings = {};
990
+ if (hasPaths) {
991
+ const list = Array.isArray(opts.paths) ? opts.paths : [opts.paths];
992
+ if (!list.length) throw new WikiDocLintError('paths が空です');
993
+ const found = [];
994
+ const sk = { count: 0 };
995
+ for (const p of list) {
996
+ if (typeof p !== 'string' || !isAbsolute(p)) throw new WikiDocLintError(`paths は絶対パスで指定してください: ${p}`);
997
+ const before = found.length;
998
+ await collectFiles(p, found, sk);
999
+ // 複数のフォルダーを渡したときに相対パスが衝突しないよう、2つ目以降はフォルダー名を前に付ける
1000
+ if (list.length > 1) {
1001
+ const name = p.replace(/[\\/]+$/, '').split(/[\\/]/).pop();
1002
+ for (let i = before; i < found.length; i++) found[i].rel = name + '/' + found[i].rel;
1003
+ }
1004
+ }
1005
+ skipped = sk.count;
1006
+ if (found.length > LIMITS.files) throw new WikiDocLintError(`ファイルが多すぎます(${LIMITS.files}件まで)`);
1007
+ let total = 0;
1008
+ for (const f of found) {
1009
+ if (!isDocName(f.rel) || f.size > LIMITS.fileBytes) {
1010
+ skipped++;
1011
+ continue;
1012
+ }
1013
+ total += f.size;
1014
+ if (total > LIMITS.totalBytes) throw new WikiDocLintError(`ファイルの合計が大きすぎます(${LIMITS.totalBytes / 1024 / 1024}MB まで)`);
1015
+ const dec = decodeBytes(new Uint8Array(await readFile(f.abs)));
1016
+ encodings[dec.encoding] = (encodings[dec.encoding] || 0) + 1;
1017
+ files.push({ path: f.rel, text: dec.text });
1018
+ }
1019
+ } else {
1020
+ if (!Array.isArray(opts.files)) throw new WikiDocLintError('files は { path, text } の配列で渡してください');
1021
+ if (opts.files.length > LIMITS.files) throw new WikiDocLintError(`ファイルが多すぎます(${LIMITS.files}件まで)`);
1022
+ let total = 0;
1023
+ for (const f of opts.files) {
1024
+ if (!f || typeof f.path !== 'string' || typeof f.text !== 'string') throw new WikiDocLintError('files の各要素は { path: 文字列, text: 文字列 }');
1025
+ if (!isDocName(f.path)) {
1026
+ skipped++;
1027
+ continue;
1028
+ }
1029
+ total += Buffer.byteLength(f.text);
1030
+ if (total > LIMITS.totalBytes) throw new WikiDocLintError(`text の合計が大きすぎます(${LIMITS.totalBytes / 1024 / 1024}MB まで)`);
1031
+ files.push({ path: f.path, text: f.text });
1032
+ }
1033
+ }
1034
+ if (!files.length) throw new WikiDocLintError('Markdown・HTML・テキスト(.md .markdown .html .htm .txt)のファイルが見つかりません');
1035
+
1036
+ // 渡した辞書は既定の辞書に足す(useDefaultDictionary: false なら渡した辞書だけを使う)
1037
+ const base = opts.useDefaultDictionary === false ? '' : lang === 'en' ? DEFAULT_DICT_EN : DEFAULT_DICT;
1038
+ const dictionary = opts.dictionary === undefined ? base : base ? base + '\n' + opts.dictionary : opts.dictionary;
1039
+ const result = lintDocs(files, {
1040
+ threshold: opts.threshold,
1041
+ dictionary,
1042
+ autoChoon: opts.autoChoon,
1043
+ entryPages: opts.entryPages,
1044
+ lang,
1045
+ });
1046
+ const out = { stats: { ...result.stats, skipped } };
1047
+ if (hasPaths) out.encodings = encodings;
1048
+ if (format !== 'markdown') {
1049
+ out.threshold = result.threshold;
1050
+ out.duplicates = result.duplicates;
1051
+ out.orphans = result.orphans;
1052
+ out.terms = result.terms;
1053
+ out.broken = result.broken;
1054
+ out.entries = result.entries;
1055
+ out.short = result.short;
1056
+ if (result.dictErrors.length) out.dictErrors = result.dictErrors;
1057
+ }
1058
+ if (format !== 'json') out.report = buildReport(result);
1059
+ return out;
1060
+ }