Skip to content

Commit 203d182

Browse files
Support for line highlighting in code blocks (#206)
(Color comes from Needle, but cannot use the css color vars directly because they embed an opacity of 1, which we want to be 0.5)
1 parent 0ea4eda commit 203d182

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

preview-src/code.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ line of code <!--4-->
4747
<4> A callout behind a line comment for XML or SGML languages like HTML.
4848

4949

50+
== Highlight lines
51+
52+
Highlight one or more lines of code by adding e.g. `//mark-line` at the end of each of them. Use the line comment syntax for the language the block contains.
53+
54+
[source, adoc]
55+
----
56+
[source, java]
57+
---
58+
var result = driver.executableQuery("MATCH (p:Person {name: $name})")
59+
.withParameters(Map.of("name", "Alice")) //mark-line
60+
.withConfig(QueryConfig.builder().withDatabase("neo4j").build())
61+
.execute();
62+
---
63+
----
64+
65+
[source, java]
66+
----
67+
var result = driver.executableQuery("MATCH (p:Person {name: $name})")
68+
.withParameters(Map.of("name", "Alice")) //mark-line
69+
.withConfig(QueryConfig.builder().withDatabase("neo4j").build())
70+
.execute();
71+
----
72+
73+
5074
== Expand code block
5175

5276
Code blocks longer than 15 lines (+5 of tolerance) are collapsed, unless you add `role=nocollapse`

src/css/code.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,8 @@
367367
.code-walkthrough .toc {
368368
display: none;
369369
}
370+
371+
.highlight .highlight-line {
372+
background: var(--mark-background);
373+
}
370374
}

src/css/vars.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
--tip-background-color: rgba(var(--theme-light-palette-primary-bg-weak));
252252
--warning-color: rgba(var(--theme-light-palette-danger-bg-strong));
253253
--warning-background-color: rgba(var(--theme-light-palette-danger-bg-weak));
254-
--mark-background: rgba(var(--colors-highlights-yellow));
254+
--mark-background: rgba(247 151 103 / 0.3);
255255

256256
/* success */
257257
--success-color: rgba(var(--theme-light-palette-success-bg-strong));

src/js/vendor/highlight.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,12 @@
128128
}
129129
})
130130
hljs.highlightAll()
131+
132+
// Apply line highlighting to lines marked with `// marked-line`
133+
// eat away line breaks or they would be doubled by the div
134+
hljs.addPlugin({
135+
'after:highlightElement': ({ el, result, text }) => {
136+
result.value = result.value.replaceAll(/^(\s*)(.+?)\s*<span class="hljs-comment">.*?\bmark-line\b.*?<\/span>\n/mg, '<div class="highlight-line">$1$2</div>')
137+
}
138+
});
131139
})()

0 commit comments

Comments
 (0)