
@craftercms/content
This package contains services for retrieving content and navigation using APIs offered by Crafter CMS.
Usage
All of Crafter CMS packages can be used either via npm or in plain html/javascript via regular script imports.
Via npm
- Install module using
yarn
or npm
- Yarn:
yarn add @craftercms/content
- npm:
npm install @craftercms/content
- Import and use the service(s) you need
Via html script imports
- Download the bundle and import them in your page.
- The bundle declare a global variable named
craftercms
. You can access all craftercms' packages and functions under this root. - The
content
package depends on rxjs
, @craftercms/utils
, @craftercms/classes
; make sure to import those too before the content
script.
Tip: Once you've imported the scripts, type craftercms
on your browser's dev tools console to inspect the package(s)
Vanilla html/js example
<div id="myFeature"></div>
<script src="https://unpkg.com/rxjs"></script>
<script src="https://unpkg.com/@craftercms/utils"></script>
<script src="https://unpkg.com/@craftercms/classes"></script>
<script src="https://unpkg.com/@craftercms/content"></script>
<script>
(function ({ content }, { operators }) {
content.getItem(
'/site/website/index.xml',
{ baseUrl: 'http://localhost:8080', site: 'editorial' }
).pipe(
operators.map(content.parseDescriptor)
).subscribe((model) => {
const elem = document.querySelector('#myFeature');
Object.entries(model).forEach(([fieldId, value]) => {
if (fieldId !== 'craftercms') {
const el = document.createElement('div');
el.innerText = `${fieldId}: ${value}`;
elem.appendChild(el);
}
});
});
})(craftercms, rxjs);
</script>
Service pre-configuration
You may pre-configure content services to a certain configuration to then you may omit the config param on subsequent calls to services
import { Item } from '@craftercms/models';
import { getItem } from '@craftercms/content';
import { crafterConf } from '@craftercms/classes';
crafterConf.configure({
baseUrl: 'http://authoring.company.com',
site: 'editorial'
});
getItem('/site/website/index.xml').subscribe((item: Item) => {
console.log(item);
});
Package Index
The examples below assume usage in the style of using via npm. If you're using the bundles,
directly importing as a script in the browser, these functions will be under the global variable
named craftercms.content
(i.e. window.craftercms.content
).
parseDescriptor
Parse a Descriptor, Item or a GraphQL response into a Content Instance. It could also be a collection of any of these types.
parseDescriptor(response: Descriptor | Item | GraphQLResponse | Descriptor[] | Item[] | GraphQLResponse)
Parameters | |
---|
response | The response of a getItem, getDescriptor or GraphQL fetch call |
Returns
ContentInstance
Examples
- If you want a cleaner/parsed response, you may use
parseDescriptor
util to parse the response for you. You may use it to parse getItem, getDescriptor or GraphQL responses.
import { map } from 'rxjs/operators';
import { ContentInstance } from '@craftercms/models';
import { getChildren, getItem, parseDescriptor } from '@craftercms/content';
getItem('/site/website/index.xml', { site: 'editorial' }).pipe(
map(parseDescriptor)
).subscribe((content: ContentInstance) => {
console.log(content);
});
getChildren('/site/website', { site: 'editorial' }).pipe(
map(parseDescriptor)
).subscribe((content: ContentInstance[]) => {
console.log(content);
});
preParseSearchResults
Inspects and parses elasticsearch hits and pre-parses objects before they can be sent to parseDescriptor
@see https://github.com/craftercms/craftercms/issues/4057
import { createQuery, search } from '@craftercms/search';
import { parseDescriptor, preParseSearchResults } from '@craftercms/content';
search(
createQuery({
query: {
bool: {
filter: []
}
}
}),
{ baseUrl: 'http://localhost:8080', site: 'editorial' }
).pipe(
map(({ hits, ...rest }) => ({
...rest,
hits: hits.map(({ _source }) => parseDescriptor(
preParseSearchResults(_source)
))
}))
).subscribe((results) => {
});
Get Item
Get an Item from the content store.
getItem(path: string, config?: CrafterConfig)
Parameters | |
---|
path | The item’s path in the content store |
config | Crafter configuration. Optional. Default value in here. |
Returns
Item - from the content store
Examples
- Get the index page from the site:
import { Item } from '@craftercms/models';
import { getItem } from '@craftercms/content';
getItem('/site/website/index.xml', { site: 'editorial' }).subscribe((item: Item) => {
console.log(item);
});
- If you want a cleaner/parsed response, you may use
parseDescriptor
util to parse the response for you.
import { map } from 'rxjs/operators';
import { ContentInstance } from '@craftercms/models';
import { getItem, parseDescriptor } from '@craftercms/content';
getItem('/site/website/index.xml', { site: 'editorial' }).pipe(
map(parseDescriptor)
).subscribe((content: ContentInstance) => {
console.log(content);
});
Get Descriptor
Get the descriptor data of an Item in the content store.
getDescriptor(path: string, config?: CrafterConfig)
Parameters | |
---|
path | The item’s path in the content store |
config | Crafter configuration. Optional. Default value in here. |
Returns
Descriptor - from the content store
Examples
- Get the index page from the site:
import { map } from 'rxjs/operators';
import { getDescriptor, parseDescriptor } from '@craftercms/content';
import { Descriptor, ContentInstance } from '@craftercms/models';
getDescriptor('/site/website/index.xml', { site: 'editorial' }).subscribe((descriptor: Descriptor) => {
console.log(descriptor);
});
getDescriptor('/site/website/index.xml').pipe(
map(parseDescriptor)
).subscribe((content: ContentInstance) => {
console.log(content);
});
Get Children
Get the list of Items directly under a folder in the content store.
getChildren(path: string, config?: CrafterConfig)
Parameters | |
---|
path | The folder’s path |
config | Crafter configuration. Optional. Default value in here. |
Returns
Item[] - List of Items from the content store
Examples
- Get the children items under root folder from the site:
import { getChildren } from '@craftercms/content';
getChildren('/site/website', { site: 'editorial' }).subscribe((children) => {
console.log(children);
});
getChildren('/site/website').subscribe((children) => {
console.log(children);
});
Get Tree
Get the complete Item hierarchy under the specified folder in the content store.
getTree(path: string, depth: number, config: CrafterConfig)
Parameters | |
---|
path | The folder’s path |
depth | Amount of levels to include. Optional. Default is 1 |
config | Crafter configuration. Optional. Default value in here. |
Returns
Item - from the content store
Examples
- Get the items tree under root folder from the site:
import { getTree } from '@craftercms/content';
getTree('/site/website', 3, { site: 'editorial' }).subscribe((tree) => {
console.log(tree);
});
getTree('/site/website', 3).subscribe((tree) => {
console.log(tree);
});
Get Navigation Tree
Returns the navigation tree with the specified depth for the specified store URL.
getNavTree(path: string, depth: number, currentPageUrl: string, config: CrafterConfig)
Parameters | |
---|
path | The folder’s path |
depth | Amount of levels to include. Optional. Default is 1 |
currentPageUrl | The URL of the current page. Optional. Default is '' |
config | Crafter configuration. Optional. Default value in here. |
Returns
NavigationItem - from the content store
Examples
- Get the navigation tree of the root folder from the site (depth = 3):
import { getTree } from '@craftercms/content';
getTree('/site/website', 3, { site: 'editorial' }).subscribe((tree) => {
console.log(tree);
});
getTree('/site/website', 3).subscribe((tree) => {
console.log(tree);
});
Get Navigation Breadcrumb
Returns the navigation items that form the breadcrumb for the specified store URL.
getNavBreadcrumb(path: string, root: string, config: CrafterConfig)
Parameters | |
---|
path | The folder’s path |
root | the root URL, basically the starting point of the breadcrumb. Optional. Default is '' |
config | Crafter configuration. Optional. Default value in here. |
Returns
NavigationItem[] - List of NavigationItem from the content store
Examples
- Get the breadcrumb for the root folder from the site:
import { getNavBreadcrumb } from '@craftercms/content';
getNavBreadcrumb('/site/website').subscribe((navBreadcrumb) => {
console.log(navBreadcrumb);
});
getNavBreadcrumb('/site/website').subscribe((navBreadcrumb) => {
console.log(navBreadcrumb);
});
Transform
Transforms a URL, based on the current site’s configuration.
transform(transformerName: string, path: string, config: CrafterConfig)
Parameters | |
---|
transformerName | Name of the transformer to apply |
path | URL that will be transformed |
config | Crafter configuration. Optional. Default value in here. |
Returns
string - URL transformed according to transformer applied.
Examples
- Transform a store path into a render path
import { urlTransform } from '@craftercms/content';
urlTransform('storeUrlToRenderUrl', '/site/website/style/index.xml', { site: 'editorial' }).subscribe((webUrl) => {
console.log(webUrl);
});
urlTransform('storeUrlToRenderUrl', '/site/website/style/index.xml').subscribe((webUrl) => {
console.log(webUrl);
});
- Transform a render path into a store path
import { transform } from '@craftercms/content';
transform('renderUrlToStoreUrl', '/technology', { site: 'editorial' }).subscribe((path) => {
console.log(path);
});
transform('renderUrlToStoreUrl', '/technology').subscribe((path) => {
console.log(path);
})