Skip to content
View as Markdown
View as Markdown

@salesforce/b2c-tooling-sdk / operations/content / Library

Class: Library

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:208

Provides an interface for manipulating B2C Commerce content libraries.

Use the static Library.parse() factory to create instances from library XML. The library tree supports filtering, traversal, mutation, and serialization back to importable XML.

Example

typescript
const library = await Library.parse(xmlString);

// Filter to specific pages
library.filter(node => node.id === 'homepage');

// Traverse visible nodes
library.traverse(node => {
  console.log(node.id, node.type);
});

// Serialize back to XML
const xml = await library.toXMLString({ traverseHidden: false });

Properties

assetQuery

assetQuery: string[] = []

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:209


tree

tree: LibraryNode

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:210

Methods

filter()

filter(predicate, options): this

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:407

Filter the library tree by setting hidden flags on root children.

Parameters

predicate

FilterCallback

Return true to keep the node visible

options

Filter options

recursive?

boolean = false

Returns

this

this (for chaining)


getContentBlocks()

getContentBlocks(): LibraryNode[]

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:320

Returns the library's content blocks (fragments) as a deduplicated catalog.

A content block is a <content> element typed fragment.*. Unlike pages or content assets, fragments are not root-level tree children — they surface wherever a page/component/other-fragment links them. This method scans the full content set (not just the linked tree) so that unlinked blocks are included too, and returns one source-of-truth LibraryNode per block, with its child subtree attached (Layout fragments keep their region children).

Returns

LibraryNode[]

One LibraryNode per content block, deduplicated by content-id.

Example

typescript
const library = await Library.parse(xml);
for (const block of library.getContentBlocks()) {
  console.log(block.displayName ?? block.id, block.typeId);
}

getTreeString()

getTreeString(options): string

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:502

Returns a text tree visualization of the library structure.

Parameters

options

TreeStringOptions = {}

Tree string options (traversal and optional colorize function)

Returns

string

Multi-line tree string


nodes()

nodes(options): Generator<LibraryNode>

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:382

Generator-based depth-first iteration of the library tree.

Parameters

options

TraverseOptions = {}

Traversal options

Returns

Generator<LibraryNode>

Yields

LibraryNode for each visited node

Example

typescript
for (const node of library.nodes()) {
  console.log(node.id, node.type);
}

promoteToRoot()

promoteToRoot(node): this

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:433

Move a node from its current parent to the root of the library tree.

Useful for exporting individual components that are normally nested under pages. The node is removed from its original parent's children and added as a root child.

Parameters

node

LibraryNode

The node to promote

Returns

this

this (for chaining)


reset()

reset(): this

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:452

Reset all hidden flags, making every node visible again.

Returns

this

this (for chaining)


toJSON()

toJSON(): Record<string, unknown>

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:561

Returns a JSON-safe representation of the library tree.

Returns

Record<string, unknown>


toXMLString()

toXMLString(options): Promise<string>

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:470

Serialize the library back to importable XML.

Only includes visible content unless traverseHidden is true.

Parameters

options

Serialization options

traverseHidden?

boolean = true

Returns

Promise<string>

XML string


traverse()

traverse(callback, options): this

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:349

Depth-first traversal of the library tree.

Parameters

callback

TraverseCallback

Function called for each visited node

options

TraverseOptions = {}

Traversal options

Returns

this

this (for chaining)


parse()

static parse(libraryXML, options): Promise<Library>

Defined in: packages/b2c-tooling-sdk/src/operations/content/library.ts:230

Parse library XML into a Library tree.

Parameters

libraryXML

string

Raw XML string of a content library

options

LibraryParseOptions = {}

Parse options

Returns

Promise<Library>

Parsed Library instance

Released under the Apache-2.0 License.