Some parts of this article have been written with AI. Be aware that AI can make mistakes.
Applies to : PowerDocs for Linux PowerDocs for Windows

Class `Pipeline`

The Pipeline class (accessed via context.pipeline) enables extensions to intercept, inspect, and transform documents before operations such as PDF export, PDF preview generation, and file saving.

Pre-processing hooks receive a traversable JsDocument instance, allowing scripts to easily inspect blocks, search for headings, tables, or custom containers, and perform macro/variable replacements before the rendering engine processes the Markdown.


Properties

Pipeline does not expose public properties.


Methods

onBeforePdfExport(handler)

Registers a transformation hook executed immediately before compiling Markdown into a PDF document.

Parameters

  • handler (function(document, env)): Callback invoked with:
    • document (JsDocument): The mutable document DOM model.
    • env (Env): Host system environment utilities.
  • If the handler returns a string, that string directly replaces the entire document content.
  • If the handler modifies document via document.replaceText(...) or document.text = ..., the modified document is automatically used.

onBeforeSave(handler)

Registers a transformation hook executed before saving the Markdown file to disk.

Parameters

  • handler (function(document, env)): Callback invoked with the mutable JsDocument and Env.

Examples

1. Dynamic Macro and Variable Expansion before PDF Export

export function activate(context) {
    context.pipeline.onBeforePdfExport((doc, env) => {
        const now = new Date();
        const dateStr = now.toLocaleDateString();

        // Replace template macros throughout the document
        doc.replaceText(/\{\{build_date\}\}/g, dateStr);
        doc.replaceText(/\{\{app_version\}\}/g, 'v2.5.0');
        doc.replaceText(/\{\{company\}\}/g, 'Acme Corporation');

        // Log count of tables found in document
        const tables = doc.find('table');
        env.log(`Preparing PDF export with ${tables.length} table(s)`);
    });
}

2. Auto-Heading Numbering Validator & Transformer

export function activate(context) {
    context.pipeline.onBeforePdfExport((doc, env) => {
        const headings = doc.find('heading');
        env.log(`Found ${headings.length} headings in document.`);

        // Ensure all Level 1 headings are logged
        headings.forEach(h => {
            if (h.level === 1) {
                env.log(`Chapter: ${h.text}`);
            }
        });
    });
}