UNPKG

@gouvfr/dsfr-nexus

Version:

Le module `dsfr-nexus` est l'interface de ligne de commande (CLI) centrale du Système de Design de l’État - DSFR. Il offre des outils pour gérer et compiler les ressources du DSFR

129 lines (103 loc) 3.97 kB
import fs from 'fs'; import yaml from 'yaml'; import { DirectiveNode } from './directive-node.js'; /** * List of fields that are considered as assets. * @type {string[]} */ const ASSET_FIELDS = []; const DEFAULT_FIELDS = ['cover', 'shortDescription']; /** * @class PageItemListLeafDirectiveNode * @extends DirectiveNode * * @attributes { * path (string) - The path to resolve items, by default the current path, * depth (number) - The depth to which to collect children pages, * rank (number|null) - The rank to filter items by (null for all, part for children parts), * fields (string) - Comma-separated fields to include in the item data } */ class PageItemListLeafDirectiveNode extends DirectiveNode { constructor (data, state) { super(data, state); this._assets = []; this._depth = this.getNumberProperty('depth') || 1; const lengths = this.getArrayProperty('length', 'number', this._depth); const names = this.getArrayProperty('name', 'string', this._depth); const horizontals = this.getArrayProperty('horizontal', 'boolean', this._depth); const noCovers = this.getArrayProperty('noCover', 'boolean', this._depth); const path = this.getStringProperty('path') || state.path; const fields = this.getArrayProperty('fields', 'string') ?? DEFAULT_FIELDS; const rank = this.getNumberProperty('rank') || null; const sort = this.getStringProperty('sort') || null; this._items = this.getItems(path, rank, sort, fields, this._depth - 1, lengths, names, horizontals, noCovers); } getItems (path, rank = null, sort = null, fields = [], recursive = 0, lengths = [], names = [], horizontals = [], noCovers = []) { const itemsData = this._state.resolveItems(path, rank, sort); if (!itemsData || itemsData.length === 0) return []; if (lengths.length > 0) { const length = lengths[0]; if (itemsData.length > length) { itemsData.length = length; } } if (names.length > 0) { const name = names[0]; itemsData.forEach(item => item.name = name); } if (horizontals.length > 0) { const horizontal = horizontals[0]; itemsData.forEach(item => item.horizontal = horizontal); } if (noCovers.length > 0) { const noCover = noCovers[0]; itemsData.forEach(item => item.noCover = noCover); } return itemsData.map((data) => this.getItem(data, rank, sort, fields, recursive, lengths.slice(1), names.slice(1), horizontals.slice(1), noCovers.slice(1))) || []; } getItem (data, rank = null, sort = null, fields = [], recursive = 0, lengths = [], names = [], horizontals = [], noCovers = []) { const item = { url: data.url, text: data.text, isCurrent: data.isCurrent === true, }; if (data.name) item.name = data.name; if (data.horizontal) item.horizontal = data.horizontal; if (data.noCover) item.noCover = data.noCover; if (fields.length) { const ymlFile = fs.readFileSync(data.config, 'utf8'); const yml = yaml.parse(ymlFile); for (const field of fields) { const value = yml[field]; switch (true) { case value === undefined: break; case ASSET_FIELDS.includes(field): item[field] = this.addAsset(value, { src: yml.src, deploy: yml.deploy, path: yml.path} ); break; default: item[field] = value; } } } if (recursive > 0) { item.items = this.getItems(data.path, rank, sort, fields, recursive - 1, lengths, names, horizontals, noCovers); } return item; } get items () { return this._items; } get depth () { return this._depth; } get data () { return { type: this.type, items: this.items, depth: this.depth, name: PageItemListLeafDirectiveNode.NAME, }; } } PageItemListLeafDirectiveNode.NAME = 'dsfr-doc-page-item-list'; export { PageItemListLeafDirectiveNode };