Skip to content

Commit cf126ac

Browse files
feat: add data-wrap, data-line-nos and data-expand attributes (#41)
Co-authored-by: razonyang <razonyang@gmail.com>
1 parent 97456a0 commit cf126ac

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

assets/mods/code-block-panel/js/panel.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { default as params } from '@params';
22
import snackbar from 'mods/snackbar/js/index.ts';
33
import i18n from './i18n';
44

5+
const isTrue = (val: string | boolean): boolean => {
6+
if (typeof val === 'boolean') {
7+
return val
8+
}
9+
return val !== '' && val !== 'false' && val !== '0'
10+
}
11+
512
export default class Panel {
613
private highlight: HTMLElement
714

@@ -15,12 +22,16 @@ export default class Panel {
1522
}
1623

1724
init() {
18-
if (!params.line_nos) {
25+
const highlight = this.code.closest('.highlight')
26+
if (!isTrue(highlight?.getAttribute('data-line-nos') ?? params.line_nos)) {
1927
this.code.classList.add('code-no-ln')
2028
}
21-
if (params.wrap) {
29+
if (isTrue(highlight?.getAttribute('data-wrap') ?? params.wrap)) {
2230
this.code.classList.add('code-wrap')
2331
}
32+
if (isTrue(highlight?.getAttribute('data-expand') ?? params.expand)) {
33+
this.expand()
34+
}
2435

2536
this.pre = this.code.parentElement as HTMLElement
2637
this.highlight = this.pre.parentElement as HTMLElement
@@ -47,7 +58,7 @@ export default class Panel {
4758
return Array.from(this.code.querySelectorAll(':scope > span'))
4859
}
4960

50-
private maxHeight
61+
private maxHeight: string
5162

5263
private maxLines() {
5364
const lines = this.lines()

layouts/partials/code-block-panel/assets/js-resource.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"max_lines" 10
99
"line_nos" true
1010
"wrap" false
11+
"expand" false
1112
}}
1213
{{- $i18n := newScratch }}
1314
{{- $sites := .Sites }}

0 commit comments

Comments
 (0)