将Markdown文件转换为PDF,同时剥离Jekyll Front

编程入门 行业动态 更新时间:2024-10-28 11:22:08
将Markdown文件转换为PDF,同时剥离Jekyll Front-Matter(Convert Markdown files to PDF while stripping Jekyll Front-Matter)

我有一个javascript文件应该循环遍历给定目录,索引文件,从中删除前端内容,然后将它们转换为PDF文件。

我的大部分过程都在工作,但出于某种原因,我的.replace函数似乎没有做任何事情。

这是代码:

var fs = require( 'fs' ); var path = require( 'path' ); var process = require( 'process' ); var markdownpdf = require( 'markdown-pdf' ) var md_dir = "_pages"; var pdf_dir = "assets/pdf" // Loop through all the files in the directory fs.readdir( md_dir, function( err, files ) { if( err ) { console.error( "Could not list the directory.", err ); process.exit( 1 ); } files.forEach( function( file, index ) { // Make one pass and make the file complete var md_path = path.join( md_dir, file ); var pdf_path = path.join( pdf_dir, file + '.pdf' ); fs.stat( md_path, function( error, stat ) { if( error ) { console.error( "Error stating file.", error ); return; } // Start PDF Process on the files if( stat.isFile() ) { console.log( "'%s' is a file.", md_path ); console.log( "Stripping Jekyll Front-Matter..."); var fileContents = fs.readFileSync(md_path, 'utf8'); var pattern = /^---\n.*\n---\n/; // Strip everything between the first two sets of '---' var stripped_md = fileContents.replace( pattern, '' ); console.log( "Front-Matter stripped." ); // Pass that to markdown-pdf markdownpdf().from.string(stripped_md).to(pdf_path, function () { console.log("Created", pdf_path) } ); } // If it's not a file else if( stat.isDirectory() ) console.log( "'%s' is a directory.", md_path ); } ); } ); } );

I have a javascript file that is supposed to loop through a given directory, index the files, strip the front-matter out of them, and then convert them into PDF files.

I have most of the process working, but for some reason my .replace function doesn't seem to be doing anything.

Here's the code:

var fs = require( 'fs' ); var path = require( 'path' ); var process = require( 'process' ); var markdownpdf = require( 'markdown-pdf' ) var md_dir = "_pages"; var pdf_dir = "assets/pdf" // Loop through all the files in the directory fs.readdir( md_dir, function( err, files ) { if( err ) { console.error( "Could not list the directory.", err ); process.exit( 1 ); } files.forEach( function( file, index ) { // Make one pass and make the file complete var md_path = path.join( md_dir, file ); var pdf_path = path.join( pdf_dir, file + '.pdf' ); fs.stat( md_path, function( error, stat ) { if( error ) { console.error( "Error stating file.", error ); return; } // Start PDF Process on the files if( stat.isFile() ) { console.log( "'%s' is a file.", md_path ); console.log( "Stripping Jekyll Front-Matter..."); var fileContents = fs.readFileSync(md_path, 'utf8'); var pattern = /^---\n.*\n---\n/; // Strip everything between the first two sets of '---' var stripped_md = fileContents.replace( pattern, '' ); console.log( "Front-Matter stripped." ); // Pass that to markdown-pdf markdownpdf().from.string(stripped_md).to(pdf_path, function () { console.log("Created", pdf_path) } ); } // If it's not a file else if( stat.isDirectory() ) console.log( "'%s' is a directory.", md_path ); } ); } ); } );

最满意答案

在正则表达式模式中。 char匹配除换行符之外的所有内容。

FrontMatter有换行符,因此您必须将其添加到模式中。

喜欢: var pattern = /^---(.|\n)*?---\n/;

哪个表示---“标签”之间的所有内容或换行符

的? 让它不贪心

In a regexp pattern the . char matches everything except a newline.

The FrontMatter has newlines so you have to add this to the pattern.

Like: var pattern = /^---(.|\n)*?---\n/;

Which says everything or newline between the --- "tags"

The ? makes it non greedy

更多推荐

本文发布于:2023-08-07 22:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   文件   Markdown   Front   Jekyll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!