如何解决通过markdown-pdf将最新git commit的哈希添加到pdf的页脚中
我正在通过gulp使用markdown-pdf将.md文件转换为.pdf。我想获取最新的git commit的信息并将其添加到页脚中。我可以在gulpfile.js(found the answer)中获得哈希值:
revision = require('child_process')
.execSync('git rev-parse HEAD')
.toString().trim();
但是我怎么能把它放入页脚?
下面是我在gulpfile.js中使用的markdown-pdf代码:
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"))
}
还有我的runnings.js文件:
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;">© 2020</p><p style="float:right;width:33.33%;margin:0;font-size:10px;text-align:right;">Page ' + pageNum + ' of ' + numPages + '</p></footer>'
}
}
}
还有我的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)
}
解决方法
执行此操作的最佳方法是使用preProcessMd
方法将SHA注入页脚。您似乎确实在preProcessMd: preProcessMd,
中定义了预处理器。那实际上是在做什么吗?如果是这样,定义是什么?
编辑,从OP更新之后:快速而肮脏的解决方案是在希望SHA移到的地方使用空的span
。然后寻找该范围,并用preProcessMd
中的SHA替换。例如<span class="git-hash"></span>
。然后将<span class="git-hash">
替换为<span>YOUR-SHA
。
您可能需要像这样更新gulp任务:
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"))
}
您的function preProcessMd() {
至function preProcessMd(revision) {
注意:我不确定preProcessMd.bind(this,...
是否会出现问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。