UNPKG

@telefonica/markdown-confluence-sync

Version:

Creates/updates/deletes Confluence pages based on markdown files in a directory. Supports Mermaid diagrams and per-page configuration using frontmatter metadata. Works great with Docusaurus

51 lines (50 loc) 2.08 kB
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital // SPDX-License-Identifier: Apache-2.0 import { existsSync, lstatSync } from "fs"; import { readdir } from "fs/promises"; import { join, relative } from "node:path"; import globule from "globule"; import { DocusaurusDocItemFactory } from "./DocusaurusDocItemFactory.js"; export const DocusaurusDocTree = class DocusaurusDocTree { _path; _cwd; _logger; _filesMetadata; _filesIgnore; _contentPreprocessor; constructor(path, options) { if (!existsSync(path)) { throw new Error(`Path ${path} does not exist`); } this._path = path; this._cwd = options.cwd; this._logger = options.logger; this._filesMetadata = options.filesMetadata; this._filesIgnore = options.filesIgnore; this._contentPreprocessor = options.contentPreprocessor; } async flatten() { const rootPaths = await readdir(this._path); if (rootPaths.some((path) => path.endsWith("index.md"))) { this._logger?.warn("Ignoring index.md file in root directory."); } const rootDirs = rootPaths .map((path) => join(this._path, path)) .filter((path) => { const isDirectoryOrValidFile = lstatSync(path).isDirectory() || (path.endsWith(".md") && !path.endsWith("index.md")); const shouldBeIgnored = this._filesIgnore && globule.isMatch(this._filesIgnore, relative(this._cwd, path)); return isDirectoryOrValidFile && !shouldBeIgnored; }); const roots = rootDirs.map((path) => DocusaurusDocItemFactory.fromPath(path, { cwd: this._cwd, logger: this._logger?.namespace(path.replace(this._path, "")), filesMetadata: this._filesMetadata, contentPreprocessor: this._contentPreprocessor, filesIgnore: this._filesIgnore, })); const rootsPages = await Promise.all(roots.map((root) => root.visit())); return rootsPages.flat(); } };