@cenk1cenk2/md-printer 2.3.3 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -70,7 +70,7 @@ var MDPrinter = class extends Command {
70
70
  { task: async (ctx) => {
71
71
  const file = join(process.cwd(), this.args.file);
72
72
  if (!this.fs.exists(file)) throw new Error(`File does not exists: ${file}`);
73
- this.logger.debug("Loading file: %s", file);
73
+ this.logger.info("Loading file: %s", file);
74
74
  ctx.file = file;
75
75
  switch (extname(ctx.file)) {
76
76
  case ".md": {
@@ -91,7 +91,7 @@ var MDPrinter = class extends Command {
91
91
  { task: async (ctx) => {
92
92
  const template = ctx.metadata?.template ?? this.flags.template;
93
93
  ctx.templates = (/* @__PURE__ */ new RegExp(/\.\.?\//)).test(template) ? join(process.cwd(), template) : join(this.config.root, TEMPLATE_DIRECTORY, template);
94
- this.logger.debug("Loading template: %s from %s", template, ctx.templates);
94
+ this.logger.info("Loading template: %s from %s", template, ctx.templates);
95
95
  await Promise.all(RequiredTemplateFiles.map(async (file) => {
96
96
  const current = join(ctx.templates, file);
97
97
  if (!this.fs.exists(current)) throw new Error(`Template does not exists: ${current}`);
@@ -159,20 +159,20 @@ var MDPrinter = class extends Command {
159
159
  async runMd2Pdf(ctx) {
160
160
  let pdf;
161
161
  if (ctx.template) {
162
- this.logger.debug("Rendering as template.");
162
+ this.logger.info("Rendering as template.");
163
163
  pdf = await mdToPdf({ content: this.nunjucks.renderString(ctx.template, {
164
164
  ...ctx.metadata ?? {},
165
165
  content: ctx.content
166
166
  }) }, ctx.options);
167
167
  } else {
168
- this.logger.debug("Rendering as plain file.");
168
+ this.logger.info("Rendering as plain file.");
169
169
  pdf = await mdToPdf({ content: ctx.content }, ctx.options);
170
170
  }
171
171
  const output = pdf.filename;
172
172
  await this.fs.mkdir(dirname(output));
173
173
  if (!output) throw new Error("Output should either be defined with the variable or front-matter.");
174
174
  else if (!OUTPUT_FILE_ACCEPTED_TYPES.includes(extname(output))) throw new Error(`Output file should be ending with the extension: ${OUTPUT_FILE_ACCEPTED_TYPES.join(", ")} -> current: ${extname(output)}`);
175
- this.logger.debug("Output file will be: %s", output);
175
+ this.logger.info("Output file will be: %s", output);
176
176
  if (pdf) {
177
177
  this.logger.info("Writing file to output: %s", output);
178
178
  await this.fs.write(output, pdf.content);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["markdown: string","paths: Record<TemplateFiles, string>","ctx: MdPrinterCtx","pdf: PdfOutput"],"sources":["../../src/commands/index.ts"],"sourcesContent":["import tailwind from '@tailwindcss/postcss'\nimport { watch } from 'chokidar'\nimport { default as graymatter } from 'gray-matter'\nimport { mdToPdf } from 'md-to-pdf'\nimport type { PdfConfig } from 'md-to-pdf/dist/lib/config.js'\nimport type { PdfOutput } from 'md-to-pdf/dist/lib/generate-output.js'\nimport Nunjucks from 'nunjucks'\nimport { basename, dirname, extname, join } from 'path'\nimport postcss from 'postcss'\nimport showdown from 'showdown'\n\nimport type { ShouldRunAfterHook, ShouldRunBeforeHook } from '@cenk1cenk2/oclif-common'\nimport { Args, Flags, Command, ConfigService, FileSystemService, ParserService, JsonParser, YamlParser } from '@cenk1cenk2/oclif-common'\nimport { OUTPUT_FILE_ACCEPTED_TYPES, RequiredTemplateFiles, TEMPLATE_DIRECTORY, TemplateFiles } from '@constants'\nimport type { MdPrinterCtx } from '@interfaces'\n\nexport default class MDPrinter extends Command<typeof MDPrinter, MdPrinterCtx> implements ShouldRunBeforeHook, ShouldRunAfterHook {\n static description = 'Generates a PDF from the given markdown file with the selected HTML template.'\n\n static flags = {\n template: Flags.string({\n char: 't',\n default: 'default',\n description: 'HTML template for the generated PDF file.'\n }),\n title: Flags.string({\n char: 'T',\n description: 'Overwrite document title.'\n }),\n browser: Flags.string({\n char: 'b',\n description: 'Browser path that is going to be used for the PDF generation.',\n default: '/usr/bin/brave'\n }),\n watch: Flags.boolean({\n char: 'w',\n description: 'Watch the changes on the given file.'\n }),\n dev: Flags.boolean({\n char: 'd',\n description: 'Run with Chrome browser instead of publishing the file.'\n })\n }\n\n static args = {\n file: Args.string({\n description: 'File to be processed.',\n required: true\n }),\n output: Args.string({\n description: 'Output file that will be generated. Overwrites the one define in front-matter.',\n required: false\n })\n }\n\n private nunjucks = Nunjucks.configure({\n autoescape: false,\n throwOnUndefined: true,\n trimBlocks: true,\n lstripBlocks: false\n })\n private cs: ConfigService\n private fs: FileSystemService\n\n public async shouldRunBefore(): Promise<void> {\n this.cs = this.app.get(ConfigService)\n this.fs = this.app.get(FileSystemService)\n\n await this.app.get(ParserService).register(JsonParser, YamlParser)\n\n this.nunjucks.addFilter('markdown_to_html', (markdown: string) => {\n return new showdown.Converter().makeHtml(markdown)\n })\n this.tasks.options = { silentRendererCondition: true }\n }\n\n public async run(): Promise<void> {\n this.tasks.add([\n {\n task: async(ctx): Promise<void> => {\n const file = join(process.cwd(), this.args.file)\n\n if (!this.fs.exists(file)) {\n throw new Error(`File does not exists: ${file}`)\n }\n\n this.logger.debug('Loading file: %s', file)\n\n ctx.file = file\n switch (extname(ctx.file)) {\n case '.md': {\n const data = graymatter.read(ctx.file)\n\n ctx.content = await this.fs.read(file)\n ctx.content = data.content\n\n ctx.metadata = data.data\n\n break\n }\n\n case '.yml': {\n ctx.content = await this.fs.read(file)\n\n ctx.metadata = await this.app.get(ParserService).parse(ctx.file, ctx.content)\n\n break\n }\n\n default:\n throw new Error('File type is not accepted.')\n }\n }\n },\n\n {\n task: async(ctx): Promise<void> => {\n const template = ctx.metadata?.template ?? this.flags.template\n\n ctx.templates = new RegExp(/\\.\\.?\\//).test(template) ? join(process.cwd(), template) : join(this.config.root, TEMPLATE_DIRECTORY, template)\n\n this.logger.debug('Loading template: %s from %s', template, ctx.templates)\n\n await Promise.all(\n RequiredTemplateFiles.map(async(file) => {\n const current = join(ctx.templates, file)\n\n if (!this.fs.exists(current)) {\n throw new Error(`Template does not exists: ${current}`)\n }\n })\n )\n\n const paths: Record<TemplateFiles, string> = {\n [TemplateFiles.SETTINGS]: join(ctx.templates, TemplateFiles.SETTINGS),\n [TemplateFiles.CSS]: join(ctx.templates, TemplateFiles.CSS),\n [TemplateFiles.TAILWIND_CSS]: join(ctx.templates, TemplateFiles.TAILWIND_CSS),\n [TemplateFiles.HEADER]: join(ctx.templates, TemplateFiles.HEADER),\n [TemplateFiles.FOOTER]: join(ctx.templates, TemplateFiles.FOOTER),\n [TemplateFiles.TEMPLATE]: join(ctx.templates, TemplateFiles.TEMPLATE)\n }\n\n ctx.options = await this.cs.extend<PdfConfig>([\n paths[TemplateFiles.SETTINGS],\n {\n dest: this.args?.output ?? ctx.metadata?.dest ?? `${basename(this.args.file, extname(this.args.file))}.pdf`,\n document_title: ctx.metadata?.document_title ?? this.flags.title ?? this.args.file,\n // https://github.com/simonhaenisch/md-to-pdf/issues/247\n launch_options: {\n executablePath: this.flags.browser\n // headless: true\n }\n }\n ])\n\n this.logger.debug('Options: %o', ctx.options)\n\n if (this.fs.exists(paths[TemplateFiles.HEADER])) {\n this.logger.debug('Header exists for template.')\n\n ctx.options.pdf_options.headerTemplate = await this.fs.read(paths[TemplateFiles.HEADER])\n }\n\n if (this.fs.exists(paths[TemplateFiles.FOOTER])) {\n this.logger.debug('Footer exists for template.')\n\n ctx.options.pdf_options.footerTemplate = await this.fs.read(paths[TemplateFiles.FOOTER])\n }\n\n if (this.fs.exists(paths[TemplateFiles.TEMPLATE])) {\n this.logger.debug('Design template exists for template.')\n\n ctx.template = await this.fs.read(paths[TemplateFiles.TEMPLATE])\n\n this.logger.debug('Metadata: %o', ctx.metadata)\n }\n\n if (this.fs.exists(paths[TemplateFiles.CSS])) {\n this.logger.debug('CSS exists for template.')\n ctx.options.css = await this.fs.read(paths[TemplateFiles.CSS])\n }\n\n if (this.fs.exists(paths[TemplateFiles.TAILWIND_CSS])) {\n this.logger.debug('Tailwind CSS exists for template: %s -> %s', paths[TemplateFiles.TAILWIND_CSS])\n\n ctx.options.css = await postcss([\n tailwind({\n // content: [{ raw: ctx.template, extension: 'html' }],\n base: ctx.templates\n })\n ])\n .process(await this.fs.read(paths[TemplateFiles.TAILWIND_CSS]), { from: paths[TemplateFiles.TAILWIND_CSS] })\n .then((result) => result.css)\n }\n }\n },\n\n {\n task: async(ctx): Promise<void> => {\n if (this.flags.dev) {\n ctx.options.devtools = true\n\n this.logger.info('Running in dev mode.')\n }\n }\n }\n ])\n }\n\n public async shouldRunAfter(ctx: MdPrinterCtx): Promise<void> {\n if (this.flags.watch) {\n this.logger.info('Running in watch mode.')\n\n watch([TEMPLATE_DIRECTORY, this.args.file, join(ctx.templates, '**/*')]).on('change', async() => {\n await this.run()\n await this.runTasks()\n\n this.logger.info('Waiting for the next change.')\n\n return this.runMd2Pdf(ctx)\n })\n }\n\n return this.runMd2Pdf(ctx)\n }\n\n private async runMd2Pdf(ctx: MdPrinterCtx): Promise<void> {\n let pdf: PdfOutput\n\n if (ctx.template) {\n this.logger.debug('Rendering as template.')\n pdf = await mdToPdf({ content: this.nunjucks.renderString(ctx.template, { ...(ctx.metadata ?? {}), content: ctx.content }) }, ctx.options)\n } else {\n this.logger.debug('Rendering as plain file.')\n pdf = await mdToPdf({ content: ctx.content }, ctx.options)\n }\n\n const output = pdf.filename\n\n await this.fs.mkdir(dirname(output))\n\n if (!output) {\n throw new Error('Output should either be defined with the variable or front-matter.')\n } else if (!OUTPUT_FILE_ACCEPTED_TYPES.includes(extname(output))) {\n throw new Error(`Output file should be ending with the extension: ${OUTPUT_FILE_ACCEPTED_TYPES.join(', ')} -> current: ${extname(output)}`)\n }\n\n this.logger.debug('Output file will be: %s', output)\n\n if (pdf) {\n this.logger.info('Writing file to output: %s', output)\n\n await this.fs.write(output, pdf.content)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAgBA,IAAqB,YAArB,cAAuC,QAA2F;CAChI,OAAO,cAAc;CAErB,OAAO,QAAQ;EACb,UAAU,MAAM,OAAO;GACrB,MAAM;GACN,SAAS;GACT,aAAa;EACd,EAAC;EACF,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OAAO;AAEtD,SAAM,KAAK,GAAG,MAAM,QAAQ,IAAI,QAAQ;EACzC;CACF;AACF"}
1
+ {"version":3,"file":"index.js","names":["markdown: string","paths: Record<TemplateFiles, string>","ctx: MdPrinterCtx","pdf: PdfOutput"],"sources":["../../src/commands/index.ts"],"sourcesContent":["import tailwind from '@tailwindcss/postcss'\nimport { watch } from 'chokidar'\nimport { default as graymatter } from 'gray-matter'\nimport { mdToPdf } from 'md-to-pdf'\nimport type { PdfConfig } from 'md-to-pdf/dist/lib/config.js'\nimport type { PdfOutput } from 'md-to-pdf/dist/lib/generate-output.js'\nimport Nunjucks from 'nunjucks'\nimport { basename, dirname, extname, join } from 'path'\nimport postcss from 'postcss'\nimport showdown from 'showdown'\n\nimport type { ShouldRunAfterHook, ShouldRunBeforeHook } from '@cenk1cenk2/oclif-common'\nimport { Args, Flags, Command, ConfigService, FileSystemService, ParserService, JsonParser, YamlParser } from '@cenk1cenk2/oclif-common'\nimport { OUTPUT_FILE_ACCEPTED_TYPES, RequiredTemplateFiles, TEMPLATE_DIRECTORY, TemplateFiles } from '@constants'\nimport type { MdPrinterCtx } from '@interfaces'\n\nexport default class MDPrinter extends Command<typeof MDPrinter, MdPrinterCtx> implements ShouldRunBeforeHook, ShouldRunAfterHook {\n static description = 'Generates a PDF from the given markdown file with the selected HTML template.'\n\n static flags = {\n template: Flags.string({\n char: 't',\n default: 'default',\n description: 'HTML template for the generated PDF file.'\n }),\n title: Flags.string({\n char: 'T',\n description: 'Overwrite document title.'\n }),\n browser: Flags.string({\n char: 'b',\n description: 'Browser path that is going to be used for the PDF generation.',\n default: '/usr/bin/brave'\n }),\n watch: Flags.boolean({\n char: 'w',\n description: 'Watch the changes on the given file.'\n }),\n dev: Flags.boolean({\n char: 'd',\n description: 'Run with Chrome browser instead of publishing the file.'\n })\n }\n\n static args = {\n file: Args.string({\n description: 'File to be processed.',\n required: true\n }),\n output: Args.string({\n description: 'Output file that will be generated. Overwrites the one define in front-matter.',\n required: false\n })\n }\n\n private nunjucks = Nunjucks.configure({\n autoescape: false,\n throwOnUndefined: true,\n trimBlocks: true,\n lstripBlocks: false\n })\n private cs: ConfigService\n private fs: FileSystemService\n\n public async shouldRunBefore(): Promise<void> {\n this.cs = this.app.get(ConfigService)\n this.fs = this.app.get(FileSystemService)\n\n await this.app.get(ParserService).register(JsonParser, YamlParser)\n\n this.nunjucks.addFilter('markdown_to_html', (markdown: string) => {\n return new showdown.Converter().makeHtml(markdown)\n })\n this.tasks.options = { silentRendererCondition: true }\n }\n\n public async run(): Promise<void> {\n this.tasks.add([\n {\n task: async(ctx): Promise<void> => {\n const file = join(process.cwd(), this.args.file)\n\n if (!this.fs.exists(file)) {\n throw new Error(`File does not exists: ${file}`)\n }\n\n this.logger.info('Loading file: %s', file)\n\n ctx.file = file\n switch (extname(ctx.file)) {\n case '.md': {\n const data = graymatter.read(ctx.file)\n\n ctx.content = await this.fs.read(file)\n ctx.content = data.content\n\n ctx.metadata = data.data\n\n break\n }\n\n case '.yml': {\n ctx.content = await this.fs.read(file)\n\n ctx.metadata = await this.app.get(ParserService).parse(ctx.file, ctx.content)\n\n break\n }\n\n default:\n throw new Error('File type is not accepted.')\n }\n }\n },\n\n {\n task: async(ctx): Promise<void> => {\n const template = ctx.metadata?.template ?? this.flags.template\n\n ctx.templates = new RegExp(/\\.\\.?\\//).test(template) ? join(process.cwd(), template) : join(this.config.root, TEMPLATE_DIRECTORY, template)\n\n this.logger.info('Loading template: %s from %s', template, ctx.templates)\n\n await Promise.all(\n RequiredTemplateFiles.map(async(file) => {\n const current = join(ctx.templates, file)\n\n if (!this.fs.exists(current)) {\n throw new Error(`Template does not exists: ${current}`)\n }\n })\n )\n\n const paths: Record<TemplateFiles, string> = {\n [TemplateFiles.SETTINGS]: join(ctx.templates, TemplateFiles.SETTINGS),\n [TemplateFiles.CSS]: join(ctx.templates, TemplateFiles.CSS),\n [TemplateFiles.TAILWIND_CSS]: join(ctx.templates, TemplateFiles.TAILWIND_CSS),\n [TemplateFiles.HEADER]: join(ctx.templates, TemplateFiles.HEADER),\n [TemplateFiles.FOOTER]: join(ctx.templates, TemplateFiles.FOOTER),\n [TemplateFiles.TEMPLATE]: join(ctx.templates, TemplateFiles.TEMPLATE)\n }\n\n ctx.options = await this.cs.extend<PdfConfig>([\n paths[TemplateFiles.SETTINGS],\n {\n dest: this.args?.output ?? ctx.metadata?.dest ?? `${basename(this.args.file, extname(this.args.file))}.pdf`,\n document_title: ctx.metadata?.document_title ?? this.flags.title ?? this.args.file,\n // https://github.com/simonhaenisch/md-to-pdf/issues/247\n launch_options: {\n executablePath: this.flags.browser\n // headless: true\n }\n }\n ])\n\n this.logger.debug('Options: %o', ctx.options)\n\n if (this.fs.exists(paths[TemplateFiles.HEADER])) {\n this.logger.debug('Header exists for template.')\n\n ctx.options.pdf_options.headerTemplate = await this.fs.read(paths[TemplateFiles.HEADER])\n }\n\n if (this.fs.exists(paths[TemplateFiles.FOOTER])) {\n this.logger.debug('Footer exists for template.')\n\n ctx.options.pdf_options.footerTemplate = await this.fs.read(paths[TemplateFiles.FOOTER])\n }\n\n if (this.fs.exists(paths[TemplateFiles.TEMPLATE])) {\n this.logger.debug('Design template exists for template.')\n\n ctx.template = await this.fs.read(paths[TemplateFiles.TEMPLATE])\n\n this.logger.debug('Metadata: %o', ctx.metadata)\n }\n\n if (this.fs.exists(paths[TemplateFiles.CSS])) {\n this.logger.debug('CSS exists for template.')\n ctx.options.css = await this.fs.read(paths[TemplateFiles.CSS])\n }\n\n if (this.fs.exists(paths[TemplateFiles.TAILWIND_CSS])) {\n this.logger.debug('Tailwind CSS exists for template: %s -> %s', paths[TemplateFiles.TAILWIND_CSS])\n\n ctx.options.css = await postcss([\n tailwind({\n // content: [{ raw: ctx.template, extension: 'html' }],\n base: ctx.templates\n })\n ])\n .process(await this.fs.read(paths[TemplateFiles.TAILWIND_CSS]), { from: paths[TemplateFiles.TAILWIND_CSS] })\n .then((result) => result.css)\n }\n }\n },\n\n {\n task: async(ctx): Promise<void> => {\n if (this.flags.dev) {\n ctx.options.devtools = true\n\n this.logger.info('Running in dev mode.')\n }\n }\n }\n ])\n }\n\n public async shouldRunAfter(ctx: MdPrinterCtx): Promise<void> {\n if (this.flags.watch) {\n this.logger.info('Running in watch mode.')\n\n watch([TEMPLATE_DIRECTORY, this.args.file, join(ctx.templates, '**/*')]).on('change', async() => {\n await this.run()\n await this.runTasks()\n\n this.logger.info('Waiting for the next change.')\n\n return this.runMd2Pdf(ctx)\n })\n }\n\n return this.runMd2Pdf(ctx)\n }\n\n private async runMd2Pdf(ctx: MdPrinterCtx): Promise<void> {\n let pdf: PdfOutput\n\n if (ctx.template) {\n this.logger.info('Rendering as template.')\n pdf = await mdToPdf({ content: this.nunjucks.renderString(ctx.template, { ...(ctx.metadata ?? {}), content: ctx.content }) }, ctx.options)\n } else {\n this.logger.info('Rendering as plain file.')\n pdf = await mdToPdf({ content: ctx.content }, ctx.options)\n }\n\n const output = pdf.filename\n\n await this.fs.mkdir(dirname(output))\n\n if (!output) {\n throw new Error('Output should either be defined with the variable or front-matter.')\n } else if (!OUTPUT_FILE_ACCEPTED_TYPES.includes(extname(output))) {\n throw new Error(`Output file should be ending with the extension: ${OUTPUT_FILE_ACCEPTED_TYPES.join(', ')} -> current: ${extname(output)}`)\n }\n\n this.logger.info('Output file will be: %s', output)\n\n if (pdf) {\n this.logger.info('Writing file to output: %s', output)\n\n await this.fs.write(output, pdf.content)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAgBA,IAAqB,YAArB,cAAuC,QAA2F;CAChI,OAAO,cAAc;CAErB,OAAO,QAAQ;EACb,UAAU,MAAM,OAAO;GACrB,MAAM;GACN,SAAS;GACT,aAAa;EACd,EAAC;EACF,OAAO,MAAM,OAAO;GAClB,MAAM;GACN,aAAa;EACd,EAAC;EACF,SAAS,MAAM,OAAO;GACpB,MAAM;GACN,aAAa;GACb,SAAS;EACV,EAAC;EACF,OAAO,MAAM,QAAQ;GACnB,MAAM;GACN,aAA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3"
110
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cenk1cenk2/md-printer",
3
- "version": "2.3.3",
3
+ "version": "2.4.0",
4
4
  "description": "A markdown printer.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",