dsh-half-page 0.3.30 → 0.3.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +17 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -205,15 +205,27 @@ export async function appendNote(dir, bookTitle, chapterTitle, lines, by) {
|
|
|
205
205
|
const p = await notesPath(dir)
|
|
206
206
|
let raw = ''
|
|
207
207
|
try { raw = await readFile(p, 'utf8') } catch {}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
raw += `\n### ${chapterTitle}\n` + lines.map((l) => `- ${l}`).join('\n') + '\n'
|
|
208
|
+
const esc = bookTitle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
209
|
+
let block = `\n### ${chapterTitle}\n` + lines.map((l) => `- ${l}`).join('\n') + '\n'
|
|
212
210
|
if (by) {
|
|
213
211
|
const d = new Date()
|
|
214
212
|
const pad = (n) => String(n).padStart(2, '0')
|
|
215
213
|
const ts = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
|
|
216
|
-
|
|
214
|
+
block += `- \u21b3 \u6765\u81ea\uff1a${by} \u00b7 ${ts}\n`
|
|
215
|
+
}
|
|
216
|
+
const headRe = new RegExp(`^##\\s+${esc}\\s*$`, 'm')
|
|
217
|
+
const m = headRe.exec(raw)
|
|
218
|
+
if (!m) {
|
|
219
|
+
raw = (raw.trim() ? raw.replace(/\s*$/, '') + '\n\n' : '') + `## ${bookTitle}\n` + block
|
|
220
|
+
} else {
|
|
221
|
+
const at0 = m.index + m[0].length
|
|
222
|
+
const next = raw.slice(at0).search(/^##\s+/m)
|
|
223
|
+
if (next < 0) {
|
|
224
|
+
raw = raw.replace(/\s*$/, '') + '\n' + block
|
|
225
|
+
} else {
|
|
226
|
+
const at = at0 + next
|
|
227
|
+
raw = raw.slice(0, at).replace(/\s*$/, '') + '\n' + block + '\n' + raw.slice(at)
|
|
228
|
+
}
|
|
217
229
|
}
|
|
218
230
|
await writeFile(p, raw, 'utf8')
|
|
219
231
|
return p
|