@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
76 lines (75 loc) • 2.99 kB
JavaScript
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital
// SPDX-License-Identifier: Apache-2.0
import { relative } from "node:path";
import { glob } from "glob";
import globule from "globule";
import { isStringWithLength } from "../support/typesValidations.js";
import { MarkdownDocFactory } from "./pages/DocusaurusDocPageFactory.js";
export const MarkdownFlatDocuments = class MarkdownFlatDocuments {
_cwd;
_logger;
_initialized = false;
_filesPattern;
_filesIgnore;
_filesMetadata;
_mode;
_contentPreprocessor;
constructor({ logger, filesPattern, filesIgnore, filesMetadata, cwd, mode, contentPreprocessor, }) {
this._mode = mode;
this._cwd = cwd;
this._filesPattern = filesPattern;
this._filesIgnore = filesIgnore;
this._filesMetadata = filesMetadata;
this._contentPreprocessor = contentPreprocessor;
this._logger = logger.namespace("doc-flat");
}
async read() {
await this._init();
const filesPaths = await this._obtainedFilesPaths();
this._logger.debug(`Found ${filesPaths.length} files in ${this._cwd} matching the pattern '${this._filesPattern}'`);
return await this._transformFilePathsToMarkdownDocuments(filesPaths);
}
async _obtainedFilesPaths() {
return await glob(this._filesPattern, {
cwd: this._cwd,
absolute: true,
// @ts-expect-error The globule types are not compatible with the glob types
ignore: {
ignored: (p) => {
return (!/\.mdx?$/.test(p.name) ||
(this._filesIgnore &&
globule.isMatch(this._filesIgnore,
// cspell:disable-next-line
relative(this._cwd, p.fullpath()))));
},
},
});
}
async _transformFilePathsToMarkdownDocuments(filesPaths) {
const files = filesPaths.map((filePath) => MarkdownDocFactory.fromPath(filePath, {
logger: this._logger,
filesMetadata: this._filesMetadata,
contentPreprocessor: this._contentPreprocessor,
}));
const pages = files.map((item) => ({
title: item.meta.confluenceTitle || item.meta.title,
id: item.meta.confluencePageId,
path: item.path,
relativePath: relative(this._cwd, item.path),
content: item.content,
ancestors: [],
name: item.meta.confluenceShortName,
}));
this._logger.debug(`Found ${pages.length} pages in ${this._cwd}`);
return pages;
}
_init() {
if (!this._initialized) {
if (!isStringWithLength(this._filesPattern)) {
throw new Error(`File pattern can't be empty in ${this._mode} mode`);
}
this._filesPattern = this._filesPattern;
this._initialized = true;
}
}
};