Skip to content
Document Authoring DA  API Docs v1.10.0
npmGitHub

BlockLevel

BlockLevel = Paragraph | Table

Union type representing the block-level elements that can appear in a document.

BlockLevel is a discriminated union of Paragraph and Table, the two types of block-level elements that can exist within a document’s content structure. Block-level elements are the primary building blocks of document content, appearing within:

Use the type discriminator property to determine which specific block-level element you’re working with:

  • type: 'paragraph' - A Paragraph element
  • type: 'table' - A Table element

Block-level elements are obtained from BlockLevelContainer.blocklevels, which returns an array of BlockLevel elements that you can iterate through and process based on their type.

Iterate through and process block-level elements:

const content = section.content();
const blockLevels = content.blocklevels();
for (const blockLevel of blockLevels) {
if (blockLevel.type === 'paragraph') {
// It's a paragraph
const textView = blockLevel.asTextView();
const text = textView.getPlainText();
console.log('Paragraph:', text);
} else if (blockLevel.type === 'table') {
// It's a table
const rows = blockLevel.rows();
console.log(`Table with ${rows.length} rows`);
}
}

Count paragraphs and tables in a section:

const blockLevels = section.content().blocklevels();
let paragraphCount = 0;
let tableCount = 0;
for (const bl of blockLevels) {
if (bl.type === 'paragraph') {
paragraphCount++;
} else if (bl.type === 'table') {
tableCount++;
}
}
console.log(`Section has ${paragraphCount} paragraphs and ${tableCount} tables`);

Process only paragraphs with specific text:

const blockLevels = section.content().blocklevels();
for (const bl of blockLevels) {
if (bl.type === 'paragraph') {
const text = bl.asTextView().getPlainText();
if (text.includes('TODO')) {
// Highlight TODOs in red
bl.replaceText(/TODO/g, {
text: 'TODO',
formatting: { color: '#ff0000', bold: true }
});
}
}
}

Find the first table in a section:

const blockLevels = section.content().blocklevels();
const firstTable = blockLevels.find(bl => bl.type === 'table');
if (firstTable && firstTable.type === 'table') {
// TypeScript knows this is a Table due to type narrowing
const rows = firstTable.rows();
console.log(`First table has ${rows.length} rows`);
} else {
console.log('No tables found');
}

Extract all text from both paragraphs and tables:

const blockLevels = section.content().blocklevels();
const allText: string[] = [];
for (const bl of blockLevels) {
if (bl.type === 'paragraph') {
const text = bl.asTextView().getPlainText();
allText.push(text);
} else if (bl.type === 'table') {
// Extract text from all cells in the table
for (const row of bl.rows()) {
for (const cell of row.cells()) {
const cellBlocks = cell.blocklevels();
for (const cellBl of cellBlocks) {
if (cellBl.type === 'paragraph') {
const text = cellBl.asTextView().getPlainText();
allText.push(text);
}
}
}
}
}
}
console.log('All text:', allText.join('\n'));

Replace text across all block-level elements:

const blockLevels = section.content().blocklevels();
let totalReplacements = 0;
for (const bl of blockLevels) {
// Both Paragraph and Table have replaceText method
const count = bl.replaceText(/old/g, 'new');
totalReplacements += count;
}
console.log(`Replaced ${totalReplacements} occurrences`);