import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filesDir = path.join(__dirname, "files");
const kjvPath = path.join(filesDir, "en_kjv.txt");
// Step 1: Extract fully-red verse IDs from KJV
// A "fully red" verse is one where the entire verse text (after stripping ¶ and whitespace) is wrapped in <red>...</red>
const kjvLines = fs.readFileSync(kjvPath, "utf-8").split("\n").filter(Boolean);
const fullyRedIds = new Set();
for (const line of kjvLines) {
if (!line.includes("<red>")) continue;
const parts = line.split("|");
const text = parts.slice(6).join("|");
// Strip leading pilcrow and whitespace
const stripped = text.replace(/^[¶\s]+/, "").trim();
// Check if the entire text is wrapped in a single <red>...</red>
if (/^<red>.*<\/red>$/.test(stripped)) {
const chapter = parts[2];
fullyRedIds.add(`${book}:${chapter}:${verse}`);
console.log(`Found ${fullyRedIds.size} fully-red verses in KJV`);
// Step 2: Apply <red> tags to all other bible files
const files = fs.readdirSync(filesDir).filter((f) => f.endsWith(".txt") && f !== "en_kjv.txt");
for (const file of files) {
const filePath = path.join(filesDir, file);
const lines = fs.readFileSync(filePath, "utf-8").split("\n");
const newLines = lines.map((line) => {
const parts = line.split("|");
if (parts.length < 6) return line;
const chapter = parts[2];
const id = `${book}:${chapter}:${verse}`;
if (fullyRedIds.has(id)) {
// The text portion starts at index 6
const prefix = parts.slice(0, 6).join("|");
const text = parts.slice(6).join("|");
// Don't double-wrap if already has red tags
if (text.includes("<red>")) return line;
// Preserve leading ¶ and whitespace
const match = text.match(/^([¶\s]*)(.*)/);
const leading = match[1] || "";
const content = match[2] || "";
return `${prefix}|${leading}<red>${content}</red>`;
fs.writeFileSync(filePath, newLines.join("\n"), "utf-8");
console.log(`${file}: tagged ${count} verses`);