# add-lines-myanmar.js

```javascript
#!/usr/bin/env -S obsidian-script --language=javascript

/**
 * add-lines-myanmar.js
 * 
 * Thêm dòng trống giữa các đoạn trong file markdown chứa Myanmar text.
 * 
 * Nguyên tắc:
 *   Nếu một dòng kết thúc bằng dấu chấm câu Myanmar (၊ hoặc ။)
 *   và dòng tiếp theo không trống, không phải page header (## PAGE),
 *   và giữa chúng chưa có dòng trống → thêm dòng trống.
 * 
 * Cách dùng (Obsidian Templater / QuickAdd):
 *   1. Mở file .md cần xử lý
 *   2. Chạy script này
 *   3. File được cập nhật (có backup .bak)
 * 
 * Cách dùng (CLI với Node.js):
 *   node add-lines-myanmar.js <file.md>
 * 
 * @author Panna (System Architect)
 * @version 1.0
 */

// ── Config ──────────────────────────────────────────────────────────────────
// Myanmar sentence-ending punctuation: , (comma) and ။ (full stop)
const MYANMAR_EOL_CHARS = new Set([
  '\u104A',  // ၊  Myanmar comma
  '\u104B',  // ။  Myanmar full stop / period
]);
const PAGE_HEADER_RE = /^##\s+PAGE\s+\d+/;
const BACKUP_SUFFIX = '.bak';

// ── Core logic ──────────────────────────────────────────────────────────────

/**
 * Add blank lines after Myanmar punctuation (၊ ။) when next line is not blank.
 * @param {string} content - File content
 * @returns {string} Updated content
 */
function addParagraphBreaks(content) {
  const lines = content.split('\n');
  const result = [];
  let modified = false;

  for (let i = 0; i < lines.length; i++) {
    result.push(lines[i]);

    // Skip empty lines, page headers, and last line
    const trimmed = lines[i].trimEnd();
    if (
      !trimmed ||
      PAGE_HEADER_RE.test(trimmed) ||
      i === lines.length - 1
    ) {
      continue;
    }

    // Check if line ends with Myanmar punctuation (၊ hoặc ။)
    const lastChar = trimmed[trimmed.length - 1];
    if (!lastChar || !MYANMAR_EOL_CHARS.has(lastChar)) {
      continue;
    }

    // Check next line(s) — if next line is empty or page header, skip
    const nextLine = lines[i + 1]?.trim();
    if (!nextLine || PAGE_HEADER_RE.test(nextLine)) {
      continue;
    }

    // Insert blank line
    result.push('');
    modified = true;
  }

  // Remove trailing blank lines (keep at most 1)
  while (result.length > 1 && result[result.length - 1] === '' && result[result.length - 2] === '') {
    result.pop();
  }

  return { text: result.join('\n'), modified };
}

// ── CLI runner ──────────────────────────────────────────────────────────────

function run() {
  const args = process.argv.slice(2);
  if (args.length === 0) {
    console.error('Usage: node add-lines-myanmar.js <file.md>');
    process.exit(1);
  }

  const fs = require('fs');
  const path = require('path');
  const filePath = path.resolve(args[0]);

  if (!fs.existsSync(filePath)) {
    console.error(`❌ File not found: ${filePath}`);
    process.exit(1);
  }

  const content = fs.readFileSync(filePath, 'utf-8');
  const { text, modified } = addParagraphBreaks(content);

  if (!modified) {
    console.log('✓ No changes needed — all paragraphs already separated.');
    process.exit(0);
  }

  // Backup
  const bakPath = filePath + BACKUP_SUFFIX;
  fs.copyFileSync(filePath, bakPath);
  console.log(`📦 Backup: ${bakPath}`);

  // Write
  fs.writeFileSync(filePath, text, 'utf-8');
  console.log(`✅ Updated: ${filePath}`);
  console.log(`   Lines: ${content.split('\n').length} → ${text.split('\n').length}`);
}

// ── Export for Obsidian ─────────────────────────────────────────────────────

if (typeof module !== 'undefined' && typeof require !== 'undefined') {
  module.exports = { addParagraphBreaks };
}

// ── Run if CLI ──────────────────────────────────────────────────────────────

if (typeof require !== 'undefined' && require.main === module) {
  run();
}
```
