Skip to content

Commit

Permalink
Merge branch 'date-format' of https://github.com/poke/vscode-markdown…
Browse files Browse the repository at this point in the history
…-pdf into feature/#197
  • Loading branch information
yzane committed Aug 17, 2023
2 parents 0c3e886 + 9f48706 commit 50f8ed9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ If the download is not successful or you want to avoid downloading every time yo
- `<span class='url'></span>` : markdown full path name
- `<span class='pageNumber'></span>` : current page number
- `<span class='totalPages'></span>` : total pages in the document
- `%%ISO-DATETIME%%` : current date and time in ISO-based format (`YYYY-MM-DD hh:mm:ss`)
- `%%ISO-DATE%%` : current date in ISO-based format (`YYYY-MM-DD`)
- `%%ISO-TIME%%` : current time in ISO-based format (`hh:mm:ss`)

```javascript
"markdown-pdf.headerTemplate": "<div style=\"font-size: 9px; margin-left: 1cm;\"> <span class='title'></span></div> <div style=\"font-size: 9px; margin-left: auto; margin-right: 1cm; \"> <span class='date'></span></div>",
Expand Down
25 changes: 23 additions & 2 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ function exportPdf(data, filename, type, uri) {
path: exportFilename,
scale: vscode.workspace.getConfiguration('markdown-pdf', uri)['scale'],
displayHeaderFooter: vscode.workspace.getConfiguration('markdown-pdf', uri)['displayHeaderFooter'],
headerTemplate: vscode.workspace.getConfiguration('markdown-pdf', uri)['headerTemplate'] || '',
footerTemplate: vscode.workspace.getConfiguration('markdown-pdf', uri)['footerTemplate'] || '',
headerTemplate: transformTemplate(vscode.workspace.getConfiguration('markdown-pdf', uri)['headerTemplate'] || ''),
footerTemplate: transformTemplate(vscode.workspace.getConfiguration('markdown-pdf', uri)['footerTemplate'] || ''),
printBackground: vscode.workspace.getConfiguration('markdown-pdf', uri)['printBackground'],
landscape: landscape_option,
pageRanges: vscode.workspace.getConfiguration('markdown-pdf', uri)['pageRanges'] || '',
Expand Down Expand Up @@ -506,6 +506,27 @@ function exportPdf(data, filename, type, uri) {
); // vscode.window.withProgress
}

/**
* 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`
*/
function transformTemplate(templateText) {
if (templateText.indexOf('%%ISO-DATETIME%%') !== -1) {
templateText = templateText.replace('%%ISO-DATETIME%%', new Date().toISOString().substr(0, 19).replace('T', ' '));
}
if (templateText.indexOf('%%ISO-DATE%%') !== -1) {
templateText = templateText.replace('%%ISO-DATE%%', new Date().toISOString().substr(0, 10));
}
if (templateText.indexOf('%%ISO-TIME%%') !== -1) {
templateText = templateText.replace('%%ISO-TIME%%', new Date().toISOString().substr(11, 8));
}

return templateText;
}

function isExistsPath(path) {
if (path.length === 0) {
return false;
Expand Down

0 comments on commit 50f8ed9

Please sign in to comment.