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 `JsDocument` & Document DOM Model

The JsDocument class represents a lightweight, structured Document Object Model (DOM) of a Markdown file. It allows extensions to easily query, traverse, inspect, and modify the document structure (blocks, headings, tables, code fences, custom containers, plain text).


📋 JsDocument Properties

Property Type Description
text string The complete raw Markdown text of the document. Assigning a new string reparses the DOM blocks.
blocks JsBlock[] Array of all top-level logical blocks in the document.
lineCount number Total number of lines in the document.

⚙️ JsDocument Methods

find(selector) / findAll(selector)

Queries and returns all blocks matching the given selector string.

Supported Selectors

  • '*' : Matches all blocks.
  • 'heading' or 'header' : Matches all headings (Levels 1 to 6).
  • 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' : Matches headings of a specific level.
  • 'table' : Matches all Markdown tables.
  • 'code' or 'codeblock' : Matches all fenced code blocks (```).
  • 'code:csharp', 'code:json' : Matches code blocks of a specific language tag.
  • 'custom' or 'container' : Matches custom containers (::: tag ... :::).
  • 'swagger', 'DO', 'DONT' or 'custom:swagger' : Matches custom containers with a specific tag name.
  • 'paragraph' : Matches standard text paragraphs.

Returns

Array of JsBlock matching the query.


findText(pattern)

Finds all textual occurrences matching a plain string or regular expression across the document.

Parameters

  • pattern (string): Text or regex pattern to search for.

Returns

Array of JsTextMatch objects:

  • match.text (string): Matched string.
  • match.line (number): 1-based line number in the document.
  • match.index (number): Starting character offset.
  • match.length (number): Character length.
  • match.block (JsBlock | null): The block containing the match.

replaceText(search, replacement)

Replaces matching strings or regex patterns throughout the document and automatically updates the block hierarchy.

Parameters

  • search (string | RegExp): Substring or regex to find.
  • replacement (string): Replacement text.

Returns

number: Number of replacements performed (1 if changed, 0 otherwise).


toMarkdown()

Returns the current, up-to-date Markdown string of the document.


📦 Class JsBlock

Represents a single logical block within the document.

Properties

Property Type Description
type string Block type: 'paragraph', 'heading', 'table', 'code', 'custom'.
level number Heading level (1 to 6), or 0 for other blocks.
text string Text content of the block (without enclosing fences/markers).
tag string Language for code blocks (e.g. 'csharp') or name for custom blocks (e.g. 'swagger').
args string First-line arguments passed to custom blocks (e.g. 'GET /api/v1/users').
lineNumber number 1-based starting line number of the block in the document.
index number Sequential index of the block.
lines string[] Array of raw lines composing the block.
children JsBlock[] Child blocks for nested containers.

Table-Specific Properties

Property Type Description
headers string[] Header cell strings.
rows JsTableRow[] Array of rows, where each row has a .cells string array.
columnCount number Number of columns in the table.
rowCount number Number of body rows (excluding headers).

Table Methods

  • getCell(rowIndex, colIndex) (string): Returns the cell text at the given row and column index (0-based).

💻 Code Examples

1. Inspecting Tables in Document

export function activate(context) {
    context.pipeline.onBeforePdfExport((doc, env) => {
        const tables = doc.find('table');

        tables.forEach((table, index) => {
            env.log(`Table #${index + 1}: ${table.columnCount} cols, ${table.rowCount} rows`);
            env.log(`Headers: ${table.headers.join(', ')}`);

            // Read specific cell: row 0, col 1
            const cellVal = table.getCell(0, 1);
            env.log(`Cell (0, 1): ${cellVal}`);
        });
    });
}

2. Finding Custom Blocks and Headings

export function activate(context) {
    context.pipeline.onBeforePdfExport((doc, env) => {
        // Find all H1 headings
        const h1s = doc.find('h1');
        h1s.forEach(h => env.log(`Title: ${h.text} (line ${h.lineNumber})`));

        // Find all Swagger custom blocks
        const endpoints = doc.find('swagger');
        endpoints.forEach(ep => {
            env.log(`Endpoint: ${ep.args}`);
            env.log(`Description:\n${ep.text}`);
        });
    });
}

3. Finding and Replacing Variables

export function activate(context) {
    context.pipeline.onBeforePdfExport((doc, env) => {
        // Search occurrences of deprecated word
        const matches = doc.findText('DEPRECATED_PARAM');
        env.log(`Found ${matches.length} occurrences of deprecated param.`);

        // Replace throughout document
        doc.replaceText(/DEPRECATED_PARAM/g, 'NEW_PARAM');
    });
}