Skip to content

Commit 87a1bd3

Browse files
authored
docs: fix anchor links in rendered specs (OAI#3200)
Signed-off-by: Mike Ralphson <[email protected]>
1 parent 4bfca78 commit 87a1bd3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

scripts/md2html/md2html.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ function preface(title,options) {
7676
if (options.respec) {
7777
preface += '<script src="../js/respec-oai.js" class="remove"></script>';
7878
preface += `<script class="remove">var respecConfig = ${JSON.stringify(respec)};</script>`;
79-
preface += fs.readFileSync('./analytics/google.html','utf8');
79+
try {
80+
preface += fs.readFileSync('./analytics/google.html','utf8');
81+
}
82+
catch (ex) {}
8083
preface += '</head><body>';
8184
preface += '<style>';
8285
preface += '#respec-ui { visibility: hidden; }';
@@ -167,6 +170,7 @@ let indents = [0];
167170
// process the markdown
168171
for (let l in lines) {
169172
let line = lines[l];
173+
let linkTarget;
170174

171175
if (line.startsWith('## Table of Contents')) inTOC = true;
172176
if (line.startsWith('<!-- /TOC')) inTOC = false;
@@ -213,7 +217,9 @@ for (let l in lines) {
213217
let title = comp[1];
214218
if (inDefs) title = '<dfn>'+title+'</dfn>';
215219
let link = comp[0].split('<a ')[1].replace('name=','id=');
216-
line = ('#'.repeat(newIndent)+' <span '+link+title+'</span>');
220+
const anchor = link.split("'").join('"').split('"')[1];
221+
line = '#'.repeat(newIndent)+' <span>'+title+'</span>';
222+
linkTarget = '<a id="'+anchor+'"></a>';
217223
}
218224
else {
219225
let title = line.split('# ')[1];
@@ -303,7 +309,7 @@ for (let l in lines) {
303309
line = prefix+md.render(line);
304310
}
305311

306-
lines[l] = line;
312+
lines[l] = (linkTarget ? linkTarget : '') + line;
307313
}
308314

309315
s = preface(`OpenAPI Specification v${argv.subtitle} | Introduction, Definitions, & More`,argv)+'\n\n'+lines.join('\n');

0 commit comments

Comments
 (0)