You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all I want to thank all contributers for this amazing plug-in.
My Problem
I work on many different manuals, books and documentations etc. at the same time and I use the same header-template for nearly all markdown files but with different data like title or version for current document. So the reader can see the title or (customized) pdf-creation-date like "2025-01" on every page of PDF, like this:
I had to open the Plug-in settings and change the "Header Template" everytime I opened another markdown file (with completely other topic) and often I forgot to replace the title string in the header template (plug-in settings)... This cost me a lot of attention and time.
My Solution
Why not putting meta-data about the markdown file used for header of PDF pages directly into the markdown file itself?
This way each markdown file contains its own data for the header and I don't have to change the header for PDF in plug-in-settings no more!
The value of Markdown PDF Plug-in-Setting Header Template:
Step 1: Add Data to Script-Section of Markdown file
I put the custom values for current markdown file like title, date, author, copyright etc. directly into the markdown as variable values in a <script> section like this:
Step 3: Modify Function "async function markdownPdf(option_type)"
Search for the above function signature (in extensions.js) and add following line:
varcontent=convertMarkdownToHtml(mdfilename,type,text);/***************************** ADD THIS LINE *****************************/customPageHeaderVariables=extractVariablesFromMarkdown(content);/****************************************************************************/varhtml=makeHtml(content,uri);awaitexportPdf(html,filename,type,uri);
...
Step 3: Modify Method "function transformTemplate(templateText)" and add new Function "function generateHeaderTemplate(templateText)"
/** * Transform the text of the header or footer template, replacing the following supported placeholders: * * - `%%ISO-DATETIME%%` – For an ISO-based date and time format: `YYYY-MM-DD hh:mm:ss` * - `%%ISO-DATE%%` – For an ISO-based date format: `YYYY-MM-DD` * - `%%ISO-TIME%%` – For an ISO-based time format: `hh:mm:ss` */functiontransformTemplate(templateText){// First replace pdf-page-header-parameters by values found from <script> section in markdown,// so we can use date/time parameter keys in pdf-page-header-parameter values (from <script> section).templateText=generateHeaderTemplate(templateText);letyear=newDate().getFullYear().toString();letmonth=newDate().getMonth()+1;letmm=month<10 ? "0"+month.toString() : month.toString();letday=newDate().getDate();letdd=day<10 ? "0"+day : day.toString();templateText=templateText.replace(/%ISO-DATETIME%/g,newDate().toISOString().substr(0,19).replace('T',' ')||'').replace(/%ISO-DATE%/g,newDate().toISOString().substr(0,10)||'').replace(/%ISO-TIME%/g,newDate().toISOString().substr(11,8)||'').replace(/%yyyy%/g,year||'').replace(/%mm%/g,mm||'').replace(/%month%/g,month||'').replace(/%day%/g,day||'').replace(/%dd%/g,dd||'').replace(/%yyyy-mm%/g,newDate().getFullYear().toString()+"-"+mm||'');returntemplateText;}/********************* NEW FUNCTION *****************/functiongenerateHeaderTemplate(templateText){letupdatedTemplate=templateText;updatedTemplate=updatedTemplate.replace(/%pdfPageHeaderTitle%/g,customPageHeaderVariables.pdfPageHeaderTitle||'').replace(/%pdfPageHeaderVersion%/g,customPageHeaderVariables.pdfPageHeaderVersion||'').replace(/%pdfPageHeaderDate%/g,customPageHeaderVariables.pdfPageHeaderDate||'').replace(/%pdfPageHeaderCopyright%/g,customPageHeaderVariables.pdfPageHeaderCopyright||'').replace(/%pdfPageHeaderAuthor%/g,customPageHeaderVariables.pdfPageHeaderAuthor||'');returnupdatedTemplate;}/***************************************************/...
Step 4
Restart VS Code!
Last Step
Open your markdown file (containing the <script> section with pdf-header values as variables (if not already done after restarting VS Code) and press CTRL + S keys to save, if you have enabled auto-convert to PDF, else do it by calling the command.
From now on, you don't need to touch the Header Template in the Markdown PDF Plug-in Settings anymore :-)
As you may noticed, I also changed the code to for replacing %%%ISO-DATE%%% etc. in the way of calling replace method (by regular expression), reduced the usage of % character from triple to only one, and also added following keys for more flexibility:
%yyyy% for current year, like: 2025
%month% for current month, like: 1 (for january)
%mm% for current month, like: 01 (for january)
%day% for current day, like: 1, ..., 31
%dd% for current day, like: 01, ..., 31
%yyyy-mm% for current year and mnoth, like: 2025-01
You can also use the date-keys (like %ISO-DATE% or %yyyy% etc.) in the pdf-header-variable-values in the <script> section.
You also can put the author key %pdfPageHeaderAuthor% into the value of pdfPageHeaderCopyright.
I hope this can help some of you with the same problem.
The text was updated successfully, but these errors were encountered:
First of all I want to thank all contributers for this amazing plug-in.
My Problem
I work on many different manuals, books and documentations etc. at the same time and I use the same header-template for nearly all markdown files but with different data like title or version for current document. So the reader can see the title or (customized) pdf-creation-date like "2025-01" on every page of PDF, like this:
I had to open the Plug-in settings and change the "Header Template" everytime I opened another markdown file (with completely other topic) and often I forgot to replace the title string in the header template (plug-in settings)... This cost me a lot of attention and time.
My Solution
Why not putting meta-data about the markdown file used for header of PDF pages directly into the markdown file itself?
This way each markdown file contains its own data for the header and I don't have to change the header for PDF in plug-in-settings no more!
The value of Markdown PDF Plug-in-Setting Header Template:
Step 1: Add Data to Script-Section of Markdown file
I put the custom values for current markdown file like title, date, author, copyright etc. directly into the markdown as variable values in a
<script>
section like this:Step 2: Add Variables and Function to extract them into extension.js
Step 3: Modify Function "async function markdownPdf(option_type)"
Search for the above function signature (in extensions.js) and add following line:
Step 3: Modify Method "function transformTemplate(templateText)" and add new Function "function generateHeaderTemplate(templateText)"
Step 4
Restart VS Code!
Last Step
Open your markdown file (containing the <script> section with pdf-header values as variables (if not already done after restarting VS Code) and press CTRL + S keys to save, if you have enabled auto-convert to PDF, else do it by calling the command.
From now on, you don't need to touch the Header Template in the Markdown PDF Plug-in Settings anymore :-)
As you may noticed, I also changed the code to for replacing
%%%ISO-DATE%%%
etc. in the way of callingreplace
method (by regular expression), reduced the usage of % character from triple to only one, and also added following keys for more flexibility:%yyyy%
for current year, like: 2025%month%
for current month, like: 1 (for january)%mm%
for current month, like: 01 (for january)%day%
for current day, like: 1, ..., 31%dd%
for current day, like: 01, ..., 31%yyyy-mm%
for current year and mnoth, like: 2025-01You can also use the date-keys (like
%ISO-DATE%
or%yyyy%
etc.) in the pdf-header-variable-values in the<script>
section.You also can put the author key
%pdfPageHeaderAuthor%
into the value ofpdfPageHeaderCopyright
.I hope this can help some of you with the same problem.
The text was updated successfully, but these errors were encountered: