diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-06 21:11:39 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-07 16:15:56 +0800 |
commit | 447311b0614cd09e29a55e46c38153ea9a1b3247 (patch) | |
tree | 97d2dd9f1389ffc7430f79a1521cda0c1ef2206b | |
parent | 650a1667d4769df876af5115594f4043d1d5a552 (diff) |
feat: fix top padding of reference style links
-rw-r--r-- | src/editor.mjs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/editor.mjs b/src/editor.mjs index 8fe8c0b..71e66be 100644 --- a/src/editor.mjs +++ b/src/editor.mjs | |||
@@ -12,6 +12,8 @@ const context = document.querySelector('[data-mode]') | |||
12 | const dumbyContainer = document.querySelector('.DumbyMap') | 12 | const dumbyContainer = document.querySelector('.DumbyMap') |
13 | const textArea = document.querySelector('.editor textarea') | 13 | const textArea = document.querySelector('.editor textarea') |
14 | let dumbymap | 14 | let dumbymap |
15 | |||
16 | const refLinkPattern = /\[([^\[\]]+)\]:\s+(.+)/ | ||
15 | let refLinks = [] | 17 | let refLinks = [] |
16 | 18 | ||
17 | /** | 19 | /** |
@@ -196,7 +198,7 @@ const cm = editor.codemirror | |||
196 | const getRefLinks = () => editor.value() | 198 | const getRefLinks = () => editor.value() |
197 | .split('\n') | 199 | .split('\n') |
198 | .map(line => { | 200 | .map(line => { |
199 | const [, ref, link] = line.match(/\[([^\[\]]+)\]:\s+(.+)/) ?? [] | 201 | const [, ref, link] = line.match(refLinkPattern) ?? [] |
200 | return { ref, link } | 202 | return { ref, link } |
201 | }) | 203 | }) |
202 | .filter(({ ref, link }) => ref && link) | 204 | .filter(({ ref, link }) => ref && link) |
@@ -432,7 +434,11 @@ const menuForEditor = (event, menu) => { | |||
432 | } | 434 | } |
433 | while (refLinks.find(({ref}) => ref === anchorName)) | 435 | while (refLinks.find(({ref}) => ref === anchorName)) |
434 | 436 | ||
435 | cm.replaceRange(`\n[${anchorName}]: ${link}`, { line: Infinity }) | 437 | const lastLineIsRefLink = cm.getLine(cm.lastLine()).match(refLinkPattern) |
438 | cm.replaceRange( | ||
439 | `${lastLineIsRefLink ? '' : '\n'}\n[${anchorName}]: ${link}`, | ||
440 | { line: Infinity } | ||
441 | ) | ||
436 | } | 442 | } |
437 | }) | 443 | }) |
438 | menu.insertBefore(item, menu.firstChild) | 444 | menu.insertBefore(item, menu.firstChild) |