Skip to content

Commit

Permalink
Merge pull request #815 from oasisprotocol/matevz/feat/highlight-line…
Browse files Browse the repository at this point in the history
…-numbers

remark: Add support for highlighting line numbers
  • Loading branch information
matevz authored May 14, 2024
2 parents 7248385 + fba847f commit de807c4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
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

0 comments on commit de807c4

Please sign in to comment.