0

I am using markdown-pdf via gulp to convert .md files to .pdf. I want to get the sha of the latest git commit and add it to the footer. I can get the hash like so in my gulpfile.js (found the answer):

revision = require('child_process')
  .execSync('git rev-parse HEAD')
  .toString().trim();

But how can I get that into my footer?

Below is my code for markdown-pdf that I am using in my gulpfile.js:

function docsToPdf() {
    return src(["Views/Documentation/Files/*.md", "!Views/Documentation/Files/_README.md"])
        .pipe(markdownPdf({
            preProcessMd: preProcessMd,
            remarkable: {
                html: true
            },
            paperBorder: "1cm",
            runningsPath: "Content/Pdf/assets/js/runnings.js",
            cssPath: "Content/Pdf/assets/css/pdf.min.css"
        }))
        .pipe(dest("Content/Pdf"))
}

And my runnings.js file:

module.exports = {
    header: {
        height: '2cm',
        contents: function (pageNum) {
            if (pageNum == 1) {
                return '<header class="pdf-header" style="padding-bottom: 20px;"><h2 style="text-align:center;margin:0;">Documentation</h2></header>'
            }
            return ''
        }
    },

    footer: {
        height: '1.5cm',
        contents: function (pageNum, numPages) {
            return '<footer class="pdf-footer" style="padding-top:20px;"><p style="float:left;width:33.33%;margin:0;font-size:10px;">' + new Date().toDateString() + '</p><p style="float:left;width:33.33%;margin:0;font-size:10px;text-align:center;">&copy; 2020</p><p style="float:right;width:33.33%;margin:0;font-size:10px;text-align:right;">Page ' + pageNum + ' of ' + numPages + '</p></footer>'
        }
    }
}

And my preProccessMd:

function preProcessMd() {
    var splitter = split()
    var docsUrl = "https://example.org/docs/";
    var urlOne = /\[\[((?:(?!\[\[).)*?)\|(.*?)]]/g;
    var urlImg = /(\()(images)/g;
    var replacer = through(function (data, path) {
        this.queue(
            data
                .replace(urlOne, (_, x, y) => `[${x}](${docsUrl}${y.replace(/(?!^)[A-Z]/g, '-$&').toLowerCase()})`)
                .replace(urlImg, "$1$2".replace("$2", "content/images/docs"))
            + "\n"
        )
    })

    splitter.pipe(replacer)
    return duplexer(splitter, replacer)
}
st3phhays
  • 95
  • 7

1 Answers1

0

The best way to do this would be to use the preProcessMd method to inject the SHA into the footer. You do seem to have the preprocessor defined in preProcessMd: preProcessMd,. Is that actually doing anything? If so, what is the definition?

EDIT, after update from OP: Quick and dirty solution would be to use an empty span where you want the SHA to go. Then look for that span and replace with the SHA in your preProcessMd. For e.g., <span class="git-hash"></span>. Then replace <span class="git-hash"> with <span>YOUR-SHA.

You might need to update your gulp task like so:

function docsToPdf() {
    revision = require('child_process')
      .execSync('git rev-parse HEAD')
      .toString().trim();

    return src(["Views/Documentation/Files/*.md", "!Views/Documentation/Files/_README.md"])
        .pipe(markdownPdf({
            preProcessMd: preProcessMd.bind(this, revision),
            remarkable: {
                html: true
            },
            paperBorder: "1cm",
            runningsPath: "Content/Pdf/assets/js/runnings.js",
            cssPath: "Content/Pdf/assets/css/pdf.min.css"
        }))
        .pipe(dest("Content/Pdf"))
}

And your function preProcessMd() { to function preProcessMd(revision) {

NOTE: I'm unsure if the preProcessMd.bind(this, ... is going to be problematic or not.

  • Thanks for the reply. I edited my post to include that function. It mostly is just replaces some urls right now. – st3phhays Aug 27 '20 at 14:10
  • Thanks so much for the suggestion, unfortunately it did not work. I'm thinking that maybe the `preProcessMd` is ran before the footer is injected by runnings.js, so it has nothing to find/replace. – st3phhays Sep 02 '20 at 14:44
  • @Designxsteph hmmm that's weird. So it goes MD -> HTML -> PDF. Only thing I can think of is to do the replacement in `preProcessHtml`. Could you give that shot? – Manish Honnatti Sep 03 '20 at 17:40