diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2024-09-13 18:52:01 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2024-09-13 18:59:13 +0800 |
| commit | f1f5462fadd410dbbb6c284481b6ca7219313e64 (patch) | |
| tree | 3ec7d26eb68aca7ebc774ea8c934061dcb0c744e /src | |
| parent | 63a0a58a4b7ae086a887ffabb6c764963e62cd2e (diff) | |
feat: Style code block even line is empty
Diffstat (limited to 'src')
| -rw-r--r-- | src/css/index.css | 3 | ||||
| -rw-r--r-- | src/editor.mjs | 19 |
2 files changed, 20 insertions, 2 deletions
diff --git a/src/css/index.css b/src/css/index.css index 93a4918..0997779 100644 --- a/src/css/index.css +++ b/src/css/index.css | |||
| @@ -64,7 +64,8 @@ body { | |||
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | /* FIXME For those empty line (no child with cm-comment) */ | 66 | /* FIXME For those empty line (no child with cm-comment) */ |
| 67 | .CodeMirror-line:has(.cm-comment) { | 67 | .inside-code-block, |
| 68 | .CodeMirror-line:has(.cm-formatting-code-block) { | ||
| 68 | background: rgba(0,0,0,.05) !important; | 69 | background: rgba(0,0,0,.05) !important; |
| 69 | 70 | ||
| 70 | .cm-comment { | 71 | .cm-comment { |
diff --git a/src/editor.mjs b/src/editor.mjs index 5470b64..d1b4ba1 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
| @@ -89,10 +89,27 @@ if (queryParams.get('render')) { | |||
| 89 | toggleMaps(HtmlContainer) | 89 | toggleMaps(HtmlContainer) |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | // Quick hack to style lines inside code block | ||
| 93 | const addClassToCodeLines = () => { | ||
| 94 | const lines = cm.getLineHandle(0).parent.lines | ||
| 95 | let insideCodeBlock = false | ||
| 96 | lines.forEach((line, index) => { | ||
| 97 | if (line.text.match(/^````*/)) { | ||
| 98 | insideCodeBlock = !insideCodeBlock | ||
| 99 | } else if (insideCodeBlock) { | ||
| 100 | cm.addLineClass(index, "text", "inside-code-block") | ||
| 101 | } else { | ||
| 102 | cm.removeLineClass(index, "text", "inside-code-block") | ||
| 103 | } | ||
| 104 | }) | ||
| 105 | } | ||
| 106 | addClassToCodeLines() | ||
| 107 | |||
| 92 | // Re-render HTML by editor content | 108 | // Re-render HTML by editor content |
| 93 | cm.on("change", () => { | 109 | cm.on("change", (_, obj) => { |
| 94 | markdown2HTML(HtmlContainer, editor.value()) | 110 | markdown2HTML(HtmlContainer, editor.value()) |
| 95 | createDocLinks(HtmlContainer) | 111 | createDocLinks(HtmlContainer) |
| 112 | addClassToCodeLines() | ||
| 96 | }) | 113 | }) |
| 97 | // }}} | 114 | // }}} |
| 98 | 115 | ||