Skip to content

Commit ca8c07b

Browse files
committed
Fix highlight-directive-comments language matching
1 parent 0009ad7 commit ca8c07b

File tree

7 files changed

+85
-41
lines changed

7 files changed

+85
-41
lines changed

src/graphql/highlight.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ async function highlight(args, pluginOptions, { cache, createNodeId }) {
2525
...rest
2626
} = await plugin.once(() => setup(pluginOptions, cache));
2727

28-
const lineTransformers = getLineTransformers({
29-
theme,
30-
languageAliases,
31-
getLineClassName,
32-
wrapperClassName,
33-
...rest
34-
});
28+
const lineTransformers = await getLineTransformers(
29+
{
30+
theme,
31+
languageAliases,
32+
getLineClassName,
33+
wrapperClassName,
34+
...rest
35+
},
36+
cache
37+
);
3538

3639
const themeCache = await cache.get('themes');
3740
const grammarCache = await cache.get('grammars');

src/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ function createPlugin() {
4141
...rest
4242
} = await once(() => setup(options, cache), 'setup');
4343

44-
const lineTransformers = getLineTransformers({
45-
theme,
46-
wrapperClassName,
47-
languageAliases,
48-
extensions,
49-
getLineClassName,
50-
injectStyles,
51-
replaceColor,
52-
logLevel,
53-
...rest
54-
});
44+
const lineTransformers = await getLineTransformers(
45+
{
46+
theme,
47+
wrapperClassName,
48+
languageAliases,
49+
extensions,
50+
getLineClassName,
51+
injectStyles,
52+
replaceColor,
53+
logLevel,
54+
...rest
55+
},
56+
cache
57+
);
5558

5659
// 1. Gather all code fence nodes from Markdown AST.
5760

src/transformers/highlightDirectiveLineTransformer.js

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
// @ts-check
22
const { highlightLine } = require('./transformerUtils');
3+
const { getScope } = require('../storeUtils');
34

45
/**
56
* @param {string} language
6-
* @param {object} languageCommentMap
7+
* @param {string} scope
8+
* @param {Record<string, (message: string) => string>} languageCommentMap
79
* @return {(commentMessage: string) => string} curried function taking a string argument and
810
* prefixing/wrapping that with a language's comment syntax
911
*/
10-
const getCommentForLanguage = (language, languageCommentMap) => message => {
12+
const getCommentForLanguage = (language, scope, languageCommentMap) => message => {
1113
// example: languageCommentMap = {js: str => `// ${str}`}
12-
if (languageCommentMap[language]) return languageCommentMap[language](message);
13-
if (['js', 'ts', 'php', 'swift', 'c#', 'go', 'java', 'jsonc'].includes(language)) return `// ${message}`;
14-
if (['python', 'ruby', 'shell', 'perl', 'coffee', 'yaml'].includes(language)) return `# ${message}`;
15-
if (['css', 'c', 'cpp', 'objc', 'less'].includes(language)) return `/* ${message} */`;
16-
if (['html', 'xml', 'markdown'].includes(language)) return `<!-- ${message} -->`;
17-
if (['clojure'].includes(language)) return `; ${message}`;
18-
if (['sql'].includes(language)) return `-- ${message}`;
19-
if (['fortran'].includes(language)) return `! ${message}`;
20-
return `// ${message}`;
14+
if (languageCommentMap[language]) {
15+
return languageCommentMap[language](message);
16+
}
17+
18+
switch (scope) {
19+
case 'source.python':
20+
case 'source.ruby':
21+
case 'source.shell':
22+
case 'source.perl':
23+
case 'source.coffee':
24+
case 'source.yaml':
25+
return `# ${message}`;
26+
case 'source.css':
27+
case 'source.c':
28+
case 'source.cpp':
29+
case 'source.objc':
30+
case 'source.css.less':
31+
return `/* ${message} */`;
32+
case 'text.html.derivative':
33+
case 'text.xml':
34+
case 'text.html.markdown':
35+
return `<!-- ${message} -->`;
36+
case 'source.clojure':
37+
return `; ${message}`;
38+
case 'source.sql':
39+
return `-- ${message}`;
40+
default:
41+
return `// ${message}`;
42+
}
2143
};
2244

2345
/**
@@ -30,12 +52,15 @@ const textIsHighlightDirective = (text, commentWrapper) => directive =>
3052
['// ' + directive, commentWrapper(directive)].includes(text.trim());
3153

3254
/**
33-
* @param {object} languageCommentMap user-defined object mapping language keys to commenting functions
55+
* @param {Record<string, (message: string) => string>} languageCommentMap user-defined object mapping language keys to commenting functions
56+
* @param {Record<string, string>} languageAliases
57+
* @param {*} grammarCache
3458
*/
35-
function createHighlightDirectiveLineTransformer(languageCommentMap) {
59+
function createHighlightDirectiveLineTransformer(languageCommentMap, languageAliases, grammarCache) {
3660
/** @type {LineTransformer<HighlightCommentTransfomerState>} */
3761
const transformer = ({ line, language, state }) => {
38-
const commentWrapper = getCommentForLanguage(language, languageCommentMap);
62+
const scope = getScope(language, grammarCache, languageAliases);
63+
const commentWrapper = getCommentForLanguage(language, scope, languageCommentMap);
3964
const isDirective = textIsHighlightDirective(line.text, commentWrapper);
4065
if (isDirective('highlight-start')) {
4166
return { state: { inHighlightRange: true } }; // no `line` - drop this line from output

src/transformers/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@ const { createHighlightDirectiveLineTransformer } = require('./highlightDirectiv
44
const getTransformedLines = require('./getTransformedLines');
55

66
/**
7-
* @returns {LineTransformer[]}
7+
* @param {PluginOptions} pluginOptions
8+
* @param {GatsbyCache} cache
9+
* @returns {Promise<LineTransformer[]>}
810
*/
9-
function getDefaultLineTransformers() {
10-
return [createHighlightDirectiveLineTransformer({}), highlightMetaTransformer];
11+
async function getDefaultLineTransformers(pluginOptions, cache) {
12+
return [
13+
createHighlightDirectiveLineTransformer({}, pluginOptions.languageAliases, await cache.get('grammars')),
14+
highlightMetaTransformer
15+
];
1116
}
1217

1318
/**
1419
* @param {...LineTransformer} transformers
1520
*/
1621
function addLineTransformers(...transformers) {
17-
return () => [...transformers, ...getDefaultLineTransformers()];
22+
return getLineTransformers;
23+
24+
/**
25+
* @param {PluginOptions} pluginOptions
26+
* @param {GatsbyCache} cache
27+
*/
28+
async function getLineTransformers(pluginOptions, cache) {
29+
return [...transformers, ...(await getDefaultLineTransformers(pluginOptions, cache))];
30+
}
1831
}
1932

2033
module.exports = { getDefaultLineTransformers, addLineTransformers, getTransformedLines };

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ interface PluginOptions {
6363
replaceColor?: (colorValue: string, theme: string) => string;
6464
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
6565
host?: Host;
66-
getLineTransformers?: (pluginOptions: PluginOptions) => LineTransformer[];
66+
getLineTransformers?: (pluginOptions: PluginOptions, cache: GatsbyCache) => LineTransformer[] | Promise<LineTransformer[]>;
6767
}
6868

6969
interface GatsbyCache {

test/integration/baselines/highlight-directive-comments.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<pre class="grvsc-container default-dark" data-language="ts" data-index="0"><code class="grvsc-code"><span class="grvsc-line"><span class="mtk4">const</span><span class="mtk1"> </span><span class="mtk12">x</span><span class="mtk1"> = </span><span class="mtk7">0</span><span class="mtk1">;</span></span>
22
<span class="grvsc-line grvsc-line-highlighted"><span class="mtk4">const</span><span class="mtk1"> </span><span class="mtk12">y</span><span class="mtk1"> = </span><span class="mtk7">1</span><span class="mtk1">; </span></span>
33
<span class="grvsc-line"><span class="mtk4">const</span><span class="mtk1"> </span><span class="mtk12">z</span><span class="mtk1"> = </span><span class="mtk7">2</span><span class="mtk1">;</span></span></code></pre>
4-
<pre class="grvsc-container default-dark" data-language="shell" data-index="1"><code class="grvsc-code"><span class="grvsc-line">npm install</span>
5-
<span class="grvsc-line grvsc-line-highlighted">npm run build </span>
6-
<span class="grvsc-line">npm start</span></code></pre>
4+
<pre class="grvsc-container default-dark" data-language="sh" data-index="1"><code class="grvsc-code"><span class="grvsc-line"><span class="mtk1">npm install</span></span>
5+
<span class="grvsc-line grvsc-line-highlighted"><span class="mtk1">npm run build </span></span>
6+
<span class="grvsc-line"><span class="mtk1">npm start</span></span></code></pre>
77
<pre class="grvsc-container default-dark" data-language="js" data-index="2"><code class="grvsc-code"><span class="grvsc-line"><span class="mtk3">// This is where the interesting part is</span></span>
88
<span class="grvsc-line grvsc-line-highlighted"><span class="mtk4">function</span><span class="mtk1"> </span><span class="mtk11">somethingSuperInteresting</span><span class="mtk1">() {</span></span>
99
<span class="grvsc-line grvsc-line-highlighted"><span class="mtk1"> </span><span class="mtk10">console</span><span class="mtk1">.</span><span class="mtk11">log</span><span class="mtk1">(</span><span class="mtk4">this</span><span class="mtk1">);</span></span>

test/integration/cases/highlight-directive-comments/test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const y = 1; // highlight-line
44
const z = 2;
55
```
66

7-
```shell
7+
```sh
88
npm install
99
npm run build # highlight-line
1010
npm start

0 commit comments

Comments
 (0)