// you may not need all of these but they come up a lot
import fs from "fs" ;
import assert from "assert" ;
import logging from '../lib/logging.js' ;
import glob from "fast-glob" ;
import path from "path" ;
import template from "lodash/template.js" ;
import { defer } from "../lib/api.js" ;
import PDFDocument from "pdfkit" ;
const log = logging . create ( import . meta . url ) ;
const title _box = { x : 17 , y : 415 , width : 405 , align : "center" } ;
const summary _box = { width : 405 , height : 540 } ;
const main _title = { align : "center" , width : 1196 , height : 883 , x : 586 , y : 99 } ;
const main _box = { width : 928 , height : 535 , x : 720 , y : 323 } ;
export const description = "Generates a PDF presentation from an input .md file and a background template."
// your command uses the npm package commander's options format
export const options = [
[ "--heading-font <path>" , "Font file to use for headings." , "static/fonts/VictorMono-Bold.ttf" ] ,
[ "--body-font <path>" , "Font file to use for body text." , "static/fonts/VictorMono-Medium.ttf" ] ,
[ "--output <path>" , "The output .pdf file to write. Defaults to <input>.pdf" ] ,
]
// put required options in the required variable
export const required = [
[ "--input <path>" , "The input .md file for the generated presentation." ] ,
[ "--template <path>" , "The background template image to use." ] ,
]
// handy function for checking things are good and aborting
const check = ( test , fail _message ) => {
if ( ! test ) {
log . error ( fail _message ) ;
process . exit ( 1 ) ;
}
}
export const start _pdf = ( opts ) => {
const doc = new PDFDocument ( {
font : "Courier" ,
size : [ 1920 , 1080 ] ,
info : {
"Title" : "Test" ,
"Author" : "Zed A. Shaw" ,
"Subject" : "Test subject" ,
"Keywords" : "test test test" ,
"CreationDate" : new Date ( ) ,
"ModDate" : new Date ( ) ,
} } ) ;
const out _stream = doc . pipe ( fs . createWriteStream ( opts . output ) ) ;
doc . registerFont ( "heading" , opts . headingFont ) ;
doc . registerFont ( "body" , opts . bodyFont ) ;
return [ doc , out _stream ] ;
}
const write _page = ( doc , template , content ) => {
// place the background image
doc . image ( template , 0 , 0 , {
width : 1920 ,
height : 1080
} ) ;
// create the left side constant summary
doc . fontSize ( 40 ) ;
doc . font ( "heading" ) . text ( content . title , title _box . x , title _box . y , title _box ) ;
doc . fontSize ( 30 ) ;
doc . moveDown ( ) ;
// the lesson summary
doc . font ( "body" ) . text ( content . summary , summary _box ) ;
doc . fillColor ( "white" ) ;
if ( content . type == "title-bullets" ) {
doc . fontSize ( 120 ) ;
// add the title/body text
doc . font ( "heading" ) . text ( content . slide _title , main _title . x , main _title . y , main _title ) ;
doc . fontSize ( 80 ) ;
doc . list ( content . slide _body , main _box . x , main _box . y , main _box ) ;
} else if ( content . type == "title-image" ) {
// it's a title only slide
doc . fontSize ( 120 ) ;
// add the title/body text
doc . font ( "heading" ) . text ( content . slide _title , main _title . x , main _title . y , main _title ) ;
doc . image ( content . slide _body , main _box . x , main _box . y , {
fit : [ main _box . width , main _box . height ] ,
align : "center" ,
valign : "center"
} ) ;
} else if ( content . type == "title-only" ) {
// it's a title only slide
doc . fontSize ( 160 ) ;
doc . font ( "heading" ) . text ( content . slide _title , main _title . x , main _title . y + ( main _title . height / 3.5 ) , main _title ) ;
} else {
// this handles title-text slides, and anything else
doc . fontSize ( 120 ) ;
// add the title/body text
doc . font ( "heading" ) . text ( content . slide _title , main _title . x , main _title . y , main _title ) ;
// if I put a # I want this to be a subtitle
if ( content . slide _body . startsWith ( "#" ) ) {
doc . fontSize ( 100 ) ;
const trim _body = content . slide _body . slice ( 1 ) . trim ( ) ;
doc . font ( "body" ) . text ( trim _body , main _box . x , main _box . y , { align : "center" , ... main _box } ) ;
} else {
doc . fontSize ( 60 ) ;
doc . font ( "body" ) . text ( content . slide _body , main _box . x , main _box . y , main _box ) ;
}
}
}
const next _page = ( doc ) => {
doc . addPage ( ) ;
}
const end _pdf = ( doc ) => {
doc . end ( ) ;
}
const parse _input = ( input ) => {
const data = fs . readFileSync ( input ) . toString ( ) ;
const result = [ ] ;
const [ head _data , slides _data ] = data . split ( "===" ) ;
const head = JSON . parse ( head _data ) ;
for ( let slide of slides _data . split ( "---" ) ) {
const split = slide . trim ( ) . split ( "\n" ) ;
const title = split . shift ( ) ;
const body = split . join ( "\n" ) . trim ( ) ;
if ( title || body ) {
const page = {
slide _title : title ,
slide _body : body ,
... head
}
if ( ! body ) {
page . type = "title-only" ;
} else if ( body . startsWith ( "*" ) ) {
page . type = "title-bullets" ;
page . slide _body = body . split ( "\n" ) . map ( l => l . slice ( 1 ) . trim ( ) ) ;
} else if ( body . startsWith ( "[" ) ) {
page . type = "title-image" ;
page . slide _body = body . slice ( 1 , - 1 ) ;
} else {
page . type = "title-text" ;
}
result . push ( page ) ;
}
}
return result ;
}
const make _pdf _path = ( input ) => {
const result = path . parse ( input ) ;
return path . join ( result . dir , ` ${ result . name } .pdf ` ) ;
}
const generate _presentation = ( opts , waiting ) => {
try {
if ( ! opts . output ) {
opts . output = make _pdf _path ( opts . input ) ;
}
const [ doc , out _stream ] = start _pdf ( opts ) ;
out _stream . on ( "finish" , ( ) => waiting . resolve ( ) ) ;
const pages = parse _input ( opts . input ) ;
for ( let i = 0 ; i < pages . length ; i ++ ) {
const page = pages [ i ] ;
write _page ( doc , opts . template , page ) ;
// don't add a page when at the end
if ( i !== pages . length - 1 ) {
next _page ( doc ) ;
}
}
end _pdf ( doc ) ;
} catch ( error ) {
console . error ( error ) ;
waiting . resolve ( ) ;
}
}
export const main = async ( opts ) => {
const in _files = glob . sync ( opts . input ) ;
for ( let file of in _files ) {
console . log ( file ) ;
const waiting = defer ( file ) ;
const settings = { ... opts } ;
settings . input = file ;
generate _presentation ( settings , waiting ) ;
await waiting ;
}
process . exit ( 0 ) ;
}