@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
61 lines (60 loc) • 2.47 kB
JavaScript
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital
// SPDX-License-Identifier: Apache-2.0
import { join, basename, dirname, relative, sep } from "node:path";
import { DocusaurusDocTree } from "./tree/DocusaurusDocTree.js";
import { buildIndexFileRegExp, getIndexFileFromPaths } from "./util/files.js";
export const DocusaurusTreePages = class DocusaurusTreePages {
_path;
_tree;
_logger;
_filesMetadata;
_filesIgnore;
_cwd;
_contentPreprocessor;
constructor({ logger, path, filesMetadata, filesIgnore, contentPreprocessor, cwd, }) {
this._path = path;
this._logger = logger;
this._filesMetadata = filesMetadata;
this._filesIgnore = filesIgnore;
this._contentPreprocessor = contentPreprocessor;
this._cwd = cwd;
this._tree = new DocusaurusDocTree(this._path, {
cwd: this._cwd,
logger: this._logger.namespace("doc-tree"),
filesMetadata: this._filesMetadata,
filesIgnore: this._filesIgnore,
contentPreprocessor: this._contentPreprocessor,
});
}
async read() {
const items = await this._tree.flatten();
const pages = items.map((item) => ({
title: item.meta.confluenceTitle || item.meta.title,
path: item.path,
relativePath: relative(this._path, item.path),
content: item.content,
ancestors: [],
name: item.meta.confluenceShortName,
}));
this._logger.debug(`Found ${pages.length} pages in ${this._path}`);
const pagePaths = pages.map(({ path }) => path);
return pages.map((page) => ({
...page,
ancestors: this._getItemAncestors(page, pagePaths),
}));
}
_getItemAncestors(page, paths) {
// HACK: Added filter to removed empty string because windows separator
// add double slash and this cause empty string in the end of array
const dirnamePath = basename(dirname(page.path));
const idSegments = relative(this._path, page.path)
.replace(buildIndexFileRegExp(sep, dirnamePath), "")
.split(sep)
.filter((value) => value !== "");
if (idSegments.length === 1)
return [];
return idSegments
.slice(0, -1)
.map((_idSegment, index) => getIndexFileFromPaths(join(this._path, ...idSegments.slice(0, index + 1)), paths));
}
};