Skip to content

Commit

Permalink
Merge pull request OAI#4107 from ralfhandl/fix/respec-erratic-syntax-…
Browse files Browse the repository at this point in the history
…highlighting

ReSpec build script: no automatic syntax highlighting for unspecified or "unknown" languages
  • Loading branch information
handrews authored Sep 26, 2024
2 parents 0e3eec8 + 281116b commit e9b9785
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
38 changes: 37 additions & 1 deletion scripts/md2html/md2html.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,41 @@ const path = require('path');
const url = require('url');

const hljs = require('highlight.js');
hljs.registerLanguage('uritemplate', function() {
return {
case_insensitive: true,
contains: [
{
scope: "attr",
match: /(?<=[{,])[^,}\n\r]+/,
}
],
}
});
hljs.registerLanguage('uri', function() {
return {
case_insensitive: true,
classNameAliases: {
pathsegment: "attr",
option: "attr",
value: "literal"
},
contains: [
{
scope: "pathsegment",
match: /(?<=[/])[^/?#\n\r]+/,
},
{
scope: "option",
match: /(?<=[?&#])[^=?&#\n\r]+/,
},
{
scope: "value",
match: /(?<=\=)[^?&#\n\r]+/,
}
],
}
});
const cheerio = require('cheerio');

let argv = require('yargs')
Expand All @@ -36,7 +71,8 @@ const md = require('markdown-it')({
'</code></pre>';
}

return '<pre class="highlight '+lang+'" tabindex="0"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
if (lang) console.warn('highlight.js does not support language',lang);
return '<pre class="nohighlight" tabindex="0"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
}
});

Expand Down
8 changes: 7 additions & 1 deletion tests/md2html/fixtures/basic-new.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
</code></pre>
<pre class="nohighlight" tabindex="0"><code>text/plain
</code></pre>
<pre class="highlight " tabindex="0"><code>no language
<pre class="nohighlight" tabindex="0"><code>no language
</code></pre>
<pre class="nohighlight" tabindex="0"><code>unknown language
</code></pre>
<pre class="nohighlight" tabindex="0"><code>https://<span class="hljs-attr">foo.com</span>/<span class="hljs-attr">bar</span>?<span class="hljs-attr">baz</span>=<span class="hljs-literal">qux</span>&amp;<span class="hljs-attr">fred</span>=<span class="hljs-literal">waldo</span>#<span class="hljs-attr">fragment</span>
</code></pre>
<pre class="nohighlight" tabindex="0"><code>https://foo.com/bar{<span class="hljs-attr">?baz*</span>,<span class="hljs-attr">qux</span>}
</code></pre>
</section></section><section class="appendix"><h1>Appendix A: Revision History</h1>
<table>
Expand Down
12 changes: 12 additions & 0 deletions tests/md2html/fixtures/basic-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ text/plain
no language
```

```unknown
unknown language
```

```uri
https://foo.com/bar?baz=qux&fred=waldo#fragment
```

```uritemplate
https://foo.com/bar{?baz*,qux}
```

## Appendix A: Revision History

Version | Date
Expand Down

0 comments on commit e9b9785

Please sign in to comment.