<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tri Thu</title>
    <description>The latest articles on DEV Community by Tri Thu (@novelverba).</description>
    <link>https://dev.to/novelverba</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4265782%2Fc3f59230-fdfa-467e-944e-114a74b9cb0b.png</url>
      <title>DEV Community: Tri Thu</title>
      <link>https://dev.to/novelverba</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/novelverba"/>
    <language>en</language>
    <item>
      <title>Drawing e-reader covers with HTML Canvas: 16 grays, font subsets and titles without spaces</title>
      <dc:creator>Tri Thu</dc:creator>
      <pubDate>Sat, 28 Nov 2026 05:11:21 +0000</pubDate>
      <link>https://dev.to/novelverba/drawing-e-reader-covers-with-html-canvas-16-grays-font-subsets-and-titles-without-spaces-17cf</link>
      <guid>https://dev.to/novelverba/drawing-e-reader-covers-with-html-canvas-16-grays-font-subsets-and-titles-without-spaces-17cf</guid>
      <description>&lt;p&gt;A Carta e-ink screen shows 16 levels of gray, so a smooth glow or gradient on a book cover collapses into a few flat steps with hard edges. What works is keeping ramps out of the lettering on e-ink covers and previewing the cover at 16 grays before it reaches the device. More canvas traps showed up on the way: font subsets that are not loaded yet when you measure text, titles in scripts without spaces, and mixed reading directions.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://novelverba.com" rel="noopener noreferrer"&gt;NovelVerba&lt;/a&gt;, a tool that translates long Chinese, Japanese and Korean web novels from EPUB or TXT files. Books that start as a TXT file usually reach the reader as a blank tile with a file name, so I added a free cover maker: pick one of 17 genre templates or your own image, type the title, then download a JPG or attach it to an EPUB. It all runs on a canvas in the browser. The title is in the reader's language, which for my users means Vietnamese with stacked tone marks, Thai with no spaces, and Arabic from right to left.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sixteen grays turn a soft glow into rings
&lt;/h2&gt;

&lt;p&gt;Each step on a 16-level screen is 17 values apart (255 / 15). A glow that fades across 40 values of brightness has two or three steps to land on, so instead of a halo you get a couple of flat rings.&lt;/p&gt;

&lt;p&gt;The preview simulates that with a luma conversion that snaps to 16 levels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;EINK_GRAY_LEVELS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;grayscale_canvas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CanvasRenderingContext2D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;levels&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getImageData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;levels&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;levels&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.299&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.587&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.114&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putImageData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snapping is for the preview only. The exported JPG keeps continuous gray, because the device converts the image for its own panel anyway, and quantizing it twice makes no sense.&lt;/p&gt;

&lt;p&gt;The black and white mode changes the drawing itself, not just the colors. Gradient fills in the lettering become one flat ink color, glows are dropped, and any fading band behind the title goes too. The template backgrounds are made with a calm area where the title sits, so nothing has to go behind the lettering. A user's own image has no such area, so in color mode it gets a soft band at the top, and a quick brightness check of the title area picks light or dark lettering as a starting point, with a manual switch when mid-tone art fools the guess.&lt;/p&gt;

&lt;h2&gt;
  
  
  Register every font subset, load only the one a title needs
&lt;/h2&gt;

&lt;p&gt;The title face is Lora, split into latin, latin-ext and vietnamese subsets. My build merges all CSS into one stylesheet, so a CSS &lt;code&gt;@font-face&lt;/code&gt; import would ship these faces on every page. Instead the tool registers &lt;code&gt;FontFace&lt;/code&gt; objects with a &lt;code&gt;unicodeRange&lt;/code&gt; when it loads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="no"&gt;LORA_FACES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fonts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FontFace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Lora"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sb"&gt;`url(${spec.url})`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"700"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unicodeRange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unicode_range&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Registering downloads nothing. The trap is measuring. Canvas layout depends on &lt;code&gt;measureText&lt;/code&gt;, and &lt;code&gt;document.fonts.load(font)&lt;/code&gt; without a string only fetches the latin subset. A Vietnamese title can then be measured in a fallback face, and the line breaks stop matching what gets drawn once the real subset arrives. So every string that will be drawn goes in: &lt;code&gt;document.fonts.load(font, title)&lt;/code&gt;, and the same for author, series and volume. The preview also redraws if fonts were still loading when the first pass ran.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping titles that have no spaces
&lt;/h2&gt;

&lt;p&gt;Splitting on spaces works for English and Vietnamese, not for Thai or Chinese. &lt;code&gt;Intl.Segmenter&lt;/code&gt; gives word boundaries for both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;segmenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Intl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Segmenter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;granularity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"word"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;segmenter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"NFC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// part.segment, part.isWordLike&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Punctuation needs one more rule: closing marks stick to the word before them and opening quotes to the word after, so no line ends on an opening mark. The title then shrinks until it fits its box, and a word is never split. If one word, often a German compound, still does not fit at the smallest size, the tool says so and keeps the download disabled instead of cutting it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arabic, Thai, and a series line in two directions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Letter spacing is forced to zero for Arabic and Thai, even when a template asks for tracking. Tracking breaks Arabic joins and pulls Thai marks away from their letters.&lt;/li&gt;
&lt;li&gt;Arabic is drawn with &lt;code&gt;ctx.direction = "rtl"&lt;/code&gt;, and every font stack ends with &lt;code&gt;"Noto Sans Arabic"&lt;/code&gt; and no generic family, because a generic serif could win the Arabic glyphs over the loaded face.&lt;/li&gt;
&lt;li&gt;Series and volume share one line, &lt;code&gt;Series · 3&lt;/code&gt;, when it fits. Its base direction follows the series, like &lt;code&gt;dir="auto"&lt;/code&gt; on the inputs, so a Latin series with an Arabic volume number does not flip.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thai and Chinese titles use system fonts, since a full CJK web font runs to many megabytes. The cost is that the same title looks different on Windows, macOS and Android.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is still open
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The 16-gray preview is an estimate, and I have not checked these covers on every device.&lt;/li&gt;
&lt;li&gt;System fonts make Thai and CJK covers inconsistent across machines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have shipped canvas text for scripts you cannot bundle fonts for, how did you handle it? A subset per title, or the system font?&lt;/p&gt;

&lt;p&gt;The cover maker is free at &lt;a href="https://novelverba.com/novel-cover-maker" rel="noopener noreferrer"&gt;novelverba.com/novel-cover-maker&lt;/a&gt;. It runs in the browser, and your image and text never leave your browser.&lt;/p&gt;

</description>
      <category>canvas</category>
      <category>javascript</category>
      <category>i18n</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Translating an EPUB in place: the paragraph that came out as 26 lines</title>
      <dc:creator>Tri Thu</dc:creator>
      <pubDate>Thu, 26 Nov 2026 02:35:36 +0000</pubDate>
      <link>https://dev.to/novelverba/translating-an-epub-in-place-the-paragraph-that-came-out-as-26-lines-5cjp</link>
      <guid>https://dev.to/novelverba/translating-an-epub-in-place-the-paragraph-that-came-out-as-26-lines-5cjp</guid>
      <description>&lt;p&gt;If you translate an EPUB by pulling out the text and building a new book, you lose the things readers notice: images, the publisher's CSS, the table of contents, footnote links. The approach that keeps them is to translate text nodes in place, with a map from every translated line back to the exact DOM slot it came from. That map breaks on one kind of markup more than any other: a paragraph where every word sits in its own &lt;code&gt;&amp;lt;i&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;. I merge those into one line only when nine conditions hold, and I write the translation back into the first word's text node.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://novelverba.com" rel="noopener noreferrer"&gt;NovelVerba&lt;/a&gt;, a tool that translates long Chinese, Japanese and Korean novels from EPUB or TXT files. This post is about the EPUB part: what the extractor sees, the rules for merging, and how I kept it linear on chapters with hundreds of thousands of text nodes.&lt;/p&gt;

&lt;h2&gt;
  
  
  One line per text node, and a receipt for each
&lt;/h2&gt;

&lt;p&gt;The extractor walks the spine documents. Every block element (&lt;code&gt;p&lt;/code&gt;, &lt;code&gt;h1&lt;/code&gt; to &lt;code&gt;h6&lt;/code&gt;, &lt;code&gt;li&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, &lt;code&gt;td&lt;/code&gt; and a few more) opens a block context. Every non-empty text node inside it becomes a "slot", and every slot becomes one source line. For each line I store a part reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// simplified: the real type also records which kind of text node the slot is&lt;/span&gt;
&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EpubPartRef&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// "/html[1]/body[1]/p[3]", tag name plus sibling index&lt;/span&gt;
  &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// which slot inside that block&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plus a SHA-1 digest of all slot texts. The translation pipeline keeps a strict contract: one source line in, one translated line out. At export, the writer opens the original XHTML again, finds each block by path, finds the slot by index, and replaces only the text. Every &lt;code&gt;&amp;lt;i&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;a href&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; and &lt;code&gt;class&lt;/code&gt; around it stays where the publisher put it.&lt;/p&gt;

&lt;p&gt;That works well for normal prose. Then a book converted through Calibre showed up.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a shredded paragraph looks like
&lt;/h2&gt;

&lt;p&gt;Here is the shape, rebuilt with the opening line of &lt;em&gt;Pride and Prejudice&lt;/em&gt; (public domain). The real sources are Chinese, Japanese or Korean, but an English sentence makes the word boundaries easy to see while reading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"block8"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"p1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;It&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;is&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;a&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;truth&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;universally&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;acknowledged&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;, &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;that&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"calibre3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;a&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every word is its own italic element. Punctuation is a bare text node of the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;. The spaces are whitespace-only text nodes between tags. In a reader app it looks like a normal italic paragraph. Word processors and conversion chains produce this when formatting gets applied word by word, and Calibre's &lt;code&gt;index_split_NNN.html&lt;/code&gt; files keep it intact.&lt;/p&gt;

&lt;p&gt;With slot-per-line, this one sentence becomes 26 slots, so 26 source lines: &lt;code&gt;It&lt;/code&gt;, &lt;code&gt;is&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;truth&lt;/code&gt;, and so on. The translation follows the contract and comes back as 26 lines of one word each. Nothing failed, no error was raised, and the chapter read as a vertical column of single words.&lt;/p&gt;

&lt;h2&gt;
  
  
  Merge only when the paragraph is clearly shredded
&lt;/h2&gt;

&lt;p&gt;The obvious fix is "if a block has many small inline tags, join them". That breaks real formatting. A sentence with one emphasized word, a poem with one &lt;code&gt;&amp;lt;i&amp;gt;&lt;/code&gt; per verse line, a stats line like &lt;code&gt;HP 100 MP 50&lt;/code&gt; in alternating spans: all of these carry meaning in their markup.&lt;/p&gt;

&lt;p&gt;So the merge is a whitelist with nine conditions. A leaf block is merged into one line only if all of them hold:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Condition&lt;/th&gt;
&lt;th&gt;What it keeps out&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;a&lt;/td&gt;
&lt;td&gt;Every element inside is a pure formatting tag: &lt;code&gt;b strong i em u s strike del ins span font small big mark nobr&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;&amp;lt;br&amp;gt;&lt;/code&gt;, links, footnote &lt;code&gt;&amp;lt;sup&amp;gt;&lt;/code&gt;, images, ruby, math, svg, code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;b&lt;/td&gt;
&lt;td&gt;At least 3 slots, at least one containing a letter&lt;/td&gt;
&lt;td&gt;Two-slot drop caps, lines of digits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;c&lt;/td&gt;
&lt;td&gt;Every slot with a letter or digit is wrapped, and every wrapped slot has the same wrapper signature (tag plus attributes, ignoring &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;xml:id&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Sparse emphasis, mixed bold and italic, a hidden prefix span&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;d1&lt;/td&gt;
&lt;td&gt;Two adjacent word slots always have collapsible whitespace between them&lt;/td&gt;
&lt;td&gt;Words glued by CSS letter-spacing tricks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;d2&lt;/td&gt;
&lt;td&gt;No gap contains NBSP, U+2007, U+202F, U+3000, form feed or other non-collapsible whitespace&lt;/td&gt;
&lt;td&gt;Layout that depends on fixed spacing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;g&lt;/td&gt;
&lt;td&gt;At least 80% of word slots are a single token&lt;/td&gt;
&lt;td&gt;Poems and Google Docs style runs of several words&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;h&lt;/td&gt;
&lt;td&gt;No gap contains a line break&lt;/td&gt;
&lt;td&gt;One tag per source line under &lt;code&gt;white-space: pre-wrap&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;i&lt;/td&gt;
&lt;td&gt;No element declares &lt;code&gt;display&lt;/code&gt; or &lt;code&gt;white-space&lt;/code&gt; in its &lt;code&gt;style&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Flex or grid layouts where each span is its own cell&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;f&lt;/td&gt;
&lt;td&gt;The merged line is at most 2,000 characters&lt;/td&gt;
&lt;td&gt;Lines the pipeline could not split later&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Running the extractor on a few synthetic cases shows the split:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shredded sentence  -&amp;gt; ["It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife."]
sparse emphasis    -&amp;gt; ["It is a", "truth", "universally", "acknowledged", ", that a single man."]
poem lines in &amp;lt;i&amp;gt;  -&amp;gt; ["Tyger Tyger, burning bright,", "In the forests of the night;", "What immortal hand or eye,"]
&amp;lt;br&amp;gt; in the middle -&amp;gt; ["It", "is", "a", "truth", "universally", "acknowledged"]
&amp;amp;#160; between     -&amp;gt; ["It", "is", "a", "truth"]
glued &amp;lt;i&amp;gt;It&amp;lt;/i&amp;gt;&amp;lt;i&amp;gt;is&amp;lt;/i&amp;gt; -&amp;gt; ["It", "is", "a", "truth"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sparse emphasis case is the honest cost of this design. The sentence stays in five pieces, because merging it would mean deciding where &lt;code&gt;truth&lt;/code&gt; and &lt;code&gt;acknowledged&lt;/code&gt; land in a translated sentence with different word order. I have no safe answer for that yet, so the extractor keeps the old behavior and the formatting survives.&lt;/p&gt;

&lt;p&gt;The rule for wrappers (c) compares a signature, not a tag name. Two &lt;code&gt;&amp;lt;i class="calibre3"&amp;gt;&lt;/code&gt; match. &lt;code&gt;&amp;lt;i class="calibre3"&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;i class="lead"&amp;gt;&lt;/code&gt; do not. &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;xml:id&lt;/code&gt; are excluded from the signature on purpose: converters often give every word its own &lt;code&gt;id&lt;/code&gt;, and that should not block the merge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing one translated line back into 26 slots
&lt;/h2&gt;

&lt;p&gt;At export, the merged block gets exactly one translated line. The writer puts the whole line into the first slot that contains a letter, then removes every other text node in the block. It also removes every element that does not contain the target and does not carry an &lt;code&gt;id&lt;/code&gt; or &lt;code&gt;xml:id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;id&lt;/code&gt; part matters. Same example, with an anchor on one word and a link pointing at it from elsewhere in the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- source --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"p1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;It&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;is&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"truth"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;truth&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;,
&lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;universally&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;acknowledged&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&lt;/span&gt;.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"#truth"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;back&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- after write-back --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"p1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;C'est une vérité universellement reconnue.&lt;span class="nt"&gt;&amp;lt;/i&amp;gt;&amp;lt;i&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"c3"&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"truth"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/i&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"#truth"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;back&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The translated line keeps the italic wrapper and its class. The anchor survives as an empty element, so &lt;code&gt;#truth&lt;/code&gt; still resolves. The trade-off: the anchor now points at the end of the paragraph instead of the original word. For footnote back-links and TOC targets that sit on a paragraph or heading, that is fine. For an anchor on a single word in the middle of a sentence, there is no exact position left to point at once the word order changes.&lt;/p&gt;

&lt;p&gt;After every document is written, a validator compares source and output before anything goes into the zip:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no new duplicate &lt;code&gt;id&lt;/code&gt; (an &lt;code&gt;id&lt;/code&gt; that appears more often than in the source)&lt;/li&gt;
&lt;li&gt;every &lt;code&gt;href&lt;/code&gt; and &lt;code&gt;src&lt;/code&gt; from the source still exists in the output, except same-file links whose target was removed on purpose with stripped ruby (see below)&lt;/li&gt;
&lt;li&gt;every same-file fragment &lt;code&gt;href="#x"&lt;/code&gt; whose target existed in the source still has a target&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If any of these fail, the export stops with an error for that chapter. It does not quietly ship a chapter that is still in the source language, or one with a dead link the writer created.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same decision at extract time and at export time
&lt;/h2&gt;

&lt;p&gt;A project can sit for weeks between upload and export, and the code changes in between. If the merge rules change, a block that was one line at extract time could be 26 slots at export time, and the line counts would no longer add up.&lt;/p&gt;

&lt;p&gt;Two guards handle that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The parser and the writer import the same &lt;code&gt;is_fragmented_block&lt;/code&gt; function. At export, every block listed as merged is checked again on the current DOM before anything is modified. If it no longer qualifies, the export refuses with "recreate the project", instead of guessing.&lt;/li&gt;
&lt;li&gt;The digest is recomputed from the current file, including the merged text of each merged block, in the same order as at extract time. A mismatch means the source changed, and the export refuses again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The line count check runs after the digest check. A merged block with 26 parts counts as one expected line, so the formula is &lt;code&gt;parts - (parts_per_merged_block - 1)&lt;/code&gt; summed over merged blocks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping it linear
&lt;/h2&gt;

&lt;p&gt;Chapters can be large. A single XHTML file can hold thousands of paragraphs, and shredded ones multiply the text node count by twenty or more. Several naive steps were quadratic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wrapper signatures.&lt;/strong&gt; The first version built a string of all ancestor tags and attributes for every slot. With a long attribute on an outer wrapper, that string gets copied once per slot. Signatures are now interned: each element gets a numeric id keyed by &lt;code&gt;parentId/tag[attrs]&lt;/code&gt;, and a slot only stores the number. There is a test with 20,000 slots under a 256 KiB attribute.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge whitespace.&lt;/strong&gt; Reading trailing whitespace with a regex like &lt;code&gt;/[\s]*$/&lt;/code&gt; is quadratic on a text node with a long run of spaces in the middle, because the engine retries from every position. A plain loop from the end fixes it. The test uses a text node with 200,000 spaces.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finding blocks by path.&lt;/strong&gt; Resolving &lt;code&gt;/html[1]/body[1]/p[2931]&lt;/code&gt; by scanning siblings for every part is O(n²) on long documents. The writer builds a finder that groups children by tag name once per parent and caches results per path.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inserting bilingual copies.&lt;/strong&gt; In bilingual export, each translated block gets a copy of the original inserted before it. Splicing one clone at a time into a parent with thousands of children is quadratic, so clones are grouped by parent and the children array is rebuilt once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On my dev machine (Node 22, Windows), parse plus extraction on synthetic chapters gave:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 2,000 shredded paragraphs,  44,000 text nodes -&amp;gt;  2,000 lines, ~200 ms
20,000 shredded paragraphs, 440,000 text nodes -&amp;gt; 20,000 lines, ~1.4 s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ten times the input, about seven times the time. The test suite pins the shape: 2,000 valid paragraphs of 20 words each (40,000 slots) must merge into exactly 2,000 lines, and a single &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; with 50,000 per-word spans must not merge at all, because it would exceed the 2,000 character line limit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smaller things that broke write-back along the way
&lt;/h2&gt;

&lt;p&gt;None of these deserve a whole post, but each one produced a broken file at some point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities.&lt;/strong&gt; Slot text is decoded before it goes to translation. If the raw &lt;code&gt;A &amp;amp;amp; B&lt;/code&gt; goes through, the translation echoes the entity and the serializer escapes it again, and the reader shows &lt;code&gt;&amp;amp;amp;amp;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leading and trailing whitespace.&lt;/strong&gt; Translated lines come back trimmed. The writer re-attaches the original whitespace of each slot, otherwise &lt;code&gt;&amp;lt;b&amp;gt;Published:&amp;lt;/b&amp;gt; 1903&lt;/code&gt; turns into &lt;code&gt;Published:1903&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ruby.&lt;/strong&gt; For targets other than Japanese, &lt;code&gt;&amp;lt;rt&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;rp&amp;gt;&lt;/code&gt; are stripped, since furigana next to a translated word is noise. Any same-file &lt;code&gt;href&lt;/code&gt; pointing at an &lt;code&gt;id&lt;/code&gt; inside the stripped ruby loses its &lt;code&gt;href&lt;/code&gt;, so it does not become a dead link.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Right-to-left targets.&lt;/strong&gt; For Arabic output the root &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; gets &lt;code&gt;dir="rtl"&lt;/code&gt;, and a block that pinned &lt;code&gt;dir="ltr"&lt;/code&gt; itself is overridden. A drop cap wrapped in its own span is unwrapped, because an enlarged Arabic letter in a separate span loses its joining with the next letter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bilingual copies and ids.&lt;/strong&gt; The copy of the original has every &lt;code&gt;id&lt;/code&gt; removed across its whole subtree. The translated block keeps the ids, so the table of contents jumps to the translation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializing XHTML.&lt;/strong&gt; The serializer runs with self-closing tags on (&lt;code&gt;&amp;lt;br/&amp;gt;&lt;/code&gt;) but without XML mode, because XML mode turns every non-ASCII character into a numeric entity and a Chinese chapter grows to nearly three times its size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The OPF file.&lt;/strong&gt; &lt;code&gt;dc:language&lt;/code&gt; is changed by editing byte offsets inside the original string, located with a namespace-aware parse. Everything outside the edit stays byte for byte, including the publisher's formatting and the XML declaration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The zip.&lt;/strong&gt; &lt;code&gt;mimetype&lt;/code&gt; is stored uncompressed as the first entry, as the EPUB spec requires, and already compressed media (JPEG, PNG, WOFF fonts, audio) are stored instead of deflated again.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is still open
&lt;/h2&gt;

&lt;p&gt;Two things I would like to solve better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Paragraphs with a few emphasized words stay split into several lines. The formatting is preserved, but the translation sees the sentence in pieces.&lt;/li&gt;
&lt;li&gt;Word-level anchors inside a merged paragraph move to the end of the paragraph.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have worked on EPUB round trips, I am curious how you handle the first one. Do you translate the whole paragraph and then try to re-apply emphasis by alignment, or do you accept losing the inline formatting?&lt;/p&gt;

&lt;p&gt;If you want to check a book before doing anything like this, I made a free &lt;a href="https://novelverba.com/epub-validator" rel="noopener noreferrer"&gt;EPUB checker&lt;/a&gt; that looks for common structure problems (package, metadata, table of contents, internal links). It runs in the browser and the file never leaves your device. It is not a replacement for EPUBCheck.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>software</category>
      <category>typescript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
