Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 38x 38x 153x 38x 115x 38x 38x 38x 13x 13x 13x 13x 13x 13x 24x 20x 20x 39x 25x 14x 20x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 22x 22x 2x 20x 1x 19x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 2x 1x 1x 1x 2x 12x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 10x 10x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 34x 34x 34x 13x 26x 13x 13x 16x 32x 16x 1x 15x 15x 1x 2x 1x 1x 5x 5x 5x 32x 32x 13x 19x 16x 32x 5x 22x 20x 17x 15x 12x 9x 7x 6x 5x 5x 5x 5x 5x 5x 22x 5x 26x 26x 13x 13x 13x 13x 3x 3x 2x 3x 10x 1x 1x 1x 1x 1x 9x 3x 6x 3x 6x 6x 6x 6x 6x 6x 6x 5x 7x 7x 10x 10x 10x 7x 1x 1x 1x 1x 7x 7x 7x 7x 9x 9x 8x 4x 4x 4x 4x 4x 7x 7x 7x 3x | /**
* Walk markdown-it block tokens into RFCXML middle-matter sections.
*/
const { el, escapeXml } = require('./xml-builder');
const { headingToAnchor } = require('./term-anchor');
const { renderInlineToken } = require('./inline');
const { DEF_LINE_RE } = require('./terminology');
const SKIP_HEADING_RE = /^(status of this memo|copyright notice|table of contents|authors?'? addresses|colophon)$/i;
const REFERENCES_HEADING_RE = /^(normative\s+references|informative\s+references|references)$/i;
const ABSTRACT_HEADING_RE = /^abstract$/i;
const TERMINOLOGY_HEADING_RE = /^(terminology|terms and definitions|terms)$/i;
const SECURITY_HEADING_RE = /^security considerations$/i;
const IANA_HEADING_RE = /^iana considerations$/i;
const ACK_HEADING_RE = /^acknowledg(e)?ments?$/i;
const APPENDIX_HEADING_RE = /^appendix\b/i;
const UNSUPPORTED_FENCE_RE = /^(mermaid|chart|plantuml|uml|katex)$/i;
function attrGet(token, name) {
if (!token) return null;
if (typeof token.attrGet === 'function') {
return token.attrGet(name);
}
const found = (token.attrs || []).find(pair => pair[0] === name);
return found ? found[1] : null;
}
function findClose(tokens, start, openType, closeType) {
let depth = 0;
for (let i = start; i < tokens.length; i++) {
if (tokens[i].type === openType) {
depth++;
} else if (tokens[i].type === closeType) {
depth--;
Eif (depth === 0) {
return i;
}
}
}
return tokens.length - 1;
}
function headingLevel(token) {
const tag = token.tag || 'h2';
const n = Number(tag.replace(/[^0-9]/g, '')) || 2;
return n;
}
function inlineText(token) {
Iif (!token) return '';
Eif (token.children) {
return token.children.map(child => child.content || inlineText(child)).join('');
}
return token.content || '';
}
function isDefOrTrefTemplate(token) {
return token && token.type === 'template' && token.info && /^(def|tref)$/i.test(token.info.type);
}
function isTermOnlyParagraph(inlineToken) {
Iif (!inlineToken || inlineToken.type !== 'inline' || !inlineToken.children) {
return false;
}
const meaningful = inlineToken.children.filter(child => {
if (child.type === 'text') {
return child.content.trim() !== '';
}
return child.type !== 'softbreak' && child.type !== 'hardbreak';
});
return meaningful.length > 0 && meaningful.every(isDefOrTrefTemplate);
}
function isSkipHtml(token) {
const content = token.content || '';
return /terminology-section-start/.test(content) ||
/<!--\s*file:/.test(content) ||
/<!--[\s\S]*-->/.test(content.trim());
}
function renderFence(token, ctx) {
const info = (token.info || '').trim().split(/\s+/)[0] || '';
if (UNSUPPORTED_FENCE_RE.test(info)) {
ctx.warnings.push(`Skipping unsupported ${info} diagram; IETF RFCXML cannot execute JavaScript diagrams.`);
return el('artwork', { type: 'ascii-art' }, escapeXml(`[${info} diagram omitted]`));
}
Eif (info) {
return el('sourcecode', { type: info }, escapeXml(token.content.replace(/\n$/, '')));
}
return el('artwork', { type: 'ascii-art' }, escapeXml(token.content.replace(/\n$/, '')));
}
function renderList(tokens, start, ctx) {
const open = tokens[start];
const closeType = open.type.replace('_open', '_close');
const end = findClose(tokens, start, open.type, closeType);
const tag = open.type.startsWith('ordered') ? 'ol' : 'ul';
const items = [];
let i = start + 1;
while (i < end) {
if (tokens[i].type === 'list_item_open') {
const itemEnd = findClose(tokens, i, 'list_item_open', 'list_item_close');
const inner = renderTokenRange(tokens, i + 1, itemEnd, ctx);
items.push(el('li', inner.length ? inner : '<t> </t>'));
i = itemEnd + 1;
} else E{
i++;
}
}
return { xml: el(tag, items), next: end + 1 };
}
function wrapT(inner) {
const text = String(inner || '').trim();
if (!text) {
return '';
}
if (text.startsWith('<t>') || text.startsWith('<ul') || text.startsWith('<ol') ||
text.startsWith('<dl') || text.startsWith('<sourcecode') || text.startsWith('<artwork') ||
text.startsWith('<table') || text.startsWith('<figure') || text.startsWith('<aside') ||
text.startsWith('<blockquote')) {
return text;
}
return `<t>${inner}</t>`;
}
function renderTable(tokens, start, ctx) {
const end = findClose(tokens, start, 'table_open', 'table_close');
const theadCells = [];
const bodyRows = [];
let currentRow = null;
let inHead = false;
for (let i = start + 1; i < end; i++) {
const token = tokens[i];
if (token.type === 'thead_open') inHead = true;
if (token.type === 'thead_close') inHead = false;
if (token.type === 'tr_open') currentRow = [];
if (token.type === 'tr_close' && currentRow) {
if (inHead || (theadCells.length === 0 && bodyRows.length === 0 && currentRow.length)) {
if (inHead || theadCells.length === 0) {
theadCells.push(...currentRow);
} else E{
bodyRows.push(currentRow);
}
} else {
bodyRows.push(currentRow);
}
currentRow = null;
}
if ((token.type === 'th_open' || token.type === 'td_open') && currentRow) {
const closeType = token.type.replace('_open', '_close');
const cellEnd = findClose(tokens, i, token.type, closeType);
const innerTokens = tokens.slice(i + 1, cellEnd);
const inline = innerTokens.find(t => t.type === 'inline');
const cellXml = wrapT(inline ? renderInlineToken(inline, ctx) : '');
currentRow.push(el(token.type.startsWith('th') ? 'th' : 'td', cellXml || '<t> </t>'));
i = cellEnd;
}
}
const parts = [];
Eif (theadCells.length) {
parts.push(el('thead', el('tr', theadCells)));
}
Eif (bodyRows.length) {
parts.push(el('tbody', bodyRows.map(row => el('tr', row))));
}
return { xml: el('table', parts), next: end + 1 };
}
function renderDl(tokens, start, ctx) {
const end = findClose(tokens, start, 'dl_open', 'dl_close');
let containsTermTag = false;
for (let i = start; i <= end; i++) {
const token = tokens[i];
if (token.type === 'inline' && isTermOnlyParagraph(token)) {
containsTermTag = true;
}
Iif (token.type === 'template' && isDefOrTrefTemplate(token)) {
containsTermTag = true;
}
if (token.children && token.children.some(isDefOrTrefTemplate)) {
containsTermTag = true;
}
}
Eif (containsTermTag) {
return { xml: '', next: end + 1, skippedTerms: true };
}
const items = [];
let i = start + 1;
let pendingDt = '';
while (i < end) {
const token = tokens[i];
if (token.type === 'dt_open') {
const dtEnd = findClose(tokens, i, 'dt_open', 'dt_close');
const inline = tokens.slice(i, dtEnd).find(t => t.type === 'inline');
pendingDt = inline ? renderInlineToken(inline, ctx) : '';
i = dtEnd + 1;
} else if (token.type === 'dd_open') {
const ddEnd = findClose(tokens, i, 'dd_open', 'dd_close');
const inner = renderTokenRange(tokens, i + 1, ddEnd, ctx);
items.push(el('dt', pendingDt || ' '));
items.push(el('dd', inner.length ? inner : '<t> </t>'));
pendingDt = '';
i = ddEnd + 1;
} else {
i++;
}
}
return { xml: items.length ? el('dl', { newline: 'true' }, items) : '', next: end + 1 };
}
function renderTokenRange(tokens, start, end, ctx) {
const parts = [];
let i = start;
while (i < end) {
const result = walkOne(tokens, i, ctx, end);
Eif (result.xml) {
parts.push(result.xml);
}
i = result.next;
}
return parts;
}
function walkOne(tokens, i, ctx, end = tokens.length) {
const token = tokens[i];
Iif (!token || i >= end) {
return { xml: '', next: i + 1 };
}
switch (token.type) {
case 'heading_open': {
const close = findClose(tokens, i, 'heading_open', 'heading_close');
const inline = tokens.slice(i, close).find(t => t.type === 'inline');
const text = inlineText(inline).trim();
return {
xml: '',
next: close + 1,
heading: { level: headingLevel(token), text, inline }
};
}
case 'paragraph_open': {
const close = findClose(tokens, i, 'paragraph_open', 'paragraph_close');
const inline = tokens.slice(i, close).find(t => t.type === 'inline');
if (inline && isTermOnlyParagraph(inline)) {
return { xml: '', next: close + 1, skippedTerms: true };
}
const inner = inline ? renderInlineToken(inline, ctx) : '';
return { xml: wrapT(inner), next: close + 1 };
}
case 'bullet_list_open':
case 'ordered_list_open':
return renderList(tokens, i, ctx);
case 'blockquote_open': {
const close = findClose(tokens, i, 'blockquote_open', 'blockquote_close');
const inner = renderTokenRange(tokens, i + 1, close, ctx);
return { xml: el('blockquote', inner), next: close + 1 };
}
case 'fence':
case 'code_block':
return { xml: renderFence(token, ctx), next: i + 1 };
case 'table_open':
return renderTable(tokens, i, ctx);
case 'dl_open':
return renderDl(tokens, i, ctx);
case 'hr':
return { xml: '', next: i + 1 };
case 'html_block':
if (isSkipHtml(token)) {
return { xml: '', next: i + 1 };
}
ctx.warnings.push('Skipping raw HTML block in RFCXML export.');
return { xml: '', next: i + 1 };
case 'inline':
return { xml: wrapT(renderInlineToken(token, ctx)), next: i + 1 };
default: {
if (token.type.endsWith('_open') && token.type.startsWith('container_')) {
const closeType = token.type.replace('_open', '_close');
const close = findClose(tokens, i, token.type, closeType);
const inner = renderTokenRange(tokens, i + 1, close, ctx);
return { xml: el('aside', inner), next: close + 1 };
}
return { xml: '', next: i + 1 };
}
}
}
function collectFlow(tokens, ctx) {
const flow = [];
let i = 0;
while (i < tokens.length) {
const result = walkOne(tokens, i, ctx);
if (result.heading) {
flow.push({ type: 'heading', ...result.heading });
} else if (result.xml) {
flow.push({ type: 'block', xml: result.xml });
}
i = result.next;
}
return flow;
}
function classifyHeading(text) {
if (ABSTRACT_HEADING_RE.test(text)) return 'abstract';
if (SKIP_HEADING_RE.test(text)) return 'skip';
if (REFERENCES_HEADING_RE.test(text)) return 'references';
if (TERMINOLOGY_HEADING_RE.test(text)) return 'terminology';
if (SECURITY_HEADING_RE.test(text)) return 'security';
if (IANA_HEADING_RE.test(text)) return 'iana';
if (ACK_HEADING_RE.test(text)) return 'acknowledgements';
if (APPENDIX_HEADING_RE.test(text)) return 'appendix';
return 'body';
}
function nestSections(flow, options = {}) {
const abstractParts = [];
const middle = [];
const back = [];
const flags = { security: false, iana: false, terminology: false };
const stack = [{ level: 0, children: middle, bucket: 'middle' }];
function current() {
return stack[stack.length - 1];
}
for (let index = 0; index < flow.length; index++) {
const item = flow[index];
if (item.type !== 'heading') {
current().children.push(item);
continue;
}
const kind = classifyHeading(item.text);
if (kind === 'skip' || kind === 'references') {
const skipUntil = item.level;
while (index + 1 < flow.length && !(flow[index + 1].type === 'heading' && flow[index + 1].level <= skipUntil)) {
index++;
}
continue;
}
if (kind === 'abstract') {
while (index + 1 < flow.length && flow[index + 1].type !== 'heading') {
index++;
Eif (flow[index].xml) {
abstractParts.push(flow[index].xml);
}
}
continue;
}
if (options.title && item.level === 1 && item.text.trim() === String(options.title).trim()) {
continue;
}
while (stack.length > 1 && current().level >= item.level) {
stack.pop();
}
const section = {
type: 'section',
level: item.level,
name: item.text,
kind,
anchor: headingToAnchor(item.text),
children: []
};
Iif (kind === 'acknowledgements' || kind === 'appendix') {
back.push(section);
stack.push({ ...section, bucket: 'back' });
} else {
current().children.push(section);
stack.push(section);
}
if (kind === 'security') flags.security = true;
if (kind === 'iana') flags.iana = true;
if (kind === 'terminology') flags.terminology = true;
}
return { abstractParts, middle, back, flags };
}
function renderSection(section, ctx) {
const body = [];
for (const child of section.children || []) {
Iif (child.type === 'section') {
body.push(renderSection(child, ctx));
} else if (child.type === 'block' && child.xml) {
body.push(child.xml);
} else Eif (child.xml) {
body.push(child.xml);
}
}
if (section.kind === 'terminology' && ctx.terminologyDl) {
const withoutDup = body.filter(xml => !xml.includes('<dl') || xml.includes('Source:'));
body.length = 0;
body.push(...withoutDup);
body.push(ctx.terminologyDl);
}
const attrs = { anchor: section.anchor };
Iif (section.kind === 'appendix') {
attrs.numbered = 'true';
}
const inner = [el('name', escapeXml(section.name)), ...body];
return el('section', attrs, inner);
}
function wrapOrphanBlocks(nodes) {
const sections = nodes.filter(node => node.type === 'section');
const orphans = nodes.filter(node => node.type !== 'section');
if (!orphans.length) {
return sections;
}
const intro = sections.find(section => /^introduction$/i.test(section.name));
Iif (intro) {
intro.children.unshift(...orphans);
return sections;
}
sections.unshift({
type: 'section',
level: 2,
name: 'Introduction',
kind: 'body',
anchor: 'introduction',
children: orphans
});
return sections;
}
function renderSectionTree(nodes, ctx) {
return wrapOrphanBlocks(nodes).map(node => {
Eif (node.type === 'section') {
return renderSection(node, ctx);
}
if (node.type === 'block') {
return node.xml;
}
return '';
}).filter(Boolean);
}
module.exports = {
SKIP_HEADING_RE,
REFERENCES_HEADING_RE,
ABSTRACT_HEADING_RE,
TERMINOLOGY_HEADING_RE,
SECURITY_HEADING_RE,
IANA_HEADING_RE,
collectFlow,
classifyHeading,
nestSections,
renderSection,
wrapOrphanBlocks,
renderSectionTree,
walkOne,
wrapT,
isTermOnlyParagraph
};
|