Skip to content

Commit 4cbb078

Browse files
committed
Update md2html.js
1 parent 75e7f5e commit 4cbb078

1 file changed

Lines changed: 58 additions & 47 deletions

File tree

scripts/md2html/md2html.js

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ complete control over formatting and syntax highlighting */
55
'use strict';
66

77
/**
8-
@author Mike Ralphson <mike.ralphson@gmail.com>
9-
**/
8+
* @author Mike Ralphson <mike.ralphson@gmail.com>
9+
**/
1010

1111
const fs = require('fs');
1212
const path = require('path');
@@ -34,7 +34,7 @@ const md = require('markdown-it')({
3434
linkify: true,
3535
typographer: true,
3636
highlight: function (str, lang) {
37-
if (lang && hljs.getLanguage(lang)) { // && !argv.respec) {
37+
if (lang && hljs.getLanguage(lang)) {
3838
try {
3939
return '<pre class="nohighlight"><code>' +
4040
hljs.highlight(str, { language: lang }).value +
@@ -201,68 +201,31 @@ let indents = [0];
201201
for (let l in lines) {
202202
let line = lines[l];
203203

204+
// remove TOC from older spec versions, respec will generate a new one
204205
if (line.startsWith('## Table of Contents')) inTOC = true;
205206
if (line.startsWith('<!-- /TOC')) inTOC = false;
206207
if (inTOC) line = '';
207208

209+
// special formatting for Definitions section
208210
if (line.startsWith('## Definitions')) {
209211
inDefs = true;
210212
bsFix = false;
211213
}
212214
else if (line.startsWith('## ')) inDefs = false;
213215

216+
// recognize code blocks
214217
if (line.startsWith('```')) {
215218
inCodeBlock = !inCodeBlock;
216219
line += '\n'; // fixes formatting of first line of syntax-highlighted blocks
217220
}
218221

219-
if (!inCodeBlock && line.startsWith('#')) {
220-
let indent = 0;
221-
while (line[indent] === '#') indent++;
222-
let originalIndent = indent;
223-
224-
let prevIndent = indents[indents.length-1]; // peek
225-
let delta = indent-prevIndent;
226-
227-
if (!argv.respec) {
228-
if (delta===0) indent = lastIndent
229-
else if (delta<0) indent = lastIndent-1
230-
else if (delta>0) indent = lastIndent+1;
231-
}
232-
233-
if (indent < 0) {
234-
indent = 1;
235-
}
236-
if (argv.respec && (indent > 1)) {
237-
indent--;
238-
}
239-
let newIndent = indent;
240-
if (!argv.respec && (indent <= 2) && bsFix) {
241-
newIndent++;
242-
}
243-
244-
let title = line.split('# ')[1];
245-
if (inDefs) title = '<dfn>'+title+'</dfn>';
246-
line = ('#'.repeat(newIndent)+' '+title);
247-
248-
if (delta>0) indents.push(originalIndent);
249-
if (delta<0) {
250-
let d = Math.abs(delta);
251-
while (d>0) {
252-
indents.pop();
253-
d--;
254-
}
255-
}
256-
lastIndent = indent;
257-
}
258-
259222
if (line.indexOf('<a name="')>=0) {
260223
if (line.indexOf('<a name="parameterAllowEmptyValue"/>')>=0)
261224
// fix syntax error in 2.0.md
262225
line = line.replace('<a name="parameterAllowEmptyValue"/>','<span id="parameterAllowEmptyValue"></span>');
263226
else {
264-
line = line.replace('<a name=','<span id=');
265-
line = line.replace('</a>','</span>');
227+
// replace deprecated <a name="..."></a> with <span id="..."></span>
228+
line = line.replace(/<a name="([^"]+)"><\/a>/g,'<span id="$1"></span>');
266229
}
267230
}
268231

@@ -323,6 +286,8 @@ for (let l in lines) {
323286
line = line.replace(/YAML version \[1\.2\]\(https:\/\/(www\.)?yaml\.org\/spec\/1\.2\/spec\.html\)/,'[[YAML|YAML version 1.2]]');
324287
}
325288

289+
// fix relative links (to examples)
290+
//TODO: adjust when moving examples to a different repo
326291
if (!inCodeBlock && line.indexOf('](../') >= 0) {
327292
const regExp = /\((\.\.[^)]+)\)/g;
328293
line = line.replace(regExp,function(match,group1){
@@ -331,6 +296,50 @@ for (let l in lines) {
331296
});
332297
}
333298

299+
// fix indentation of headings
300+
// - make sure that each heading is at most one level deeper than the previous heading
301+
// - reduce heading level by one if we're in respec mode except for h1
302+
if (!inCodeBlock && line.startsWith('#')) {
303+
let indent = 0;
304+
while (line[indent] === '#') indent++;
305+
let originalIndent = indent;
306+
307+
let prevIndent = indents[indents.length-1]; // peek
308+
let delta = indent-prevIndent;
309+
310+
if (!argv.respec) {
311+
if (delta===0) indent = lastIndent
312+
else if (delta<0) indent = lastIndent-1
313+
else if (delta>0) indent = lastIndent+1;
314+
}
315+
316+
if (indent < 0) {
317+
indent = 1;
318+
}
319+
if (argv.respec && (indent > 1)) {
320+
indent--;
321+
}
322+
let newIndent = indent;
323+
if (!argv.respec && (indent <= 2) && bsFix) {
324+
newIndent++;
325+
}
326+
327+
let title = line.split('# ')[1];
328+
if (inDefs) title = '<dfn>'+title+'</dfn>';
329+
line = ('#'.repeat(newIndent)+' '+title);
330+
331+
if (delta>0) indents.push(originalIndent);
332+
if (delta<0) {
333+
let d = Math.abs(delta);
334+
while (d>0) {
335+
indents.pop();
336+
d--;
337+
}
338+
}
339+
lastIndent = indent;
340+
}
341+
342+
// wrap section text in <section>...</section> tags for respec
334343
if (!inCodeBlock && argv.respec && line.startsWith('#')) {
335344
let heading = 0;
336345
while (line[heading] === '#') heading++;
@@ -342,8 +351,10 @@ for (let l in lines) {
342351
const m = line.match(/# Version ([0-9.]+)$/);
343352
if (m) {
344353
// our conformance section is headlined with 'Version x.y.z'
354+
// and respec needs a conformance section in a "formal" specification
345355
newSection = '<section class="override" id="conformance">';
346-
// adjust the heading to be at level 2
356+
// adjust the heading to be at level 2 because respec insists on h2 here
357+
// Note: older specs had this at h4, newer specs at h2, and all heading levels have been reduced by 1 in the preceding block
347358
line = '#' + m[0];
348359
delta = 1;
349360
heading = 2;
@@ -353,7 +364,7 @@ for (let l in lines) {
353364
}
354365

355366
// heading level delta is either 0 or is +1/-1, or we're in respec mode
356-
/* respec insists on <section>...</section> breaks around headings */
367+
// respec insists on <section>...</section> breaks around headings
357368

358369
if (delta === 0) {
359370
prefix = '</section>'+newSection;

0 commit comments

Comments
 (0)