Skip to content

Commit 06f6089

Browse files
authored
Merge pull request #15 from bee-san/claude/add-newline-support-d8Yr3
Add \n support for line breaks in custom dict definitions
2 parents e30c135 + 502bc42 commit 06f6089

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

yomitan-dict-builder/static/custom/app.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function parseEntries(rawText) {
9494

9595
const term = line.slice(0, firstComma).trim();
9696
const reading = line.slice(firstComma + 1, secondComma).trim();
97-
const definition = line.slice(secondComma + 1).trim();
97+
const definition = line.slice(secondComma + 1).trim().replace(/\\n/g, "\n");
9898
if (!term) continue;
9999
entries.push({ term, reading, definition });
100100
}
@@ -121,9 +121,21 @@ async function buildZip(dictName, entries) {
121121
for (let i = 0; i < entries.length; i += TERM_LIMIT) {
122122
const chunk = entries.slice(i, i + TERM_LIMIT);
123123
const bankIndex = Math.floor(i / TERM_LIMIT) + 1;
124-
const bank = chunk.map(({ term, reading, definition }) =>
125-
[term, reading, TAG_NAME, "", 0, [definition], 0, ""]
126-
);
124+
const bank = chunk.map(({ term, reading, definition }) => {
125+
let defEntry;
126+
if (definition.includes("\n")) {
127+
const parts = definition.split("\n");
128+
const content = [];
129+
parts.forEach((part, i) => {
130+
if (i > 0) content.push({ tag: "br" });
131+
content.push(part);
132+
});
133+
defEntry = [{ type: "structured-content", content }];
134+
} else {
135+
defEntry = [definition];
136+
}
137+
return [term, reading, TAG_NAME, "", 0, defEntry, 0, ""];
138+
});
127139
zip.file(`term_bank_${bankIndex}.json`, JSON.stringify(bank));
128140
}
129141

@@ -177,7 +189,19 @@ async function parseZip(file) {
177189
const term = entry[0] ?? "";
178190
const reading = entry[1] ?? "";
179191
const defs = entry[5];
180-
const definition = Array.isArray(defs) ? defs.join(" / ") : (defs ?? "");
192+
const definition = Array.isArray(defs)
193+
? defs.map(d => {
194+
if (typeof d === "string") return d;
195+
if (d && d.type === "structured-content" && Array.isArray(d.content)) {
196+
return d.content.map(p => {
197+
if (typeof p === "string") return p;
198+
if (p && p.tag === "br") return "\\n";
199+
return "";
200+
}).join("");
201+
}
202+
return String(d ?? "");
203+
}).join(" / ")
204+
: (defs ?? "");
181205
lines.push(`${term}, ${reading}, ${definition}`);
182206
}
183207
}

yomitan-dict-builder/static/custom/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h1>Bee's <span>Custom Yomitan Dict</span> Maker</h1>
5050
<div class="editor-footer">
5151
<span class="hint">
5252
Format: <code>term, reading, definition</code> &mdash;
53-
commas in definitions are fine
53+
commas in definitions are fine &middot; use <code>\n</code> for line breaks
5454
</span>
5555
<span id="entry-count">0 entries</span>
5656
</div>

0 commit comments

Comments
 (0)