github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/site/static/js/plugins/book_page_title.js (about)

     1  // Add title to book Markdown pages based on directory structure.
     2  function processBookPageTitle(content) {
     3    const pathname = window.location.pathname.toLowerCase();
     4  
     5    const bookPathMatch = pathname.match(/^\/book\/(\d+)-(.+)\/(\d+)?-?(.+)?/);
     6  
     7    if (content && !content.startsWith("<!DOCTYPE html>") && bookPathMatch) {
     8      const pageNumber = parseInt(bookPathMatch[3]);
     9  
    10      // Use chapter name if on intro page and page name otherwise.
    11      const chapterNum = `# ${parseInt(bookPathMatch[1])}${
    12        pageNumber > 0 ? `.${pageNumber}` : ""
    13      }`;
    14      const pageTitle = pageNumber > 0 ? bookPathMatch[4] : bookPathMatch[2];
    15  
    16      content =
    17        `${chapterNum} ${pageTitle.replace(/-/g, " ").toTitleCase()}\n` +
    18        content;
    19    }
    20  
    21    return content;
    22  }
    23  
    24  // Load plugins into Docsify.
    25  window.$docsify = window.$docsify || {};
    26  window.$docsify.plugins = [
    27    (hook, _vm) => hook.beforeEach(processBookPageTitle),
    28  ].concat(window.$docsify.plugins || []);
    29  
    30  // Export functions for testing.
    31  if (typeof module !== "undefined") {
    32    module.exports = { processBookPageTitle };
    33  }