@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
83 lines (70 loc) • 2.75 kB
JavaScript
import fs from 'fs';
import { pageNodeFactory } from './node/page-node-factory.js';
import { createYAMLFile, readYAMLSync } from '@gouvfr/dsfr-forge';
import { HeaderInterpreter } from './resource/header-interpreter.js';
import { TitleInterpreter } from './resource/title-interpreter.js';
import { FooterInterpreter } from './resource/footer-interpreter.js';
import { NavigationInterpreter } from './resource/navigation-interpreter.js';
import { MeshInterpreter } from './content/mesh-interpreter.js';
import { parseMarkdown } from '../../../../common/markdown/parse.js';
import { fragments } from '@gouvfr/dsfr-lore';
class PageInterpreter {
constructor (state, filename) {
this._state = state;
this._filename = filename;
this._assets = [];
}
get src () {
return this._state.src;
}
async read () {
this._data = readYAMLSync(this._state.configFile(`flatplan/${this._filename}`));
const state = this._state.setData(this._data);
this._header = new HeaderInterpreter(state, this._data.resource.header);
this._footer = new FooterInterpreter(state, this._data.resource.footer);
this._navigation = new NavigationInterpreter(state, this._data.resource.navigation);
this._pageTitle = new TitleInterpreter(state, this._data);
if (this._data.mesh) {
this._mesh = new MeshInterpreter(state, this._data.mesh);
}
if (this._data.cgu.required === true) {
this._data.cgu = {...this._data.cgu, ...this._state.getStorage('cgu')};
}
const markdown = fs.readFileSync(this._data.src, 'utf8');
const nodes = parseMarkdown(markdown);
if (this._data.list !== undefined) {
nodes.push({
type: 'leafDirective',
name: 'dsfr-doc-page-item-list',
attributes: typeof this._data.list === 'object' ? this._data.list : {},
});
}
this._nodes = nodes.map(node => pageNodeFactory(node, state));
this._assets = this._data?.assets ?? [];
this._assets.push(...this._nodes.map(node => node.assets).flat());
await this._header.resolve();
await this._footer.resolve();
await this._navigation.resolve();
await this._pageTitle.resolve();
if (this._mesh) await this._mesh.read();
}
get data () {
const version = {
button: fragments.getFragment(this._data.lang, 'version.button.text'),
versions: this._state.getVersionsData(this._data.path)
};
return {
...this._data,
version: version,
mesh: this._mesh ? this._mesh.data : undefined,
nodes: this._nodes.map(node => node.data)
}
}
get assets () {
return this._assets;
}
async write () {
createYAMLFile(this._state.configFile(`pages/${this._filename}`), this.data);
}
}
export { PageInterpreter };