Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remark: Add support for highlighting line numbers #815

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ 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:

```markdown
![code go {4-6}](../../examples/somefile.go)
```

### Backward compatibility

When you move, rename or delete previously published content, make sure that
Expand Down
2 changes: 2 additions & 0 deletions src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
--ifm-font-family-monospace: "Roboto Mono", monospace;
--ifm-global-radius: 0;
--ifm-alert-border-width: 5px;
--docusaurus-highlighted-code-line-bg: #dddfe1;
}

/* For readability concerns, you should choose a lighter palette in dark mode. */
Expand All @@ -29,6 +30,7 @@ html[data-theme='dark'] {
--ifm-color-primary-light: #00ffff;
--ifm-color-primary-lighter: #00ffff;
--ifm-color-primary-lightest: #00ffff;
--docusaurus-highlighted-code-line-bg: #646464;
}

/* Custom fonts */
Expand Down
69 changes: 68 additions & 1 deletion src/remark/code-block-snippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ test('line-numbers', () => {
visitor(node);

expect(node.type).toBe('code');
expect(node.lang).toBe('js');
expect(node.meta).toBe('title="Some title"');
expect(node.value.split("\n").length).toBe(3);
});
Expand All @@ -86,6 +85,74 @@ 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}',
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}');
});

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

visitor(node);

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

test('invalid-file', () => {
let node = {
type: 'paragraph',
Expand Down
6 changes: 6 additions & 0 deletions src/remark/code-block-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ 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);
if (lineNumber) {
node.meta = lineNumber[0] + (node.meta?" "+node.meta:"");
}

// Source filename can have fragment symbol # followed by the line number(s) or region name.
const [relSrcFilePath, fragment] = imgNode.url.split("#");

Expand Down
Loading