Skip to content

Commit 413e5e8

Browse files
⚡ Handling of blank lines
1 parent b4c4bca commit 413e5e8

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/class/HTMLCodeBlockElement.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,16 @@ export default class HTMLCodeBlockElement extends HTMLElement {
104104
return;
105105
}
106106

107+
const src = (() => {
108+
if (/[^\n]\n$/.test(this.#value)) {
109+
return `${this.#value}\n`;
110+
}
111+
112+
return this.#value;
113+
})()
114+
107115
/** The resulting syntax-highlighted markup */
108-
const {value: markup} = HTMLCodeBlockElement.highlight(this.#value, {
116+
const {value: markup} = HTMLCodeBlockElement.highlight(src, {
109117
language: this.#language,
110118
});
111119

@@ -127,14 +135,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
127135
}
128136

129137
set value(src: any) {
130-
const _src = String(src);
131-
132-
if (/\n$/.test(_src)) {
133-
this.#value = `${_src}\n`;
134-
} else {
135-
this.#value = _src;
136-
}
137-
138+
this.#value = String(src);
138139
this.#render();
139140
}
140141

@@ -315,7 +316,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
315316
/* -------------------------------------------------------------------------
316317
* Hard private props initialize
317318
* ---------------------------------------------------------------------- */
318-
this.#value = (this.textContent || '').replace(/^\n/, '');
319+
this.#value = (this.textContent || '').replace(/^\n/, '').replace(/\n$/, '');
319320
this.#label = a11yName.textContent || '';
320321
this.#language = this.getAttribute('language') || '';
321322
this.#controls = this.getAttribute('controls') !== null;

0 commit comments

Comments
 (0)