Skip to content

Commit

Permalink
remark: Fix line highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed May 15, 2024
1 parent dd2834f commit 0cb06ef
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ region name:
To define the region in the referenced file put `#region some-region-name` and
`#endregion some-region-name` as a comment line.

Highlighting specific lines also work:
Highlighting specific lines also work by passing them as a third parameter:

```markdown
![code go {4-6}](../../examples/somefile.go)
Expand Down
38 changes: 2 additions & 36 deletions src/remark/code-block-snippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,53 +85,19 @@ test('region', () => {
expect(node.value.split("\n").length).toBe(4);
});

test('highlight-line-numbers', () => {
let node = {
type: 'paragraph',
children: [{
type: 'image',
alt: 'code {4-6}',
url: 'src/remark/code-block-snippets.test.ts',
}],
};

visitor(node);

expect(node.type).toBe('code');
expect(node.meta).toBe('{4-6}');
});

test('highlight-line-numbers-title', () => {
let node = {
type: 'paragraph',
children: [{
type: 'image',
alt: 'code {4-6}',
url: 'src/remark/code-block-snippets.test.ts',
title: 'Some title',
}],
};

visitor(node);

expect(node.type).toBe('code');
expect(node.meta).toBe('{4-6} title="Some title"');
});

test('highlight-line-numbers-lang', () => {
let node = {
type: 'paragraph',
children: [{
type: 'image',
alt: 'code text {4-6}',
alt: 'code text 4-6', // {4-6} curly brackets are automatically stripped.
url: 'src/remark/code-block-snippets.test.ts',
}],
};

visitor(node);

expect(node.type).toBe('code');
expect(node.lang).toBe('text');
expect(node.meta).toBe('{4-6}');
});

Expand All @@ -140,7 +106,7 @@ test('highlight-line-numbers-lang-title', () => {
type: 'paragraph',
children: [{
type: 'image',
alt: 'code text {4-6}',
alt: 'code text 4-6', // {4-6} curly brackets are automatically stripped.
url: 'src/remark/code-block-snippets.test.ts',
title: 'Some title',
}],
Expand Down
6 changes: 3 additions & 3 deletions src/remark/code-block-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export function visitor(untypedNode: mdast.Node): asserts untypedNode is mdast.C
node.meta = `title="${imgNode.title}"`;
}

// Highlight line numbers, if {some-range,and,some,more,lines} syntax used.
const lineNumber = imgNode.alt.match(/(\{.*\})/g);
// Highlight line numbers, if the third argument is passed. The curly brackets are already stripped. e.g. some-range,and,some,more,lines
const lineNumber = imgNode.alt.split(" ")[2];
if (lineNumber) {
node.meta = lineNumber[0] + (node.meta?" "+node.meta:"");
node.meta = `{${lineNumber}}${node.meta?" "+node.meta:""}`;
}

// Source filename can have fragment symbol # followed by the line number(s) or region name.
Expand Down

0 comments on commit 0cb06ef

Please sign in to comment.