@first-ch/tools-mcp 0.30.0 → 0.32.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.md +10 -2
- package/html-md.mjs +1226 -0
- package/package.json +17 -3
- package/server.mjs +87 -0
- package/wareki.mjs +669 -0
package/html-md.mjs
ADDED
|
@@ -0,0 +1,1226 @@
|
|
|
1
|
+
// HTML → Markdown 変換(tools.first-ch.com/html-md/ と同一ロジック)
|
|
2
|
+
//
|
|
3
|
+
// 「変換コア」ブロックは site 側の site/html-md/app.js と同一の実装。
|
|
4
|
+
// 2箇所ルール: 片方を直したらもう片方も同じ内容で直す(site側が正本)。
|
|
5
|
+
// site側はIIFEの中で2字下げ・こちらは行頭に export が付くだけの差なので、コメントで
|
|
6
|
+
// 挟んだ範囲を sed で切り出して diff すれば同期漏れを機械確認できる(csv-json.mjs と同じ手順。
|
|
7
|
+
// 手順そのものは FirstCHTools の CLAUDE.md を参照)。追記は必ずコアの終わりより下へ。
|
|
8
|
+
// DOM(DOMParser / innerHTML)に依存しないので、Node でもブラウザでも同じ結果になる。
|
|
9
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
10
|
+
import { basename } from 'node:path';
|
|
11
|
+
|
|
12
|
+
/* ==================== ここから変換コア(site / MCP で同一) ==================== */
|
|
13
|
+
|
|
14
|
+
/* HTMLの構文解析からMarkdownの組み立てまで。DOM(DOMParser / innerHTML)には依存しない——
|
|
15
|
+
* ブラウザとNode.jsの両方で同じ結果を出すため、字句解析も木の組み立ても自前で行う。
|
|
16
|
+
* 外部への通信は一切しない(相対URLの解決も渡された baseUrl の文字列計算だけ)。 */
|
|
17
|
+
|
|
18
|
+
// 閉じタグを持たない要素
|
|
19
|
+
const HM_VOID_TAGS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
20
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
|
21
|
+
// 中身をタグとして読まない要素(終了タグまで生のまま取る)
|
|
22
|
+
const HM_RAW_TEXT_TAGS = ['script', 'style', 'textarea', 'title'];
|
|
23
|
+
// 中身ごと捨てる要素(本文ではない)
|
|
24
|
+
const HM_DROP_TAGS = ['script', 'style', 'noscript', 'template', 'head', 'meta',
|
|
25
|
+
'link', 'base', 'param', 'source', 'track', 'col', 'colgroup', 'select', 'option',
|
|
26
|
+
'optgroup', 'datalist', 'textarea'];
|
|
27
|
+
// Markdownに対応が無く、HTMLのまま残すしかない要素(unknown オプションで挙動を選ぶ)
|
|
28
|
+
const HM_MEDIA_TAGS = ['svg', 'math', 'iframe', 'video', 'audio', 'object', 'embed', 'canvas'];
|
|
29
|
+
// 「本文だけ」で落とす要素
|
|
30
|
+
const HM_CHROME_TAGS = ['nav', 'header', 'footer', 'aside', 'form', 'menu'];
|
|
31
|
+
// ブロック要素(段落の切れ目になる)
|
|
32
|
+
const HM_BLOCK_TAGS = ['address', 'article', 'aside', 'blockquote', 'caption', 'dd',
|
|
33
|
+
'details', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form',
|
|
34
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'legend', 'li', 'main',
|
|
35
|
+
'menu', 'nav', 'ol', 'p', 'pre', 'section', 'summary', 'table', 'tbody', 'td',
|
|
36
|
+
'tfoot', 'th', 'thead', 'tr', 'ul'];
|
|
37
|
+
// 印だけ外して中身をそのまま流す要素
|
|
38
|
+
const HM_TRANSPARENT_TAGS = ['span', 'font', 'small', 'big', 'cite', 'time', 'data',
|
|
39
|
+
'label', 'picture', 'bdi', 'bdo', 'ruby', 'rb', 'nobr', 'center', 'acronym', 'output'];
|
|
40
|
+
// HTMLのまま残すインライン要素(Markdownに記法が無い)
|
|
41
|
+
const HM_KEEP_INLINE_TAGS = ['sup', 'sub', 'mark', 'ins', 'u', 'kbd', 'samp', 'var',
|
|
42
|
+
'abbr', 'q', 'dfn', 'tt', 'rt', 'rp'];
|
|
43
|
+
|
|
44
|
+
export const HM_HEADING_STYLES = ['atx', 'setext'];
|
|
45
|
+
export const HM_BULLETS = ['-', '*', '+'];
|
|
46
|
+
const HM_EMPHASIS = ['*', '_'];
|
|
47
|
+
const HM_STRONG = ['**', '__'];
|
|
48
|
+
export const HM_CODE_BLOCKS = ['fenced', 'indented'];
|
|
49
|
+
const HM_FENCES = ['```', '~~~'];
|
|
50
|
+
const HM_HRS = ['---', '***', '___'];
|
|
51
|
+
export const HM_LINK_STYLES = ['inline', 'reference', 'strip'];
|
|
52
|
+
export const HM_IMAGE_STYLES = ['keep', 'alt', 'drop'];
|
|
53
|
+
export const HM_TABLE_STYLES = ['gfm', 'html'];
|
|
54
|
+
export const HM_UNKNOWN_STYLES = ['keep', 'text', 'drop'];
|
|
55
|
+
export const HM_BR_STYLES = ['spaces', 'backslash', 'html', 'space'];
|
|
56
|
+
|
|
57
|
+
export const HM_DEFAULTS = {
|
|
58
|
+
headings: 'atx', // 見出しの書き方(atx = #・setext = 下線)
|
|
59
|
+
bullet: '-', // 箇条書きの印
|
|
60
|
+
emphasis: '*', // 強調(em)の印
|
|
61
|
+
strong: '**', // 太字(strong)の印
|
|
62
|
+
codeBlock: 'fenced', // コードブロックの書き方
|
|
63
|
+
fence: '```', // フェンスの文字
|
|
64
|
+
hr: '---', // 水平線
|
|
65
|
+
links: 'inline', // リンクの書き方
|
|
66
|
+
images: 'keep', // 画像の扱い
|
|
67
|
+
tables: 'gfm', // 表の書き方
|
|
68
|
+
unknown: 'keep', // Markdownに無い要素の扱い
|
|
69
|
+
br: 'spaces', // <br> の書き方
|
|
70
|
+
baseUrl: '', // 相対URLを絶対URLへ直すときの基準
|
|
71
|
+
mainOnly: false, // main / article だけを取り出す
|
|
72
|
+
escape: true, // Markdownの記号をエスケープする
|
|
73
|
+
gfm: true, // 表・取り消し線・タスクリスト(GitHub Flavored Markdown)
|
|
74
|
+
padTables: true, // 表の桁を揃える
|
|
75
|
+
collapseBlankLines: true, // 空行を2行以上続けない
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/* -------------------- 文字参照 -------------------- */
|
|
79
|
+
|
|
80
|
+
const HM_ENTITIES = {
|
|
81
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0', ensp: '\u2002',
|
|
82
|
+
emsp: '\u2003', thinsp: '\u2009', zwnj: '\u200c', zwj: '\u200d', lrm: '\u200e',
|
|
83
|
+
rlm: '\u200f', ndash: '–', mdash: '—', horbar: '―', lsquo: '‘', rsquo: '’',
|
|
84
|
+
sbquo: '‚', ldquo: '“', rdquo: '”', bdquo: '„', dagger: '†', Dagger: '‡',
|
|
85
|
+
permil: '‰', lsaquo: '‹', rsaquo: '›', oline: '‾', frasl: '⁄', euro: '€',
|
|
86
|
+
cent: '¢', pound: '£', yen: '¥', curren: '¤', sect: '§', copy: '©', reg: '®',
|
|
87
|
+
trade: '™', deg: '°', plusmn: '±', sup1: '¹', sup2: '²', sup3: '³', micro: 'µ',
|
|
88
|
+
para: '¶', middot: '·', cedil: '¸', ordm: 'º', ordf: 'ª', laquo: '«', raquo: '»',
|
|
89
|
+
frac14: '¼', frac12: '½', frac34: '¾', iquest: '¿', iexcl: '¡', brvbar: '¦',
|
|
90
|
+
uml: '¨', not: '¬', shy: '\u00ad', macr: '¯', acute: '´', times: '×', divide: '÷',
|
|
91
|
+
minus: '−', lowast: '∗', radic: '√', prop: '∝', infin: '∞', ne: '≠', le: '≤',
|
|
92
|
+
ge: '≥', asymp: '≈', equiv: '≡', sum: '∑', prod: '∏', int: '∫', part: '∂',
|
|
93
|
+
nabla: '∇', isin: '∈', notin: '∉', cap: '∩', cup: '∪', sub: '⊂', sup: '⊃',
|
|
94
|
+
sube: '⊆', supe: '⊇', oplus: '⊕', otimes: '⊗', perp: '⊥', sdot: '⋅', bull: '•',
|
|
95
|
+
hellip: '…', prime: '′', Prime: '″', larr: '←', uarr: '↑', rarr: '→', darr: '↓',
|
|
96
|
+
harr: '↔', crarr: '↵', lArr: '⇐', uArr: '⇑', rArr: '⇒', dArr: '⇓', hArr: '⇔',
|
|
97
|
+
loz: '◊', spades: '♠', clubs: '♣', hearts: '♥', diams: '♦', star: '☆',
|
|
98
|
+
check: '✓', cross: '✗', alpha: 'α', beta: 'β', gamma: 'γ', delta: 'δ',
|
|
99
|
+
epsilon: 'ε', zeta: 'ζ', eta: 'η', theta: 'θ', iota: 'ι', kappa: 'κ',
|
|
100
|
+
lambda: 'λ', mu: 'μ', nu: 'ν', xi: 'ξ', omicron: 'ο', pi: 'π', rho: 'ρ',
|
|
101
|
+
sigmaf: 'ς', sigma: 'σ', tau: 'τ', upsilon: 'υ', phi: 'φ', chi: 'χ', psi: 'ψ',
|
|
102
|
+
omega: 'ω', Alpha: 'Α', Beta: 'Β', Gamma: 'Γ', Delta: 'Δ', Epsilon: 'Ε',
|
|
103
|
+
Zeta: 'Ζ', Eta: 'Η', Theta: 'Θ', Iota: 'Ι', Kappa: 'Κ', Lambda: 'Λ', Mu: 'Μ',
|
|
104
|
+
Nu: 'Ν', Xi: 'Ξ', Omicron: 'Ο', Pi: 'Π', Rho: 'Ρ', Sigma: 'Σ', Tau: 'Τ',
|
|
105
|
+
Upsilon: 'Υ', Phi: 'Φ', Chi: 'Χ', Psi: 'Ψ', Omega: 'Ω', agrave: 'à',
|
|
106
|
+
aacute: 'á', acirc: 'â', atilde: 'ã', auml: 'ä', aring: 'å', aelig: 'æ',
|
|
107
|
+
ccedil: 'ç', egrave: 'è', eacute: 'é', ecirc: 'ê', euml: 'ë', igrave: 'ì',
|
|
108
|
+
iacute: 'í', icirc: 'î', iuml: 'ï', eth: 'ð', ntilde: 'ñ', ograve: 'ò',
|
|
109
|
+
oacute: 'ó', ocirc: 'ô', otilde: 'õ', ouml: 'ö', oslash: 'ø', ugrave: 'ù',
|
|
110
|
+
uacute: 'ú', ucirc: 'û', uuml: 'ü', yacute: 'ý', thorn: 'þ', yuml: 'ÿ',
|
|
111
|
+
szlig: 'ß', Agrave: 'À', Aacute: 'Á', Acirc: 'Â', Atilde: 'Ã', Auml: 'Ä',
|
|
112
|
+
Aring: 'Å', AElig: 'Æ', Ccedil: 'Ç', Egrave: 'È', Eacute: 'É', Ecirc: 'Ê',
|
|
113
|
+
Euml: 'Ë', Igrave: 'Ì', Iacute: 'Í', Icirc: 'Î', Iuml: 'Ï', ETH: 'Ð',
|
|
114
|
+
Ntilde: 'Ñ', Ograve: 'Ò', Oacute: 'Ó', Ocirc: 'Ô', Otilde: 'Õ', Ouml: 'Ö',
|
|
115
|
+
Oslash: 'Ø', Ugrave: 'Ù', Uacute: 'Ú', Ucirc: 'Û', Uuml: 'Ü', Yacute: 'Ý',
|
|
116
|
+
THORN: 'Þ', Yuml: 'Ÿ', OElig: 'Œ', oelig: 'œ', Scaron: 'Š', scaron: 'š',
|
|
117
|
+
fnof: 'ƒ', circ: 'ˆ', tilde: '˜',
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// Windows-1252 として書かれた数値参照(“ など)の読み替え表
|
|
121
|
+
const HM_CP1252 = {
|
|
122
|
+
128: '€', 130: '‚', 131: 'ƒ', 132: '„', 133: '…', 134: '†', 135: '‡', 136: 'ˆ',
|
|
123
|
+
137: '‰', 138: 'Š', 139: '‹', 140: 'Œ', 142: 'Ž', 145: '‘', 146: '’', 147: '“',
|
|
124
|
+
148: '”', 149: '•', 150: '–', 151: '—', 152: '˜', 153: '™', 154: 'š', 155: '›',
|
|
125
|
+
156: 'œ', 158: 'ž', 159: 'Ÿ',
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** 数値文字参照を1文字へ */
|
|
129
|
+
function hmCodePoint(num) {
|
|
130
|
+
if (!Number.isFinite(num) || num <= 0) return '�';
|
|
131
|
+
if (HM_CP1252[num]) return HM_CP1252[num];
|
|
132
|
+
if (num > 0x10ffff || (num >= 0xd800 && num <= 0xdfff)) return '�';
|
|
133
|
+
return String.fromCodePoint(num);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 文字参照(& ' ')をほどく */
|
|
137
|
+
export function hmDecodeEntities(text) {
|
|
138
|
+
if (text.indexOf('&') === -1) return text;
|
|
139
|
+
return text.replace(/&(#[Xx][0-9A-Fa-f]+|#\d+|[A-Za-z][A-Za-z0-9]{1,31});?/g, (whole, body) => {
|
|
140
|
+
if (body.charAt(0) === '#') {
|
|
141
|
+
const hex = body.charAt(1) === 'x' || body.charAt(1) === 'X';
|
|
142
|
+
const num = hex ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
143
|
+
return hmCodePoint(num);
|
|
144
|
+
}
|
|
145
|
+
if (Object.prototype.hasOwnProperty.call(HM_ENTITIES, body)) return HM_ENTITIES[body];
|
|
146
|
+
// 末尾に「;」が無い形(&foo)は元の文字列のまま残す
|
|
147
|
+
return whole;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* -------------------- 字句解析 -------------------- */
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* HTMLをトークンの列にする。壊れた引用符で暴走しないよう、
|
|
155
|
+
* 正規表現ではなく1文字ずつ走査する(終端が無ければそこで打ち切って印を残す)。
|
|
156
|
+
*/
|
|
157
|
+
export function hmTokenize(html) {
|
|
158
|
+
const tokens = [];
|
|
159
|
+
const flags = { unterminatedTag: false, unterminatedComment: false };
|
|
160
|
+
const len = html.length;
|
|
161
|
+
let i = 0;
|
|
162
|
+
let textStart = 0;
|
|
163
|
+
|
|
164
|
+
const pushText = (end) => {
|
|
165
|
+
if (end > textStart) tokens.push({ type: 'text', value: html.slice(textStart, end) });
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
while (i < len) {
|
|
169
|
+
const lt = html.indexOf('<', i);
|
|
170
|
+
if (lt === -1) break;
|
|
171
|
+
const next = html.charAt(lt + 1);
|
|
172
|
+
if (next === '!') {
|
|
173
|
+
if (html.substr(lt + 2, 2) === '--') {
|
|
174
|
+
const end = html.indexOf('-->', lt + 4);
|
|
175
|
+
pushText(lt);
|
|
176
|
+
if (end === -1) { flags.unterminatedComment = true; textStart = len; i = len; break; }
|
|
177
|
+
tokens.push({ type: 'comment', value: html.slice(lt + 4, end) });
|
|
178
|
+
i = end + 3;
|
|
179
|
+
textStart = i;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (html.substr(lt + 2, 7).toUpperCase() === '[CDATA[') {
|
|
183
|
+
const end = html.indexOf(']]>', lt + 9);
|
|
184
|
+
pushText(lt);
|
|
185
|
+
const stop = end === -1 ? len : end;
|
|
186
|
+
tokens.push({ type: 'text', value: html.slice(lt + 9, stop) });
|
|
187
|
+
i = end === -1 ? len : end + 3;
|
|
188
|
+
textStart = i;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const end = html.indexOf('>', lt + 2);
|
|
192
|
+
pushText(lt);
|
|
193
|
+
tokens.push({ type: 'doctype' });
|
|
194
|
+
i = end === -1 ? len : end + 1;
|
|
195
|
+
textStart = i;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (next === '/') {
|
|
199
|
+
const m = /^<\/([A-Za-z][A-Za-z0-9:-]*)\s*/.exec(html.slice(lt, lt + 80));
|
|
200
|
+
if (!m) { i = lt + 1; continue; }
|
|
201
|
+
const end = html.indexOf('>', lt + m[0].length - 1);
|
|
202
|
+
pushText(lt);
|
|
203
|
+
tokens.push({ type: 'end', name: m[1].toLowerCase() });
|
|
204
|
+
if (end === -1) { flags.unterminatedTag = true; textStart = len; i = len; break; }
|
|
205
|
+
i = end + 1;
|
|
206
|
+
textStart = i;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (!/[A-Za-z]/.test(next)) { i = lt + 1; continue; }
|
|
210
|
+
|
|
211
|
+
// 開始タグ
|
|
212
|
+
let p = lt + 1;
|
|
213
|
+
while (p < len && /[A-Za-z0-9:-]/.test(html.charAt(p))) p += 1;
|
|
214
|
+
const name = html.slice(lt + 1, p).toLowerCase();
|
|
215
|
+
const attrs = {};
|
|
216
|
+
let selfClosing = false;
|
|
217
|
+
let broken = false;
|
|
218
|
+
for (;;) {
|
|
219
|
+
while (p < len && /\s/.test(html.charAt(p))) p += 1;
|
|
220
|
+
if (p >= len) { broken = true; break; }
|
|
221
|
+
const c = html.charAt(p);
|
|
222
|
+
if (c === '>') { p += 1; break; }
|
|
223
|
+
if (c === '/' && html.charAt(p + 1) === '>') { selfClosing = true; p += 2; break; }
|
|
224
|
+
if (c === '/' || c === '=') { p += 1; continue; }
|
|
225
|
+
const nameStart = p;
|
|
226
|
+
while (p < len && !/[\s/>=]/.test(html.charAt(p))) p += 1;
|
|
227
|
+
const attrName = html.slice(nameStart, p).toLowerCase();
|
|
228
|
+
let value = '';
|
|
229
|
+
let q = p;
|
|
230
|
+
while (q < len && /\s/.test(html.charAt(q))) q += 1;
|
|
231
|
+
if (html.charAt(q) === '=') {
|
|
232
|
+
q += 1;
|
|
233
|
+
while (q < len && /\s/.test(html.charAt(q))) q += 1;
|
|
234
|
+
const quote = html.charAt(q);
|
|
235
|
+
if (quote === '"' || quote === "'") {
|
|
236
|
+
const close = html.indexOf(quote, q + 1);
|
|
237
|
+
if (close === -1) {
|
|
238
|
+
// 引用符が閉じていない。ここで打ち切らないと残り全部を属性値として飲み込む
|
|
239
|
+
value = html.slice(q + 1);
|
|
240
|
+
p = len;
|
|
241
|
+
broken = true;
|
|
242
|
+
attrs[attrName] = hmDecodeEntities(value);
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
value = html.slice(q + 1, close);
|
|
246
|
+
p = close + 1;
|
|
247
|
+
} else {
|
|
248
|
+
const vStart = q;
|
|
249
|
+
while (q < len && !/[\s>]/.test(html.charAt(q))) q += 1;
|
|
250
|
+
value = html.slice(vStart, q);
|
|
251
|
+
p = q;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (attrName) attrs[attrName] = hmDecodeEntities(value);
|
|
255
|
+
}
|
|
256
|
+
pushText(lt);
|
|
257
|
+
if (broken) flags.unterminatedTag = true;
|
|
258
|
+
tokens.push({ type: 'start', name, attrs, selfClosing });
|
|
259
|
+
i = p;
|
|
260
|
+
textStart = i;
|
|
261
|
+
|
|
262
|
+
if (HM_RAW_TEXT_TAGS.indexOf(name) !== -1 && !selfClosing) {
|
|
263
|
+
const lower = html.toLowerCase();
|
|
264
|
+
let close = lower.indexOf('</' + name, i);
|
|
265
|
+
if (close === -1) close = len;
|
|
266
|
+
const raw = html.slice(i, close);
|
|
267
|
+
tokens.push({ type: 'text', value: raw, raw: true });
|
|
268
|
+
tokens.push({ type: 'end', name });
|
|
269
|
+
const gt = html.indexOf('>', close);
|
|
270
|
+
i = close === len ? len : (gt === -1 ? len : gt + 1);
|
|
271
|
+
textStart = i;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
pushText(len);
|
|
275
|
+
return { tokens, flags };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/* -------------------- 木の組み立て -------------------- */
|
|
279
|
+
|
|
280
|
+
// 開始タグが来たときに自動で閉じる組み合わせ
|
|
281
|
+
const HM_AUTO_CLOSE = {
|
|
282
|
+
li: ['li'],
|
|
283
|
+
dt: ['dt', 'dd'],
|
|
284
|
+
dd: ['dt', 'dd'],
|
|
285
|
+
tr: ['td', 'th', 'tr'],
|
|
286
|
+
td: ['td', 'th'],
|
|
287
|
+
th: ['td', 'th'],
|
|
288
|
+
thead: ['td', 'th', 'tr'],
|
|
289
|
+
tbody: ['td', 'th', 'tr', 'thead'],
|
|
290
|
+
tfoot: ['td', 'th', 'tr', 'thead', 'tbody'],
|
|
291
|
+
option: ['option'],
|
|
292
|
+
p: ['p'],
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/** 要素ノード */
|
|
296
|
+
function hmElement(name, attrs) {
|
|
297
|
+
return { type: 'element', name, attrs: attrs || {}, children: [] };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* トークン列から木を作る。閉じ忘れ・閉じすぎのどちらでも落ちない
|
|
302
|
+
* (開いたままの要素は最後にまとめて閉じ、行き場のない終了タグは捨てる)。
|
|
303
|
+
*/
|
|
304
|
+
function hmBuildTree(tokens) {
|
|
305
|
+
const root = hmElement('#root', {});
|
|
306
|
+
const stack = [root];
|
|
307
|
+
const flags = { strayEnd: 0, unclosed: 0 };
|
|
308
|
+
const top = () => stack[stack.length - 1];
|
|
309
|
+
|
|
310
|
+
for (const t of tokens) {
|
|
311
|
+
if (t.type === 'text') {
|
|
312
|
+
top().children.push({ type: 'text', value: t.raw ? t.value : hmDecodeEntities(t.value), raw: !!t.raw });
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
if (t.type === 'comment') {
|
|
316
|
+
top().children.push({ type: 'comment', value: t.value });
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (t.type === 'doctype') continue;
|
|
320
|
+
if (t.type === 'start') {
|
|
321
|
+
const closers = HM_AUTO_CLOSE[t.name];
|
|
322
|
+
if (closers) {
|
|
323
|
+
while (stack.length > 1 && closers.indexOf(top().name) !== -1) stack.pop();
|
|
324
|
+
}
|
|
325
|
+
// <p> はブロック要素の開始で閉じる
|
|
326
|
+
if (HM_BLOCK_TAGS.indexOf(t.name) !== -1 && t.name !== 'p') {
|
|
327
|
+
while (stack.length > 1 && top().name === 'p') stack.pop();
|
|
328
|
+
}
|
|
329
|
+
const el = hmElement(t.name, t.attrs);
|
|
330
|
+
top().children.push(el);
|
|
331
|
+
if (!t.selfClosing && HM_VOID_TAGS.indexOf(t.name) === -1) stack.push(el);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (t.type === 'end') {
|
|
335
|
+
let found = -1;
|
|
336
|
+
for (let k = stack.length - 1; k > 0; k -= 1) {
|
|
337
|
+
if (stack[k].name === t.name) { found = k; break; }
|
|
338
|
+
}
|
|
339
|
+
if (found === -1) { flags.strayEnd += 1; continue; }
|
|
340
|
+
if (found !== stack.length - 1) flags.unclosed += stack.length - 1 - found;
|
|
341
|
+
stack.length = found;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (stack.length > 1) flags.unclosed += stack.length - 1;
|
|
345
|
+
return { root, flags };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** HTMLを木にする(本体・外部通信なし) */
|
|
349
|
+
export function hmParse(html) {
|
|
350
|
+
const { tokens, flags } = hmTokenize(String(html === undefined || html === null ? '' : html));
|
|
351
|
+
const built = hmBuildTree(tokens);
|
|
352
|
+
return { root: built.root, flags: Object.assign({}, flags, built.flags) };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/* -------------------- 木の下ごしらえ -------------------- */
|
|
356
|
+
|
|
357
|
+
/** name の要素を(最初のものだけ)深さ優先で探す */
|
|
358
|
+
function hmFind(node, names) {
|
|
359
|
+
if (node.type === 'element' && names.indexOf(node.name) !== -1) return node;
|
|
360
|
+
if (!node.children) return null;
|
|
361
|
+
for (const child of node.children) {
|
|
362
|
+
const hit = hmFind(child, names);
|
|
363
|
+
if (hit) return hit;
|
|
364
|
+
}
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** 捨てる要素を取り除きながら、落とした数を数える */
|
|
369
|
+
function hmPrune(node, o, counts) {
|
|
370
|
+
if (!node.children) return;
|
|
371
|
+
const kept = [];
|
|
372
|
+
for (const child of node.children) {
|
|
373
|
+
if (child.type === 'comment') { counts.comments += 1; continue; }
|
|
374
|
+
if (child.type !== 'element') { kept.push(child); continue; }
|
|
375
|
+
if (HM_DROP_TAGS.indexOf(child.name) !== -1) {
|
|
376
|
+
if (child.name === 'script') counts.scripts += 1;
|
|
377
|
+
else if (child.name === 'style') counts.styles += 1;
|
|
378
|
+
if (child.name === 'colgroup' || child.name === 'col') continue;
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
if (o.mainOnly && HM_CHROME_TAGS.indexOf(child.name) !== -1) { counts.chrome += 1; continue; }
|
|
382
|
+
if (child.attrs && (child.attrs.hidden !== undefined
|
|
383
|
+
|| /(^|;)\s*display\s*:\s*none/i.test(child.attrs.style || ''))) {
|
|
384
|
+
counts.hidden += 1;
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
hmPrune(child, o, counts);
|
|
388
|
+
kept.push(child);
|
|
389
|
+
}
|
|
390
|
+
node.children = kept;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/* -------------------- 文字幅とエスケープ -------------------- */
|
|
394
|
+
|
|
395
|
+
// 等幅フォントで2桁を占める文字(表の桁揃えに使う)
|
|
396
|
+
const HM_WIDE_RANGES = [
|
|
397
|
+
[0x1100, 0x115f], [0x2e80, 0x303e], [0x3041, 0x33ff], [0x3400, 0x4dbf],
|
|
398
|
+
[0x4e00, 0x9fff], [0xa000, 0xa4cf], [0xac00, 0xd7a3], [0xf900, 0xfaff],
|
|
399
|
+
[0xfe30, 0xfe6f], [0xff00, 0xff60], [0xffe0, 0xffe6], [0x1f300, 0x1f64f],
|
|
400
|
+
[0x1f900, 0x1f9ff], [0x20000, 0x3fffd],
|
|
401
|
+
];
|
|
402
|
+
|
|
403
|
+
/** 表示幅(全角を2桁として数える) */
|
|
404
|
+
export function hmWidth(s) {
|
|
405
|
+
let w = 0;
|
|
406
|
+
for (const ch of String(s)) {
|
|
407
|
+
const cp = ch.codePointAt(0);
|
|
408
|
+
if (cp === 0x200b || cp === 0xfeff || (cp >= 0x0300 && cp <= 0x036f)) continue;
|
|
409
|
+
let wide = false;
|
|
410
|
+
for (const r of HM_WIDE_RANGES) {
|
|
411
|
+
if (cp >= r[0] && cp <= r[1]) { wide = true; break; }
|
|
412
|
+
}
|
|
413
|
+
w += wide ? 2 : 1;
|
|
414
|
+
}
|
|
415
|
+
return w;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** Markdownの記号として読まれてしまう文字を打ち消す */
|
|
419
|
+
export function hmEscapeText(text) {
|
|
420
|
+
return text
|
|
421
|
+
.replace(/([\\*[\]`])/g, '\\$1')
|
|
422
|
+
.replace(/_/g, (m, idx, whole) => {
|
|
423
|
+
// 単語の途中(snake_case)は読み手に意味があるのでそのまま残す
|
|
424
|
+
const before = idx > 0 ? whole.charAt(idx - 1) : '';
|
|
425
|
+
const after = whole.charAt(idx + 1) || '';
|
|
426
|
+
const word = /[A-Za-z0-9]/;
|
|
427
|
+
return word.test(before) && word.test(after) ? '_' : '\\_';
|
|
428
|
+
})
|
|
429
|
+
.replace(/<(?=[A-Za-z/!?])/g, '\\<')
|
|
430
|
+
.replace(/&(?=[A-Za-z#][A-Za-z0-9]*;)/g, '\\&')
|
|
431
|
+
.replace(/~~/g, '\\~\\~');
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** 行頭がブロック記法に見える場合だけ打ち消す */
|
|
435
|
+
function hmEscapeLineStarts(text) {
|
|
436
|
+
return text.split('\n').map((line) => line
|
|
437
|
+
.replace(/^(\s*)(#{1,6})(\s|$)/, '$1\\$2$3')
|
|
438
|
+
.replace(/^(\s*)([-+])(\s)/, '$1\\$2$3')
|
|
439
|
+
.replace(/^(\s*)(\d{1,9})([.)])(\s)/, '$1$2\\$3$4')
|
|
440
|
+
.replace(/^(\s*)(>)/, '$1\\$2')
|
|
441
|
+
.replace(/^(\s*)(={2,}|-{2,})\s*$/, '$1\\$2')).join('\n');
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/* -------------------- URL -------------------- */
|
|
445
|
+
|
|
446
|
+
/** 相対URLを baseUrl で絶対URLにする(base が空なら何もしない) */
|
|
447
|
+
export function hmResolveUrl(url, base) {
|
|
448
|
+
const raw = String(url === undefined || url === null ? '' : url).trim();
|
|
449
|
+
if (!raw || !base) return raw;
|
|
450
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(raw) || raw.charAt(0) === '#') return raw;
|
|
451
|
+
try {
|
|
452
|
+
return new URL(raw, base).href;
|
|
453
|
+
} catch (e) {
|
|
454
|
+
return raw;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Markdownのリンク先として書ける形に整える */
|
|
459
|
+
function hmLinkTarget(url) {
|
|
460
|
+
const raw = String(url || '').trim().replace(/\s+/g, ' ');
|
|
461
|
+
if (raw === '') return '';
|
|
462
|
+
if (/[\s()<>]/.test(raw)) return '<' + raw.replace(/[<>]/g, (c) => encodeURIComponent(c)) + '>';
|
|
463
|
+
return raw;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** タイトル属性を " " で括る */
|
|
467
|
+
function hmTitlePart(title) {
|
|
468
|
+
const t = String(title || '').trim().replace(/\s+/g, ' ');
|
|
469
|
+
if (!t) return '';
|
|
470
|
+
return ' "' + t.replace(/"/g, '\\"') + '"';
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/* -------------------- 生HTMLへの書き戻し -------------------- */
|
|
474
|
+
|
|
475
|
+
const HM_ATTR_ESCAPE = { '&': '&', '<': '<', '>': '>', '"': '"' };
|
|
476
|
+
|
|
477
|
+
/** 残すと決めた要素をHTMLの文字列に戻す */
|
|
478
|
+
export function hmSerialize(node) {
|
|
479
|
+
if (node.type === 'text') return String(node.value).replace(/[&<>]/g, (c) => HM_ATTR_ESCAPE[c]);
|
|
480
|
+
if (node.type === 'comment') return '';
|
|
481
|
+
if (node.type !== 'element') return '';
|
|
482
|
+
const attrs = Object.keys(node.attrs).map((k) => {
|
|
483
|
+
const v = String(node.attrs[k]).replace(/[&<>"]/g, (c) => HM_ATTR_ESCAPE[c]);
|
|
484
|
+
return v === '' ? ' ' + k : ' ' + k + '="' + v + '"';
|
|
485
|
+
}).join('');
|
|
486
|
+
if (HM_VOID_TAGS.indexOf(node.name) !== -1) return '<' + node.name + attrs + '>';
|
|
487
|
+
const inner = (node.children || []).map(hmSerialize).join('');
|
|
488
|
+
return '<' + node.name + attrs + '>' + inner + '</' + node.name + '>';
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** 要素の中の文字だけを取り出す(コードブロック・alt用) */
|
|
492
|
+
function hmTextOf(node) {
|
|
493
|
+
if (node.type === 'text') return String(node.value);
|
|
494
|
+
if (node.type !== 'element') return '';
|
|
495
|
+
if (node.name === 'br') return '\n';
|
|
496
|
+
return (node.children || []).map(hmTextOf).join('');
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/* -------------------- インライン -------------------- */
|
|
500
|
+
|
|
501
|
+
/** インライン要素かどうか(ブロックでなければインラインとして扱う) */
|
|
502
|
+
function hmIsBlock(node) {
|
|
503
|
+
return node.type === 'element' && HM_BLOCK_TAGS.indexOf(node.name) !== -1;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** 前後の空白を印の外へ出す(** text ** は強調にならないため) */
|
|
507
|
+
function hmWrapInline(inner, mark) {
|
|
508
|
+
const m = /^(\s*)([\s\S]*?)(\s*)$/.exec(inner);
|
|
509
|
+
const body = m[2];
|
|
510
|
+
if (body === '') return m[1] + m[3] === '' ? '' : ' ';
|
|
511
|
+
return m[1] + mark + body + mark + m[3];
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** インラインコードのバッククォートの本数を決める */
|
|
515
|
+
function hmCodeSpan(text) {
|
|
516
|
+
const body = text.replace(/\r?\n/g, ' ');
|
|
517
|
+
let longest = 0;
|
|
518
|
+
const runs = body.match(/`+/g);
|
|
519
|
+
if (runs) for (const r of runs) longest = Math.max(longest, r.length);
|
|
520
|
+
const fence = '`'.repeat(longest + 1);
|
|
521
|
+
const pad = /^`|`$|^\s|\s$/.test(body) && body !== '' ? ' ' : '';
|
|
522
|
+
return fence + pad + body + pad + fence;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** ノードの並びをインラインのMarkdownへ */
|
|
526
|
+
function hmInline(nodes, ctx) {
|
|
527
|
+
let out = '';
|
|
528
|
+
for (const node of nodes) {
|
|
529
|
+
out += hmInlineNode(node, ctx);
|
|
530
|
+
}
|
|
531
|
+
return out;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function hmInlineNode(node, ctx) {
|
|
535
|
+
const o = ctx.o;
|
|
536
|
+
if (node.type === 'comment') return '';
|
|
537
|
+
if (node.type === 'text') {
|
|
538
|
+
let text = String(node.value);
|
|
539
|
+
if (!ctx.pre) text = text.replace(/[\t\n\r ]+/g, ' ').replace(/\u00a0/g, ' ');
|
|
540
|
+
if (ctx.code) return text;
|
|
541
|
+
return o.escape ? hmEscapeText(text) : text;
|
|
542
|
+
}
|
|
543
|
+
if (node.type !== 'element') return '';
|
|
544
|
+
const name = node.name;
|
|
545
|
+
const attrs = node.attrs || {};
|
|
546
|
+
|
|
547
|
+
if (HM_DROP_TAGS.indexOf(name) !== -1) return '';
|
|
548
|
+
if (name === 'br') {
|
|
549
|
+
if (ctx.cell) return o.gfm ? '<br>' : ' ';
|
|
550
|
+
if (o.br === 'space') return ' ';
|
|
551
|
+
if (o.br === 'html') return '<br>\n';
|
|
552
|
+
if (o.br === 'backslash') return '\\\n';
|
|
553
|
+
return ' \n';
|
|
554
|
+
}
|
|
555
|
+
if (name === 'wbr') return '';
|
|
556
|
+
if (name === 'img') {
|
|
557
|
+
ctx.stats.images += 1;
|
|
558
|
+
const alt = String(attrs.alt || '').replace(/\s+/g, ' ').replace(/[[\]]/g, '\\$&');
|
|
559
|
+
if (o.images === 'drop') return '';
|
|
560
|
+
const src = hmResolveUrl(attrs.src || attrs['data-src'] || '', o.baseUrl);
|
|
561
|
+
if (o.images === 'alt' || !src) return alt;
|
|
562
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(src) && o.baseUrl === '') ctx.stats.relative += 1;
|
|
563
|
+
return ' + hmTitlePart(attrs.title) + ')';
|
|
564
|
+
}
|
|
565
|
+
if (name === 'input') {
|
|
566
|
+
// リストの項目の先頭にあるチェックボックスは li 側で読む。それ以外は本文ではない
|
|
567
|
+
if (String(attrs.type || '').toLowerCase() === 'checkbox') return '';
|
|
568
|
+
return attrs.value && String(attrs.type || '').toLowerCase() !== 'hidden' ? String(attrs.value) : '';
|
|
569
|
+
}
|
|
570
|
+
if (name === 'a') {
|
|
571
|
+
const href = String(attrs.href || '');
|
|
572
|
+
const inner = hmInline(node.children, Object.assign({}, ctx, { inLink: true }));
|
|
573
|
+
if (ctx.inLink || !href || o.links === 'strip') return inner;
|
|
574
|
+
if (/^javascript:/i.test(href)) return inner;
|
|
575
|
+
const label = inner.trim();
|
|
576
|
+
if (label === '') return '';
|
|
577
|
+
ctx.stats.links += 1;
|
|
578
|
+
const url = hmResolveUrl(href, o.baseUrl);
|
|
579
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(url) && url.charAt(0) !== '#' && o.baseUrl === '') ctx.stats.relative += 1;
|
|
580
|
+
if (o.links === 'reference') {
|
|
581
|
+
const id = hmRefId(ctx, url, attrs.title);
|
|
582
|
+
return '[' + label + '][' + id + ']';
|
|
583
|
+
}
|
|
584
|
+
// 文字列がそのままURLなら自動リンクにする
|
|
585
|
+
if (label.replace(/\\/g, '') === url && /^(https?|mailto):/i.test(url)) return '<' + url + '>';
|
|
586
|
+
return '[' + label + '](' + hmLinkTarget(url) + hmTitlePart(attrs.title) + ')';
|
|
587
|
+
}
|
|
588
|
+
if (name === 'code') {
|
|
589
|
+
if (ctx.code) return hmInline(node.children, ctx);
|
|
590
|
+
return hmCodeSpan(hmTextOf(node));
|
|
591
|
+
}
|
|
592
|
+
if (name === 'strong' || name === 'b') {
|
|
593
|
+
return hmWrapInline(hmInline(node.children, ctx), o.strong);
|
|
594
|
+
}
|
|
595
|
+
if (name === 'em' || name === 'i') {
|
|
596
|
+
return hmWrapInline(hmInline(node.children, ctx), o.emphasis);
|
|
597
|
+
}
|
|
598
|
+
if (name === 'del' || name === 's' || name === 'strike') {
|
|
599
|
+
if (o.gfm) return hmWrapInline(hmInline(node.children, ctx), '~~');
|
|
600
|
+
return o.unknown === 'keep' ? hmSerialize(node) : hmInline(node.children, ctx);
|
|
601
|
+
}
|
|
602
|
+
if (HM_TRANSPARENT_TAGS.indexOf(name) !== -1) return hmInline(node.children, ctx);
|
|
603
|
+
if (HM_MEDIA_TAGS.indexOf(name) !== -1) {
|
|
604
|
+
ctx.stats.media += 1;
|
|
605
|
+
if (o.unknown === 'drop') return '';
|
|
606
|
+
if (o.unknown === 'text') return hmInline(node.children, ctx);
|
|
607
|
+
ctx.notes.push({ code: 'MEDIA_KEPT', tag: name });
|
|
608
|
+
return hmSerialize(node);
|
|
609
|
+
}
|
|
610
|
+
if (HM_KEEP_INLINE_TAGS.indexOf(name) !== -1) {
|
|
611
|
+
const inner = hmInline(node.children, ctx);
|
|
612
|
+
if (inner.trim() === '') return '';
|
|
613
|
+
if (o.unknown === 'keep') {
|
|
614
|
+
ctx.notes.push({ code: 'HTML_KEPT', tag: name });
|
|
615
|
+
return '<' + name + '>' + inner + '</' + name + '>';
|
|
616
|
+
}
|
|
617
|
+
return inner;
|
|
618
|
+
}
|
|
619
|
+
// 未知のインライン要素は中身だけ流す
|
|
620
|
+
return hmInline(node.children, ctx);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** 参照リンクの番号を採る(同じ宛先は同じ番号を使い回す) */
|
|
624
|
+
function hmRefId(ctx, url, title) {
|
|
625
|
+
const key = url + '\u0000' + (title || '');
|
|
626
|
+
if (ctx.refMap[key]) return ctx.refMap[key];
|
|
627
|
+
const id = String(ctx.refs.length + 1);
|
|
628
|
+
ctx.refMap[key] = id;
|
|
629
|
+
ctx.refs.push({ id, url, title: title || '' });
|
|
630
|
+
return id;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/* -------------------- ブロック -------------------- */
|
|
634
|
+
|
|
635
|
+
/** 子ノードをブロックの配列にする(続くインラインは段落にまとめる) */
|
|
636
|
+
function hmBlocks(children, ctx) {
|
|
637
|
+
const out = [];
|
|
638
|
+
let buffer = [];
|
|
639
|
+
const flush = () => {
|
|
640
|
+
if (!buffer.length) return;
|
|
641
|
+
let text = hmInline(buffer, ctx).trim();
|
|
642
|
+
buffer = [];
|
|
643
|
+
if (text === '') return;
|
|
644
|
+
if (ctx.o.escape) text = hmEscapeLineStarts(text);
|
|
645
|
+
out.push({ text, kind: 'paragraph' });
|
|
646
|
+
};
|
|
647
|
+
for (const child of children) {
|
|
648
|
+
if (hmIsBlock(child)) {
|
|
649
|
+
flush();
|
|
650
|
+
const block = hmBlock(child, ctx);
|
|
651
|
+
if (block && block.text !== '') out.push(block);
|
|
652
|
+
} else {
|
|
653
|
+
buffer.push(child);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
flush();
|
|
657
|
+
return out;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** ブロックの配列を1つの文字列へ(リストの直後だけ空行を空けない) */
|
|
661
|
+
function hmJoinBlocks(blocks, tight) {
|
|
662
|
+
let out = '';
|
|
663
|
+
for (let i = 0; i < blocks.length; i += 1) {
|
|
664
|
+
if (i > 0) out += (tight && blocks[i].kind === 'list') ? '\n' : '\n\n';
|
|
665
|
+
out += blocks[i].text;
|
|
666
|
+
}
|
|
667
|
+
return out;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** 1つのブロック要素をMarkdownへ */
|
|
671
|
+
function hmBlock(el, ctx) {
|
|
672
|
+
const o = ctx.o;
|
|
673
|
+
const name = el.name;
|
|
674
|
+
|
|
675
|
+
if (/^h[1-6]$/.test(name)) {
|
|
676
|
+
const level = Number(name.charAt(1));
|
|
677
|
+
const text = hmInline(el.children, ctx).replace(/\s*\n\s*/g, ' ').trim();
|
|
678
|
+
if (text === '') return null;
|
|
679
|
+
ctx.stats.headings += 1;
|
|
680
|
+
if (o.headings === 'setext' && level <= 2) {
|
|
681
|
+
return { text: text + '\n' + (level === 1 ? '='.repeat(Math.max(3, hmWidth(text))) : '-'.repeat(Math.max(3, hmWidth(text)))), kind: 'heading' };
|
|
682
|
+
}
|
|
683
|
+
return { text: '#'.repeat(level) + ' ' + text.replace(/#+\s*$/, ''), kind: 'heading' };
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
if (name === 'hr') return { text: o.hr, kind: 'hr' };
|
|
687
|
+
|
|
688
|
+
if (name === 'pre') return hmCodeBlock(el, ctx);
|
|
689
|
+
|
|
690
|
+
if (name === 'blockquote') {
|
|
691
|
+
const inner = hmJoinBlocks(hmBlocks(el.children, ctx), false);
|
|
692
|
+
if (inner.trim() === '') return null;
|
|
693
|
+
const text = inner.split('\n').map((line) => (line === '' ? '>' : '> ' + line)).join('\n');
|
|
694
|
+
return { text, kind: 'quote' };
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if (name === 'ul' || name === 'ol') return hmList(el, ctx);
|
|
698
|
+
|
|
699
|
+
if (name === 'table') return hmTable(el, ctx);
|
|
700
|
+
|
|
701
|
+
if (name === 'dl') return hmDefList(el, ctx);
|
|
702
|
+
|
|
703
|
+
if (name === 'details' || name === 'summary' || name === 'figure' || name === 'figcaption'
|
|
704
|
+
|| name === 'div' || name === 'section' || name === 'article' || name === 'main'
|
|
705
|
+
|| name === 'header' || name === 'footer' || name === 'aside' || name === 'nav'
|
|
706
|
+
|| name === 'address' || name === 'form' || name === 'fieldset' || name === 'legend'
|
|
707
|
+
|| name === 'hgroup' || name === 'menu' || name === 'caption' || name === 'li'
|
|
708
|
+
|| name === 'dd' || name === 'dt' || name === 'td' || name === 'th'
|
|
709
|
+
|| name === 'tr' || name === 'thead' || name === 'tbody' || name === 'tfoot'
|
|
710
|
+
|| name === 'p') {
|
|
711
|
+
const blocks = hmBlocks(el.children, ctx);
|
|
712
|
+
if (!blocks.length) return null;
|
|
713
|
+
if (name === 'summary' && blocks.length === 1) {
|
|
714
|
+
return { text: hmWrapInline(blocks[0].text, o.strong), kind: 'paragraph' };
|
|
715
|
+
}
|
|
716
|
+
return { text: hmJoinBlocks(blocks, false), kind: blocks.length === 1 ? blocks[0].kind : 'group' };
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const blocks = hmBlocks(el.children, ctx);
|
|
720
|
+
if (!blocks.length) return null;
|
|
721
|
+
return { text: hmJoinBlocks(blocks, false), kind: 'group' };
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/** pre(+中の code)をコードブロックへ */
|
|
725
|
+
function hmCodeBlock(el, ctx) {
|
|
726
|
+
const o = ctx.o;
|
|
727
|
+
const codeEl = (el.children || []).find((c) => c.type === 'element' && c.name === 'code');
|
|
728
|
+
const target = codeEl || el;
|
|
729
|
+
let code = hmTextOf(target).replace(/\r\n?/g, '\n');
|
|
730
|
+
code = code.replace(/^\n/, '').replace(/\n[ \t]*$/, '');
|
|
731
|
+
if (code === '') return null;
|
|
732
|
+
ctx.stats.codeBlocks += 1;
|
|
733
|
+
const cls = String((codeEl && codeEl.attrs && codeEl.attrs.class) || el.attrs.class || '');
|
|
734
|
+
const m = /(?:^|\s)(?:language|lang|highlight|brush:)[-:]?([A-Za-z0-9+#.-]+)/.exec(cls);
|
|
735
|
+
let lang = m ? m[1].toLowerCase() : '';
|
|
736
|
+
if (lang === 'highlight' || lang === 'source') lang = '';
|
|
737
|
+
if (o.codeBlock === 'indented') {
|
|
738
|
+
return { text: code.split('\n').map((line) => (line === '' ? '' : ' ' + line)).join('\n'), kind: 'code' };
|
|
739
|
+
}
|
|
740
|
+
const fenceChar = o.fence.charAt(0);
|
|
741
|
+
let fenceLen = 3;
|
|
742
|
+
const runs = code.match(new RegExp('^' + (fenceChar === '`' ? '`' : '~') + '{3,}', 'gm'));
|
|
743
|
+
if (runs) for (const r of runs) fenceLen = Math.max(fenceLen, r.length + 1);
|
|
744
|
+
const fence = fenceChar.repeat(fenceLen);
|
|
745
|
+
return { text: fence + lang + '\n' + code + '\n' + fence, kind: 'code' };
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** ul / ol をリストへ(入れ子もそのまま) */
|
|
749
|
+
function hmList(el, ctx) {
|
|
750
|
+
const o = ctx.o;
|
|
751
|
+
const ordered = el.name === 'ol';
|
|
752
|
+
const startAttr = parseInt(el.attrs.start, 10);
|
|
753
|
+
let n = Number.isFinite(startAttr) ? startAttr : 1;
|
|
754
|
+
const items = [];
|
|
755
|
+
ctx.stats.lists += 1;
|
|
756
|
+
for (const child of el.children || []) {
|
|
757
|
+
if (child.type !== 'element') continue;
|
|
758
|
+
if (child.name !== 'li') {
|
|
759
|
+
// li の外に置かれた入れ子リストも項目として拾う
|
|
760
|
+
if (child.name === 'ul' || child.name === 'ol') {
|
|
761
|
+
const nested = hmList(child, ctx);
|
|
762
|
+
if (nested) items.push({ marker: '', text: nested.text, nested: true });
|
|
763
|
+
}
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
const marker = ordered ? n + '. ' : o.bullet + ' ';
|
|
767
|
+
if (ordered) n += 1;
|
|
768
|
+
let children = child.children || [];
|
|
769
|
+
let task = '';
|
|
770
|
+
if (o.gfm) {
|
|
771
|
+
const box = hmFindCheckbox(children);
|
|
772
|
+
if (box) {
|
|
773
|
+
task = box.attrs.checked !== undefined ? '[x] ' : '[ ] ';
|
|
774
|
+
children = hmWithout(children, box);
|
|
775
|
+
ctx.stats.tasks += 1;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
const blocks = hmBlocks(children, Object.assign({}, ctx, { listDepth: ctx.listDepth + 1 }));
|
|
779
|
+
const body = hmJoinBlocks(blocks, true);
|
|
780
|
+
items.push({ marker, text: task + body });
|
|
781
|
+
}
|
|
782
|
+
if (!items.length) return null;
|
|
783
|
+
const indentOf = (marker) => ' '.repeat(marker.length);
|
|
784
|
+
const text = items.map((item) => {
|
|
785
|
+
if (item.nested) {
|
|
786
|
+
return item.text.split('\n').map((line) => (line === '' ? '' : ' ' + line)).join('\n');
|
|
787
|
+
}
|
|
788
|
+
const lines = item.text.split('\n');
|
|
789
|
+
const pad = indentOf(item.marker);
|
|
790
|
+
return lines.map((line, idx) => {
|
|
791
|
+
if (idx === 0) return item.marker + line;
|
|
792
|
+
return line === '' ? '' : pad + line;
|
|
793
|
+
}).join('\n');
|
|
794
|
+
}).join('\n');
|
|
795
|
+
return { text, kind: 'list' };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/** li の先頭にあるチェックボックスを探す */
|
|
799
|
+
function hmFindCheckbox(children) {
|
|
800
|
+
for (const child of children) {
|
|
801
|
+
if (child.type === 'text' && child.value.trim() === '') continue;
|
|
802
|
+
if (child.type !== 'element') return null;
|
|
803
|
+
if (child.name === 'input' && String(child.attrs.type || '').toLowerCase() === 'checkbox') return child;
|
|
804
|
+
if (child.name === 'label' || child.name === 'p' || child.name === 'span') {
|
|
805
|
+
const hit = hmFindCheckbox(child.children || []);
|
|
806
|
+
if (hit) return hit;
|
|
807
|
+
}
|
|
808
|
+
return null;
|
|
809
|
+
}
|
|
810
|
+
return null;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/** 木から1つのノードだけ取り除いたコピーを返す */
|
|
814
|
+
function hmWithout(children, target) {
|
|
815
|
+
const out = [];
|
|
816
|
+
for (const child of children) {
|
|
817
|
+
if (child === target) continue;
|
|
818
|
+
if (child.type === 'element' && child.children && child.children.length) {
|
|
819
|
+
const inner = hmWithout(child.children, target);
|
|
820
|
+
if (inner !== child.children) {
|
|
821
|
+
out.push(Object.assign({}, child, { children: inner }));
|
|
822
|
+
continue;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
out.push(child);
|
|
826
|
+
}
|
|
827
|
+
return out;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/** dl を定義リストへ(Markdownに標準の記法が無いので term と : で書く) */
|
|
831
|
+
function hmDefList(el, ctx) {
|
|
832
|
+
const o = ctx.o;
|
|
833
|
+
const lines = [];
|
|
834
|
+
for (const child of el.children || []) {
|
|
835
|
+
if (child.type !== 'element') continue;
|
|
836
|
+
if (child.name === 'dt') {
|
|
837
|
+
const text = hmInline(child.children, ctx).replace(/\s*\n\s*/g, ' ').trim();
|
|
838
|
+
if (text) lines.push((lines.length ? '\n' : '') + hmWrapInline(text, o.strong));
|
|
839
|
+
} else if (child.name === 'dd') {
|
|
840
|
+
const text = hmJoinBlocks(hmBlocks(child.children, ctx), false).replace(/\n/g, '\n ');
|
|
841
|
+
if (text) lines.push(': ' + text);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
if (!lines.length) return null;
|
|
845
|
+
ctx.notes.push({ code: 'DEF_LIST' });
|
|
846
|
+
return { text: lines.join('\n'), kind: 'group' };
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/* -------------------- 表 -------------------- */
|
|
850
|
+
|
|
851
|
+
/** table から行(tr)を順番に集める */
|
|
852
|
+
function hmRowsOf(el, rows) {
|
|
853
|
+
for (const child of el.children || []) {
|
|
854
|
+
if (child.type !== 'element') continue;
|
|
855
|
+
if (child.name === 'tr') rows.push(child);
|
|
856
|
+
else if (child.name === 'thead' || child.name === 'tbody' || child.name === 'tfoot') hmRowsOf(child, rows);
|
|
857
|
+
}
|
|
858
|
+
return rows;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/** セルの配置(align属性 / style の text-align) */
|
|
862
|
+
function hmCellAlign(cell) {
|
|
863
|
+
const a = String(cell.attrs.align || '').toLowerCase();
|
|
864
|
+
if (a === 'left' || a === 'center' || a === 'right') return a;
|
|
865
|
+
const m = /text-align\s*:\s*(left|center|right)/i.exec(cell.attrs.style || '');
|
|
866
|
+
return m ? m[1].toLowerCase() : '';
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/** table を GFM の表へ(セルにブロックが入っていればHTMLのまま残す) */
|
|
870
|
+
function hmTable(el, ctx) {
|
|
871
|
+
const o = ctx.o;
|
|
872
|
+
ctx.stats.tables += 1;
|
|
873
|
+
const caption = (el.children || []).find((c) => c.type === 'element' && c.name === 'caption');
|
|
874
|
+
const rows = hmRowsOf(el, []);
|
|
875
|
+
if (!rows.length) return null;
|
|
876
|
+
|
|
877
|
+
if (o.tables === 'html' || !o.gfm) {
|
|
878
|
+
ctx.notes.push({ code: 'TABLE_HTML' });
|
|
879
|
+
return { text: hmSerialize(el), kind: 'table' };
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const grid = [];
|
|
883
|
+
const aligns = [];
|
|
884
|
+
let complex = false;
|
|
885
|
+
let spans = 0;
|
|
886
|
+
for (const tr of rows) {
|
|
887
|
+
const cells = [];
|
|
888
|
+
for (const cell of tr.children || []) {
|
|
889
|
+
if (cell.type !== 'element' || (cell.name !== 'td' && cell.name !== 'th')) continue;
|
|
890
|
+
// セルの中にブロック(リスト・表・コード)があるとGFMの表には収まらない
|
|
891
|
+
for (const inner of cell.children || []) {
|
|
892
|
+
if (inner.type === 'element'
|
|
893
|
+
&& ['ul', 'ol', 'table', 'pre', 'blockquote', 'dl'].indexOf(inner.name) !== -1) complex = true;
|
|
894
|
+
}
|
|
895
|
+
const text = hmInline(cell.children, Object.assign({}, ctx, { cell: true }))
|
|
896
|
+
.replace(/\s*\n\s*/g, ' ').replace(/\|/g, '\\|').trim();
|
|
897
|
+
const colspan = Math.max(1, Math.min(32, parseInt(cell.attrs.colspan, 10) || 1));
|
|
898
|
+
const rowspan = Math.max(1, parseInt(cell.attrs.rowspan, 10) || 1);
|
|
899
|
+
if (colspan > 1 || rowspan > 1) spans += 1;
|
|
900
|
+
cells.push({ text, align: hmCellAlign(cell), header: cell.name === 'th' });
|
|
901
|
+
for (let k = 1; k < colspan; k += 1) cells.push({ text: '', align: '', header: cell.name === 'th' });
|
|
902
|
+
}
|
|
903
|
+
if (cells.length) grid.push(cells);
|
|
904
|
+
}
|
|
905
|
+
if (!grid.length) return null;
|
|
906
|
+
|
|
907
|
+
if (complex) {
|
|
908
|
+
ctx.notes.push({ code: 'TABLE_COMPLEX' });
|
|
909
|
+
return { text: hmSerialize(el), kind: 'table' };
|
|
910
|
+
}
|
|
911
|
+
if (spans) ctx.notes.push({ code: 'TABLE_SPAN', n: spans });
|
|
912
|
+
|
|
913
|
+
const columns = grid.reduce((max, row) => Math.max(max, row.length), 0);
|
|
914
|
+
for (const row of grid) while (row.length < columns) row.push({ text: '', align: '', header: false });
|
|
915
|
+
|
|
916
|
+
let header;
|
|
917
|
+
let body;
|
|
918
|
+
const firstIsHeader = grid[0].some((c) => c.header)
|
|
919
|
+
|| (el.children || []).some((c) => c.type === 'element' && c.name === 'thead');
|
|
920
|
+
if (firstIsHeader) {
|
|
921
|
+
header = grid[0].map((c) => c.text);
|
|
922
|
+
body = grid.slice(1);
|
|
923
|
+
} else {
|
|
924
|
+
header = new Array(columns).fill('');
|
|
925
|
+
body = grid;
|
|
926
|
+
ctx.notes.push({ code: 'TABLE_NO_HEAD' });
|
|
927
|
+
}
|
|
928
|
+
for (let c = 0; c < columns; c += 1) {
|
|
929
|
+
let align = '';
|
|
930
|
+
for (const row of grid) {
|
|
931
|
+
if (row[c] && row[c].align) { align = row[c].align; break; }
|
|
932
|
+
}
|
|
933
|
+
aligns.push(align);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
const widths = new Array(columns).fill(3);
|
|
937
|
+
if (o.padTables) {
|
|
938
|
+
for (let c = 0; c < columns; c += 1) {
|
|
939
|
+
widths[c] = Math.max(widths[c], hmWidth(header[c]));
|
|
940
|
+
for (const row of body) widths[c] = Math.max(widths[c], hmWidth(row[c].text));
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
const pad = (text, c) => {
|
|
944
|
+
if (!o.padTables) return text;
|
|
945
|
+
const gap = widths[c] - hmWidth(text);
|
|
946
|
+
return text + ' '.repeat(gap > 0 ? gap : 0);
|
|
947
|
+
};
|
|
948
|
+
const line = (cells) => '| ' + cells.map((t, c) => pad(t, c)).join(' | ') + ' |';
|
|
949
|
+
const sep = aligns.map((a, c) => {
|
|
950
|
+
const w = o.padTables ? widths[c] : 3;
|
|
951
|
+
if (a === 'left') return ':' + '-'.repeat(Math.max(2, w - 1));
|
|
952
|
+
if (a === 'right') return '-'.repeat(Math.max(2, w - 1)) + ':';
|
|
953
|
+
if (a === 'center') return ':' + '-'.repeat(Math.max(1, w - 2)) + ':';
|
|
954
|
+
return '-'.repeat(Math.max(3, w));
|
|
955
|
+
});
|
|
956
|
+
const out = [line(header), '| ' + sep.join(' | ') + ' |'];
|
|
957
|
+
for (const row of body) out.push(line(row.map((c) => c.text)));
|
|
958
|
+
let text = out.join('\n');
|
|
959
|
+
if (caption) {
|
|
960
|
+
const capText = hmInline(caption.children, ctx).replace(/\s*\n\s*/g, ' ').trim();
|
|
961
|
+
if (capText) text = hmWrapInline(capText, o.emphasis) + '\n\n' + text;
|
|
962
|
+
}
|
|
963
|
+
return { text, kind: 'table' };
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/* -------------------- 入口 -------------------- */
|
|
967
|
+
|
|
968
|
+
/** 渡されたオプションを既定値で埋め、値の妥当性も見る */
|
|
969
|
+
function hmOptions(options) {
|
|
970
|
+
const o = Object.assign({}, HM_DEFAULTS, options || {});
|
|
971
|
+
const pick = (key, allowed) => { if (allowed.indexOf(o[key]) === -1) o[key] = HM_DEFAULTS[key]; };
|
|
972
|
+
pick('headings', HM_HEADING_STYLES);
|
|
973
|
+
pick('bullet', HM_BULLETS);
|
|
974
|
+
pick('emphasis', HM_EMPHASIS);
|
|
975
|
+
pick('strong', HM_STRONG);
|
|
976
|
+
pick('codeBlock', HM_CODE_BLOCKS);
|
|
977
|
+
pick('fence', HM_FENCES);
|
|
978
|
+
pick('hr', HM_HRS);
|
|
979
|
+
pick('links', HM_LINK_STYLES);
|
|
980
|
+
pick('images', HM_IMAGE_STYLES);
|
|
981
|
+
pick('tables', HM_TABLE_STYLES);
|
|
982
|
+
pick('unknown', HM_UNKNOWN_STYLES);
|
|
983
|
+
pick('br', HM_BR_STYLES);
|
|
984
|
+
o.baseUrl = String(o.baseUrl || '').trim();
|
|
985
|
+
return o;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* HTML断片をMarkdownへ変換する。返すのは
|
|
990
|
+
* { markdown, stats, notes, refs } で、notes はコードだけ(文言は呼び出し側が持つ)。
|
|
991
|
+
*/
|
|
992
|
+
export function hmToMarkdown(html, options) {
|
|
993
|
+
const o = hmOptions(options);
|
|
994
|
+
const parsed = hmParse(html);
|
|
995
|
+
const counts = { scripts: 0, styles: 0, comments: 0, chrome: 0, hidden: 0 };
|
|
996
|
+
hmPrune(parsed.root, o, counts);
|
|
997
|
+
|
|
998
|
+
let root = parsed.root;
|
|
999
|
+
if (o.mainOnly) {
|
|
1000
|
+
const main = hmFind(root, ['main', 'article']);
|
|
1001
|
+
if (main) root = main;
|
|
1002
|
+
else {
|
|
1003
|
+
const body = hmFind(root, ['body']);
|
|
1004
|
+
if (body) root = body;
|
|
1005
|
+
}
|
|
1006
|
+
} else {
|
|
1007
|
+
const body = hmFind(root, ['body']);
|
|
1008
|
+
if (body) root = body;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
const ctx = {
|
|
1012
|
+
o,
|
|
1013
|
+
notes: [],
|
|
1014
|
+
refs: [],
|
|
1015
|
+
refMap: {},
|
|
1016
|
+
listDepth: 0,
|
|
1017
|
+
stats: {
|
|
1018
|
+
headings: 0, links: 0, images: 0, tables: 0, codeBlocks: 0,
|
|
1019
|
+
lists: 0, tasks: 0, media: 0, relative: 0,
|
|
1020
|
+
},
|
|
1021
|
+
};
|
|
1022
|
+
|
|
1023
|
+
const blocks = hmBlocks(root.children || [], ctx);
|
|
1024
|
+
let markdown = hmJoinBlocks(blocks, false);
|
|
1025
|
+
|
|
1026
|
+
if (o.links === 'reference' && ctx.refs.length) {
|
|
1027
|
+
markdown += '\n\n' + ctx.refs
|
|
1028
|
+
.map((r) => '[' + r.id + ']: ' + hmLinkTarget(r.url) + hmTitlePart(r.title)).join('\n');
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// 行末の空白は落とす。ただし2つちょうどのときは <br> 由来の改行なので残す
|
|
1032
|
+
markdown = markdown.split('\n')
|
|
1033
|
+
.map((line) => line.replace(/[ \t]+$/, (m) => (m === ' ' ? ' ' : '')))
|
|
1034
|
+
.join('\n');
|
|
1035
|
+
if (o.collapseBlankLines) markdown = markdown.replace(/\n{3,}/g, '\n\n');
|
|
1036
|
+
markdown = markdown.replace(/^\n+/, '').replace(/\s+$/, '');
|
|
1037
|
+
if (markdown !== '') markdown += '\n';
|
|
1038
|
+
|
|
1039
|
+
// 指摘の組み立て
|
|
1040
|
+
const notes = [];
|
|
1041
|
+
const seen = {};
|
|
1042
|
+
for (const n of ctx.notes) {
|
|
1043
|
+
const key = n.code + (n.tag || '');
|
|
1044
|
+
if (seen[key]) { seen[key].n = (seen[key].n || 1) + (n.n || 1); continue; }
|
|
1045
|
+
const copy = Object.assign({}, n);
|
|
1046
|
+
seen[key] = copy;
|
|
1047
|
+
notes.push(copy);
|
|
1048
|
+
}
|
|
1049
|
+
if (counts.scripts || counts.styles) notes.unshift({ code: 'DROPPED_CODE', scripts: counts.scripts, styles: counts.styles });
|
|
1050
|
+
if (counts.chrome) notes.unshift({ code: 'MAIN_ONLY', n: counts.chrome });
|
|
1051
|
+
if (counts.hidden) notes.push({ code: 'HIDDEN_DROPPED', n: counts.hidden });
|
|
1052
|
+
if (parsed.flags.unterminatedTag) notes.unshift({ code: 'BROKEN_TAG' });
|
|
1053
|
+
if (parsed.flags.unterminatedComment) notes.unshift({ code: 'BROKEN_COMMENT' });
|
|
1054
|
+
if (parsed.flags.unclosed) notes.push({ code: 'UNCLOSED', n: parsed.flags.unclosed });
|
|
1055
|
+
if (parsed.flags.strayEnd) notes.push({ code: 'STRAY_END', n: parsed.flags.strayEnd });
|
|
1056
|
+
if (ctx.stats.relative) notes.push({ code: 'RELATIVE_URL', n: ctx.stats.relative });
|
|
1057
|
+
if (ctx.stats.tables && !o.gfm) notes.push({ code: 'GFM_OFF' });
|
|
1058
|
+
|
|
1059
|
+
const stats = Object.assign({}, ctx.stats, {
|
|
1060
|
+
chars: markdown.length,
|
|
1061
|
+
lines: markdown === '' ? 0 : markdown.replace(/\n$/, '').split('\n').length,
|
|
1062
|
+
words: (markdown.match(/[A-Za-z0-9]+/g) || []).length,
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
return { markdown, stats, notes, refs: ctx.refs, options: o };
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/* ==================== ここまで変換コア ==================== */
|
|
1069
|
+
|
|
1070
|
+
/* ==================== ここからMCP側だけの部分 ==================== */
|
|
1071
|
+
|
|
1072
|
+
// 指摘の文面(site側は同じ code を日英の T テーブルで出し分けている)
|
|
1073
|
+
const NOTE_JA = {
|
|
1074
|
+
BROKEN_TAG: () => 'タグが閉じられていません(属性の引用符が開いたままです)。以降をすべて1つの属性値として読みました。',
|
|
1075
|
+
BROKEN_COMMENT: () => 'コメント(<!--)が閉じられていないため、以降をすべてコメントとして捨てました。',
|
|
1076
|
+
UNCLOSED: (n) => `閉じられていない要素が ${n.n} 個ありました。ブラウザと同じように末尾で自動的に閉じています。`,
|
|
1077
|
+
STRAY_END: (n) => `対応する開始タグが無い閉じタグを ${n.n} 個無視しました。`,
|
|
1078
|
+
DROPPED_CODE: (n) => `<script> を ${n.scripts} 個・<style> を ${n.styles} 個取り除きました(中身は本文ではなくコードのため)。`,
|
|
1079
|
+
MAIN_ONLY: (n) => `main_only により header / nav / footer / aside / form を ${n.n} 個取り除きました。`,
|
|
1080
|
+
HIDDEN_DROPPED: (n) => `非表示の要素(hidden 属性・display:none)を ${n.n} 個取り除きました。`,
|
|
1081
|
+
RELATIVE_URL: (n) => `相対パスのままのリンク・画像が ${n.n} 件あります。base_url に元ページのアドレスを渡すと絶対URLに直せます。`,
|
|
1082
|
+
TABLE_NO_HEAD: () => '見出し行の無い表がありました。GFMの表は見出し行を省けないため、空の見出し行を書いています。',
|
|
1083
|
+
TABLE_SPAN: (n) => `colspan / rowspan を使ったセルが ${n.n} 個あります。Markdownの表にセルの結合は無いので、colspan は空セルで埋め、rowspan は先頭の行にだけ値が入ります。`,
|
|
1084
|
+
TABLE_COMPLEX: () => '表のセルの中にリスト・表・コードブロックが入っています。Markdownの表には収まらないため、その表はHTMLのまま残しました。',
|
|
1085
|
+
TABLE_HTML: () => '指定により表はHTMLのまま残しています。',
|
|
1086
|
+
DEF_LIST: () => '<dl> は「太字の語+": " の行」として書き出しました(Markdown Extra の記法で、CommonMark・GFMには含まれません)。',
|
|
1087
|
+
MEDIA_KEPT: (n) => `<${n.tag}> はMarkdownに対応する書き方が無いため、HTMLのまま残しました(unknown で扱いを変えられます)。`,
|
|
1088
|
+
HTML_KEPT: (n) => `<${n.tag}> はMarkdownに対応する書き方が無いため、インラインのHTMLタグのまま残しました。`,
|
|
1089
|
+
GFM_OFF: () => '表が含まれていますが gfm=false です——表・タスクリスト・取り消し線はGitHubの拡張で、CommonMarkには含まれません。',
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
const NOTE_EN = {
|
|
1093
|
+
BROKEN_TAG: () => 'A tag is not closed properly (an attribute quote is left open); everything after it was read as one attribute value.',
|
|
1094
|
+
BROKEN_COMMENT: () => 'An HTML comment (<!--) is never closed, so the rest of the document was treated as comment and dropped.',
|
|
1095
|
+
UNCLOSED: (n) => `${n.n} element(s) were left open and closed automatically at the end, the way a browser does.`,
|
|
1096
|
+
STRAY_END: (n) => `${n.n} closing tag(s) had no matching opening tag and were ignored.`,
|
|
1097
|
+
DROPPED_CODE: (n) => `Removed ${n.scripts} <script> and ${n.styles} <style> block(s); their contents are code, not text.`,
|
|
1098
|
+
MAIN_ONLY: (n) => `main_only removed ${n.n} header / nav / footer / aside / form block(s).`,
|
|
1099
|
+
HIDDEN_DROPPED: (n) => `${n.n} element(s) marked hidden (the hidden attribute or display:none) were removed.`,
|
|
1100
|
+
RELATIVE_URL: (n) => `${n.n} link(s) or image(s) still point at a relative path. Pass the page's address as base_url to make them absolute.`,
|
|
1101
|
+
TABLE_NO_HEAD: () => 'A table had no header row. A GFM table cannot omit one, so an empty header was written above the separator row.',
|
|
1102
|
+
TABLE_SPAN: (n) => `${n.n} cell(s) use colspan / rowspan. Markdown tables have no merged cells: a colspan was filled with empty cells and a rowspan is only written on its first row.`,
|
|
1103
|
+
TABLE_COMPLEX: () => 'A table cell contains a list, a table or a code block, which does not fit in a Markdown table, so that table was left as HTML.',
|
|
1104
|
+
TABLE_HTML: () => 'Tables were left as HTML because that is what was asked for.',
|
|
1105
|
+
DEF_LIST: () => 'A <dl> was written as a bold term plus a ": " line (the Markdown Extra syntax, which is in neither CommonMark nor GFM).',
|
|
1106
|
+
MEDIA_KEPT: (n) => `<${n.tag}> has no Markdown equivalent and was kept as raw HTML (use unknown to change that).`,
|
|
1107
|
+
HTML_KEPT: (n) => `<${n.tag}> has no Markdown equivalent and was kept as an inline HTML tag.`,
|
|
1108
|
+
GFM_OFF: () => 'The source has a table, but gfm=false — tables, task lists and strikethrough are GitHub extensions, not CommonMark.',
|
|
1109
|
+
};
|
|
1110
|
+
|
|
1111
|
+
const hmNoteText = (lang, note) => {
|
|
1112
|
+
const table = lang === 'en' ? NOTE_EN : NOTE_JA;
|
|
1113
|
+
const make = table[note.code];
|
|
1114
|
+
return make ? make(note) : note.code;
|
|
1115
|
+
};
|
|
1116
|
+
|
|
1117
|
+
export class HtmlToMarkdownToolError extends Error {}
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* HTML断片をMarkdownへ変換する(tools.first-ch.com/html-md/ と同一ロジック)。
|
|
1121
|
+
*
|
|
1122
|
+
* @param {object} opts
|
|
1123
|
+
* @param {string} [opts.html] 変換するHTML(path と排他)
|
|
1124
|
+
* @param {string} [opts.path] 変換するファイルの絶対パス(UTF-8として読む)
|
|
1125
|
+
* @param {string} [opts.outputPath] 結果を書き出す絶対パス(指定すると markdown は返さない)
|
|
1126
|
+
* @param {boolean} [opts.main_only] main / article の中だけを取り出す
|
|
1127
|
+
* @param {string} [opts.base_url] 相対URLを絶対URLへ直すための基準
|
|
1128
|
+
* @param {boolean} [opts.gfm] 表・タスクリスト・取り消し線(既定 true)
|
|
1129
|
+
* @param {boolean} [opts.escape] Markdownの記号をエスケープする(既定 true)
|
|
1130
|
+
* @param {boolean} [opts.pad_tables] 表の桁を揃える(既定 true)
|
|
1131
|
+
* @param {string} [opts.headings] atx / setext
|
|
1132
|
+
* @param {string} [opts.bullet] - / * / +
|
|
1133
|
+
* @param {string} [opts.code_block] fenced / indented
|
|
1134
|
+
* @param {string} [opts.links] inline / reference / strip
|
|
1135
|
+
* @param {string} [opts.images] keep / alt / drop
|
|
1136
|
+
* @param {string} [opts.tables] gfm / html
|
|
1137
|
+
* @param {string} [opts.unknown] keep / text / drop
|
|
1138
|
+
* @param {string} [opts.br] spaces / backslash / html / space
|
|
1139
|
+
* @param {string} [opts.lang] notes の言語(ja / en)
|
|
1140
|
+
*/
|
|
1141
|
+
export async function htmlToMarkdownTool(opts = {}) {
|
|
1142
|
+
const o = opts || {};
|
|
1143
|
+
const lang = o.lang === undefined ? 'ja' : o.lang;
|
|
1144
|
+
if (lang !== 'ja' && lang !== 'en') throw new HtmlToMarkdownToolError(`lang は ja か en: ${o.lang}`);
|
|
1145
|
+
|
|
1146
|
+
const hasHtml = typeof o.html === 'string';
|
|
1147
|
+
const hasPath = typeof o.path === 'string' && o.path.trim() !== '';
|
|
1148
|
+
if (hasHtml === hasPath) {
|
|
1149
|
+
throw new HtmlToMarkdownToolError('html か path のどちらか一方を指定してください');
|
|
1150
|
+
}
|
|
1151
|
+
const enumOpt = (name, value, allowed) => {
|
|
1152
|
+
if (value === undefined || value === null) return undefined;
|
|
1153
|
+
if (allowed.indexOf(value) === -1) {
|
|
1154
|
+
throw new HtmlToMarkdownToolError(`${name} は ${allowed.join(' / ')} のどれかです: ${value}`);
|
|
1155
|
+
}
|
|
1156
|
+
return value;
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
const source = hasHtml ? String(o.html) : await readFile(o.path, 'utf8');
|
|
1160
|
+
const options = {
|
|
1161
|
+
headings: enumOpt('headings', o.headings, HM_HEADING_STYLES),
|
|
1162
|
+
bullet: enumOpt('bullet', o.bullet, HM_BULLETS),
|
|
1163
|
+
codeBlock: enumOpt('code_block', o.code_block, HM_CODE_BLOCKS),
|
|
1164
|
+
links: enumOpt('links', o.links, HM_LINK_STYLES),
|
|
1165
|
+
images: enumOpt('images', o.images, HM_IMAGE_STYLES),
|
|
1166
|
+
tables: enumOpt('tables', o.tables, HM_TABLE_STYLES),
|
|
1167
|
+
unknown: enumOpt('unknown', o.unknown, HM_UNKNOWN_STYLES),
|
|
1168
|
+
br: enumOpt('br', o.br, HM_BR_STYLES),
|
|
1169
|
+
baseUrl: o.base_url === undefined ? '' : String(o.base_url),
|
|
1170
|
+
mainOnly: o.main_only === true,
|
|
1171
|
+
gfm: o.gfm !== false,
|
|
1172
|
+
escape: o.escape !== false,
|
|
1173
|
+
padTables: o.pad_tables !== false,
|
|
1174
|
+
};
|
|
1175
|
+
for (const key of Object.keys(options)) {
|
|
1176
|
+
if (options[key] === undefined) delete options[key];
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
const r = hmToMarkdown(source, options);
|
|
1180
|
+
const used = r.options;
|
|
1181
|
+
const result = {
|
|
1182
|
+
ok: true,
|
|
1183
|
+
source: hasHtml ? { type: 'html', bytes: Buffer.byteLength(source, 'utf8') }
|
|
1184
|
+
: { type: 'file', path: o.path, name: basename(o.path), bytes: Buffer.byteLength(source, 'utf8') },
|
|
1185
|
+
markdown: r.markdown,
|
|
1186
|
+
stats: {
|
|
1187
|
+
headings: r.stats.headings,
|
|
1188
|
+
links: r.stats.links,
|
|
1189
|
+
images: r.stats.images,
|
|
1190
|
+
tables: r.stats.tables,
|
|
1191
|
+
code_blocks: r.stats.codeBlocks,
|
|
1192
|
+
lists: r.stats.lists,
|
|
1193
|
+
task_items: r.stats.tasks,
|
|
1194
|
+
kept_html: r.stats.media,
|
|
1195
|
+
relative_urls: r.stats.relative,
|
|
1196
|
+
characters: r.stats.chars,
|
|
1197
|
+
lines: r.stats.lines,
|
|
1198
|
+
},
|
|
1199
|
+
options: {
|
|
1200
|
+
main_only: used.mainOnly,
|
|
1201
|
+
base_url: used.baseUrl,
|
|
1202
|
+
gfm: used.gfm,
|
|
1203
|
+
escape: used.escape,
|
|
1204
|
+
pad_tables: used.padTables,
|
|
1205
|
+
headings: used.headings,
|
|
1206
|
+
bullet: used.bullet,
|
|
1207
|
+
code_block: used.codeBlock,
|
|
1208
|
+
links: used.links,
|
|
1209
|
+
images: used.images,
|
|
1210
|
+
tables: used.tables,
|
|
1211
|
+
unknown: used.unknown,
|
|
1212
|
+
br: used.br,
|
|
1213
|
+
},
|
|
1214
|
+
notes: r.notes.map((n) => ({ code: n.code, message: hmNoteText(lang, n) })),
|
|
1215
|
+
};
|
|
1216
|
+
if (used.links === 'reference' && r.refs.length) {
|
|
1217
|
+
result.references = r.refs.map((ref) => ({ id: ref.id, url: ref.url, title: ref.title || null }));
|
|
1218
|
+
}
|
|
1219
|
+
if (o.outputPath) {
|
|
1220
|
+
await writeFile(o.outputPath, r.markdown, 'utf8');
|
|
1221
|
+
result.output = o.outputPath;
|
|
1222
|
+
// ファイルに書けたなら本文は重複した重い情報でしかない
|
|
1223
|
+
delete result.markdown;
|
|
1224
|
+
}
|
|
1225
|
+
return result;
|
|
1226
|
+
}
|