simonhaenisch / md-to-pdf

Hackable CLI tool for converting Markdown files to PDF using Node.js and headless Chrome.
https://www.npmjs.com/md-to-pdf
MIT License
1.2k stars 110 forks source link

feature: Dynamic Document Title #126

Open ZachEngstrom opened 2 years ago

ZachEngstrom commented 2 years ago

Problem:

module.exports = {
    document_title: `${__filename}`,
    stylesheet: "./assets/custom.css",
    pdf_options: {
        displayHeaderFooter: true,
        headerTemplate: `
        <div><span class="title"></span> | Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>
      `,
        footerTemplate: `
        <div>&copy;${new Date().getFullYear()} Spongebob</div>
        `,
    },
};

Outputs: /Users/user/path/config.js | Page 1 of 5

Setting document_title to a string value is a static value. Not setting document_title outputs an empty string.

Desired Outputs:

Request: Add the ability for the code to determine the file name of the document it is currently converting and being able to use that as the "title" value.

simonhaenisch commented 2 years ago

https://nodejs.org/api/modules.html#__filename:

__filename

The file name of the current module. This is the current module file's absolute path with symlinks resolved.

So yeah of course that would reference your config file.

Anyway, wasn't really aware that the document title is what's used for the title in the header template. So yeah I see the use case. I'm assuming manually adding a front-matter with

---
document_title: File
---

to each markdown file will be too cumbersome for you? (what's in the front-matter takes precedence over the config file)

Otherwise, can't really think of a fast solution for this, other than some better defaults for the title, e. g. file name or first h1 content.