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:324

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:325


tree

tree: LibraryNode

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

Methods

buildContentBlockConversionXML()

buildContentBlockConversionXML(contentId, displayName): Promise<string>

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

Build a self-contained library XML payload that converts a component into a content block (fragment) when imported via site-archive import.

A plain merge import applies the component.* -> fragment.* type change but drops the element's own region content-links (orphaning a Layout block's children — verified live). Meanwhile a mode="delete" on a content element deep-deletes its entire subtree and strips every incoming content-link. To faithfully reproduce Page Designer's in-place conversion (verified byte-for-byte against a manual conversion), this archive therefore:

  1. deletes the target (<content mode="delete"/>) — clearing the old type and its subtree;
  2. recreates the target as fragment.* (display-name first, own region-link types flipped) — see transformContentXmlToFragment;
  3. recreates every descendant of the target (they were cascade-deleted);
  4. re-imports every referrer (any content that links the target, possibly several — content blocks are shared) so their content-link survives.

All four happen in one archive/one import job so the storefront is never left with a dangling reference.

Parameters

contentId

string

The component content-id to convert.

displayName

string

The x-default display name for the new content block.

Returns

Promise<string>

Importable library XML string.

Throws

If the content-id is not found or is not a component.


filter()

filter(predicate, options): this

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

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:436

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:696

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:576

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:627

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:646

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:755

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:664

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:543

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:346

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.