Skip to content

Commit b7e7794

Browse files
author
Jesse Haigh
committed
initial implementation for wrapping and highlighting lines in code listings
1 parent 2668aa4 commit b7e7794

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/components/ContentNode.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ function renderNode(createElement, references) {
275275
content: node.code,
276276
showLineNumbers: node.showLineNumbers,
277277
copyToClipboard: node.copyToClipboard ?? false,
278+
wrap: node.wrap ?? 0,
279+
highlightedLines: node.highlight ?? [],
278280
};
279281
return createElement(CodeListing, { props });
280282
}

src/components/ContentNode/CodeListing.vue

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<div
1313
class="code-listing"
1414
:data-syntax="syntaxNameNormalized"
15-
:class="{ 'single-line': syntaxHighlightedLines.length === 1 }"
15+
:class="{ 'single-line': syntaxHighlightedLines.length === 1, 'is-wrapped': wrap > 0 }"
16+
:style="wrapStyle"
1617
>
1718
<Filename
1819
v-if="fileName"
@@ -103,6 +104,14 @@ export default {
103104
type: Boolean,
104105
default: () => false,
105106
},
107+
wrap: {
108+
type: Number,
109+
required: true,
110+
},
111+
highlightedLines: {
112+
type: Array,
113+
required: true,
114+
},
106115
startLineNumber: {
107116
type: Number,
108117
default: () => 1,
@@ -129,6 +138,22 @@ export default {
129138
copyableText() {
130139
return this.content.join('\n');
131140
},
141+
highlightSet() {
142+
const arr = this.highlightedLines || [];
143+
return new Set(arr.map(Number).filter(n => Number.isFinite(n) && n > 0));
144+
},
145+
wrapStyle() {
146+
const style = {};
147+
if (this.wrap > 0) {
148+
style.whiteSpace = 'pre-wrap';
149+
style.wordBreak = 'break-word';
150+
style.overflowWrap = 'anywhere';
151+
style['--wrap-ch'] = String(this.wrap);
152+
} else {
153+
style.whiteSpace = 'pre';
154+
}
155+
return style;
156+
},
132157
},
133158
watch: {
134159
content: {
@@ -138,7 +163,10 @@ export default {
138163
},
139164
methods: {
140165
isHighlighted(index) {
141-
return this.highlightedLineNumbers.has(this.lineNumberFor(index));
166+
const lineNumber = this.lineNumberFor(index);
167+
const h1 = this.highlightedLineNumbers.has(lineNumber);
168+
const h2 = this.highlightSet.has(lineNumber);
169+
return (h1 || h2);
142170
},
143171
// Returns the line number for the line at the given index in `content`.
144172
lineNumberFor(index) {
@@ -249,6 +277,17 @@ code {
249277
}
250278
}
251279
280+
.is-wrapped pre,
281+
.is-wrapped code {
282+
white-space: pre-wrap;
283+
overflow-wrap: anywhere;
284+
word-break: break-word;
285+
}
286+
287+
.is-wrapped pre {
288+
max-width: calc(var(--wrap-ch) * 1ch);
289+
}
290+
252291
.container-general {
253292
overflow: auto;
254293
}

0 commit comments

Comments
 (0)