zjp-CN / mdbook-theme

A preprocessor and a backend to config themes for mdbook, especially creating a pagetoc on the right and setting full color themes from the offical ace editor.
MIT License
38 stars 3 forks source link

Page table of contents block will be repeatedly inserted into index.hbs #2

Open garylavayou opened 1 year ago

garylavayou commented 1 year ago

In the code (src/theme/mod.rs), it lacks a logic to determine if the current index.hbs file already has the sidetoc block. Hence, during each processing, the sidetoc block will be added again.

/// update content in `index.hbs`, if and only if `pagetoc = true` for now
fn process_index(&mut self) {
    let insert = r#" <!-- Page table of contents -->
                    <div class="sidetoc"><nav class="pagetoc"></nav></div>
                    "#;
    self.content.insert(insert, "<main>", "{{{ content }}}").unwrap();
}

The results in index.hbs,

<div id="content" class="content">
    <main>
      <!-- Page table of contents -->
      <div class="sidetoc"><nav class="pagetoc"></nav></div>
      <!-- Page table of contents -->
      <div class="sidetoc"><nav class="pagetoc"></nav></div>
      {{{ content }}}
    </main>
    <!-- other contents -->
</div>

In addition, if I run mdbook serve, the block will be inserted again and again, due to the auto-reload mechanism that detects the changes of index.hbs

zjp-CN commented 1 year ago

Yes, you can refer to https://github.com/zjp-CN/mdbook-theme#avoid-repeating-call-on-this-tool-when-mdbook-watch

One solution is to set

[preprocessor.theme]
turn-off = true

once you don't modify the theme any more.

the auto-reload mechanism that detects the changes

mdbook always auto triggers mdbook-theme as long as you define a table [preprocessor.theme] in your book.toml, but this plugin hasn't considered comparing the differences[^differences], instead it always replaces...

I think to keep things simple, as stated in the link I give, you can also avoid repeating by commenting out the whole [preprocessor.theme] table, meaning mdbook-theme won't run, because the generated files under theme folder won't change.

[^differences]: setting options result in various comparison strategies, like some options need to find the value in a file and compare it with the value in book.toml, but others need to detect whether there is already the replacement.