书库三格式同步
unlistedby HuaiShuRan
维护每本书的 txt、md、epub 三格式:统一章节名和 2em 首行缩进,支持按书配置输出目录、文件列表右键操作与本地章节标题建议,写入前校验、覆盖前备份。
book-format-sync
Obsidian plugin for maintaining a book library where every book has three formats:
md + txt + epub
The plugin normalizes chapter headings, enforces a consistent 2em first-line indent for body paragraphs, converts from any one format to the other two, and can suggest Chinese chapter titles for numeric/placeholder headings.
Project handoff
- PROJECT_STATE.md — current status, confirmed behavior, architecture, tests, release process, known limitations, and next steps.
- AGENTS.md — short onboarding checklist for coding agents working in this repository.
Features
- Chapter heading normalization:
第1章/第十章/第12章→第1章 …; chapter numbers are never zero-padded:第1章,第10章,第101章. - Same-line TXT chapters: a line like
第1章 道可道…is split into a number-only heading plus body when the remainder is long or contains multiple sentence endings; the whole chapter is not treated as a title. - Consistent paragraph indentation:
- Markdown: frontmatter
cssclasses: book-format-sync; reading view usesp, editor/live preview uses.cm-line; no entities in the source. - TXT: two full-width spaces
- EPUB:
text-indent: 2em
- Markdown: frontmatter
- Three-format conversion: from
md,txt, orepub, generate the other formats without modifying the source file. - Local chapter title suggestions: detect empty, numeric, placeholder, duplicated titles; generate suggestions from the first sentence/paragraph locally. No AI, no network, no API key.
- Clean properties UI: in notes carrying
book-format-sync, the plugin hides only thecssclassesproperty row; the YAML field and other properties remain intact. - Right-click menu on
.md,.txt,.epubin the file explorer. - Safe writes: validate and re-parse all three outputs before writing; back up existing targets under
.book-format-sync/backups/. - Title suggestion cache under
.book-format-sync/titles/; manually edited titles are not overwritten.
Requirements
- Obsidian desktop (the plugin uses Node
zliband binary file access). - Obsidian
1.5.0or later. - No runtime dependencies beyond Obsidian and Node built-ins.
Installation
Manual install
-
Create:
<YourVault>/.obsidian/plugins/book-format-sync/ -
Put these files in that folder:
manifest.json main.js styles.css -
Restart Obsidian or reload the plugin.
-
Enable 书库三格式同步 in Settings → Community plugins.
From a GitHub Release
Download the release assets and place manifest.json, main.js, and styles.css in .obsidian/plugins/book-format-sync/.
Usage
Commands
书库三格式同步:扫描书库(只读)书库三格式同步:生成章节标题建议(本地)书库三格式同步:规范化并生成当前书籍三格式书库三格式同步:只规范化当前文件书库三格式同步:从当前文件转换生成另外两种格式书库三格式同步:规范化书库全部书籍
File explorer right-click menu
Right-click a .md, .txt, or .epub file:
书库三格式同步
├─ 生成章节标题建议
├─ 规范化并生成三格式
├─ 只规范化
└─ 从当前文件生成另外两种
Actions apply to the right-clicked file, not the currently open tab. 只规范化 writes only the current format, does not create the other two, and force-backs up the current file when it is also the target.
Book layout
Default layout:
10 书籍/
└─ <Book Name>/
├─ <Book Name>.md
├─ <Book Name>.txt
└─ <Book Name>.epub
The book root, folder-per-book behavior, chapter format, and title length can be changed in plugin settings.
Per-book output paths
Settings include an optional Per-book paths group. Entries are added and removed manually; the plugin does not auto-scan the vault for mappings.
Each entry has:
bookName book identifier (also the output filename stem)
mdDir Markdown output directory
txtDir TXT output directory
epubDir EPUB output directory
Paths are vault-relative. Absolute paths and any .. segment are rejected with an explicit error. Empty output directory fields fall back to 10 书籍/<bookName>/. Output directories are created automatically. Right-click an output directory input to choose an existing vault folder.
The original file is not configured separately: conversion reads the current file, and batch/right-click normalization reads the same-named md/txt/epub from the default book directory. Removing an entry does not delete generated files. Existing settings without bookProfiles migrate to an empty list and keep the default behavior; a legacy originalPath field is dropped.
Title suggestions
The local title generator detects:
- empty titles;
- pure numeric titles such as
001; - titles equal to the chapter number;
- placeholder titles such as
未命名; - repeated titles;
- short titles (≤2 CJK characters) as soft suggestions.
Titles are generated from the first 1–3 paragraphs, max 800 characters:
- first narrative sentence;
- first paragraph snippet;
- 2–4 character high-frequency fallback.
The default title length is 10 characters (configurable 4–24). The numeric chapter prefix is retained:
第1章 001 → 第1章 实至名归
Suggestions are shown in a preview table. Nothing is written until 应用选中标题 is clicked. Duplicate suggestions fall back to another candidate; if none is usable, the numeric title is kept and marked needs_review.
Development
node tools/build.js
node tests/run-tests.js
node tests/plugin-harness.js
node tests/title-apply-harness.js
node tests/menu-harness.js
tools/build.js bundles src/main.js and lib/*.js into the root main.js loaded by Obsidian. There are no npm dependencies.
Project structure
book-format-sync/
├─ manifest.json
├─ main.js # bundled Obsidian entry
├─ styles.css
├─ src/main.js # commands, modals, settings
├─ lib/
│ ├─ chapters.js # chapter parsing and normalization
│ ├─ text.js # encoding, paragraph splitting, indent rules
│ ├─ zip.js # ZIP reader/writer (Node zlib)
│ ├─ epub.js # EPUB parser/generator
│ ├─ book.js # Book model, three-format rendering/validation
│ └─ titles.js # title issue detection and local suggestions
├─ tests/
├─ tools/
└─ .github/workflows/
Versioning
manifest.jsonversion is the source of truth.- Git tags use
v+ version:v1.3.0. - GitHub Releases use the same tag.
versions.jsonmaps plugin version to minimum Obsidian version.- See CHANGELOG.md.
Limitations
- Only
md,txt, andepubare supported. - EPUB parsing is lightweight and does not support ZIP64, encrypted EPUB, or split ZIP archives.
- Images in EPUB become
[图片]placeholders in text formats. - Chapter number gaps/duplicates are reported, not renumbered.
- Title suggestions are local and extractive; they may be plain or truncated, and should be reviewed before applying.
- Markdown 2em indentation depends on the plugin CSS and Obsidian internal
cm-line/HyperMD-*classes; source bytes stay clean, but source mode may show visual indentation and upgrades may require re-checking. - Per-book path entries are manual; batch scanning still uses the default book root, so a book entirely outside that root must be converted from its configured original file/right-click entry.
License
MIT. See LICENSE.
For plugin developers
Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.