From dcf6dd2d44006628c963e8555d98603109785589 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 22 Feb 2025 14:57:58 -0400 Subject: data: DocumentSortingRule.apply --- src/data/things/sorting-rule.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js index 8d1c61b9..890a798b 100644 --- a/src/data/things/sorting-rule.js +++ b/src/data/things/sorting-rule.js @@ -1,9 +1,11 @@ export const SORTING_RULE_DATA_FILE = 'sorting-rules.yaml'; +import {readFile, writeFile} from 'node:fs/promises'; +import * as path from 'node:path'; + import {input} from '#composite'; import Thing from '#thing'; import {isStringNonEmpty, strictArrayOf} from '#validators'; -import {documentModes} from '#yaml'; import { compareCaseLessSensitive, @@ -12,6 +14,13 @@ import { sortByName, } from '#sort'; +import { + documentModes, + flattenThingLayoutToDocumentOrder, + getThingLayoutForFilename, + reorderDocumentsInYAMLSourceText, +} from '#yaml'; + import {flag} from '#composite/wiki-properties'; export class SortingRule extends Thing { @@ -130,7 +139,27 @@ export class DocumentSortingRule extends ThingSortingRule { }, }); - apply(layout) { + async apply({wikiData, dataPath}) { + let layout = getThingLayoutForFilename(this.filename, wikiData); + if (!layout) return; + + layout = this.#processLayout(layout); + + const order = flattenThingLayoutToDocumentOrder(layout); + + const realPath = + path.join( + dataPath, + this.filename.split(path.posix.sep).join(path.sep)); + + let sourceText = await readFile(realPath, 'utf8'); + + sourceText = reorderDocumentsInYAMLSourceText(sourceText, order); + + await writeFile(realPath, sourceText); + } + + #processLayout(layout) { const fresh = {...layout}; let sortable = null; -- cgit 1.3.0-6-gf8a5