diff --git a/src/constants.ts b/src/constants.ts index 57673ae6..a7c2ca03 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -16,11 +16,13 @@ const images = require('@doc-tools/transform/lib/plugins/images'); const monospace = require('@doc-tools/transform/lib/plugins/monospace'); const table = require('@doc-tools/transform/lib/plugins/table'); const term = require('@doc-tools/transform/lib/plugins/term'); +const changelog = require('@doc-tools/transform/lib/plugins/changelog'); const mermaid = require('@diplodoc/mermaid-extension'); const openapi = require('@diplodoc/openapi-extension'); includes.collect = require('@doc-tools/transform/lib/plugins/includes/collect'); images.collect = require('@doc-tools/transform/lib/plugins/images/collect'); +changelog.collect = require('@doc-tools/transform/lib/plugins/changelog/collect'); export const BUILD_FOLDER = 'build'; export const BUNDLE_FOLDER = '_bundle'; @@ -86,6 +88,7 @@ export const YFM_PLUGINS = [ term, openapi.transform(), mermaid.transform(), + changelog, ]; export const PROCESSING_FINISHED = 'Processing finished:'; diff --git a/src/models.ts b/src/models.ts index 878a2e00..65b8b6f1 100644 --- a/src/models.ts +++ b/src/models.ts @@ -3,6 +3,7 @@ import {LintConfig} from '@doc-tools/transform/lib/yfmlint'; import {FileContributors, VCSConnector, VCSConnectorConfig} from './vcs-connector/connector-models'; import {Lang, Stage, IncludeMode, ResourceType} from './constants'; +import {ChangelogItem} from '@doc-tools/transform/lib/plugins/changelog/types'; export type VarsPreset = 'internal'|'external'; @@ -193,6 +194,8 @@ export interface PluginOptions { destPath?: string; destRoot?: string; collectOfPlugins?: (input: string, options: PluginOptions) => string; + changelogs?: ChangelogItem[]; + extractChangelogs?: boolean; } export interface Plugin { diff --git a/src/resolvers/md2md.ts b/src/resolvers/md2md.ts index 100d3b20..fdb7d341 100644 --- a/src/resolvers/md2md.ts +++ b/src/resolvers/md2md.ts @@ -1,5 +1,5 @@ -import {readFileSync, writeFileSync} from 'fs'; -import {dirname, resolve} from 'path'; +import {existsSync, readFileSync, writeFileSync} from 'fs'; +import {dirname, resolve, join, basename, extname} from 'path'; import shell from 'shelljs'; import log from '@doc-tools/transform/lib/log'; import liquid from '@doc-tools/transform/lib/liquid'; @@ -9,6 +9,7 @@ import {logger, getVarsPerFile} from '../utils'; import {PluginOptions, ResolveMd2MdOptions} from '../models'; import {PROCESSING_FINISHED} from '../constants'; import {getContentWithUpdatedMetadata} from '../services/metadata'; +import {ChangelogItem} from '@doc-tools/transform/lib/plugins/changelog/types'; export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise { const {inputPath, outputPath, metadata} = options; @@ -22,7 +23,7 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise vars.__system, ); - const {result} = transformMd2Md(content, { + const {result, changelogs} = transformMd2Md(content, { path: resolvedInputPath, destPath: outputPath, root: resolve(input), @@ -34,6 +35,37 @@ export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise }); writeFileSync(outputPath, result); + + if (changelogs?.length) { + const mdFilename = basename(outputPath, extname(outputPath)); + const outputDir = dirname(outputPath); + changelogs.forEach((changes, index) => { + let changesName; + const changesDate = changes.date as string | undefined; + const changesIdx = changes.index as number | undefined; + if (typeof changesIdx === 'number') { + changesName = String(changesIdx); + } + if (!changesName && changesDate && /^\d{4}/.test(changesDate)) { + changesName = Math.trunc(new Date(changesDate).getTime() / 1000); + } + if (!changesName) { + changesName = `name-${mdFilename}-${String(changelogs.length - index).padStart(3, '0')}`; + } + + const changesPath = join(outputDir, `changes-${changesName}.json`); + + if (existsSync(changesPath)) { + throw new Error(`Changelog ${changesPath} already exists!`); + } + + writeFileSync(changesPath, JSON.stringify({ + ...changes, + source: mdFilename, + })); + }); + } + logger.info(inputPath, PROCESSING_FINISHED); return undefined; @@ -84,6 +116,7 @@ function transformMd2Md(input: string, options: PluginOptions) { } = options; let output = input; + const changelogs: ChangelogItem[] = []; if (!disableLiquid) { const liquidResult = liquidMd2Md(input, vars, path); @@ -101,11 +134,14 @@ function transformMd2Md(input: string, options: PluginOptions) { log: pluginLog, copyFile: pluginCopyFile, collectOfPlugins, + changelogs, + extractChangelogs: true, }); } return { result: output, + changelogs, logs: pluginLog.get(), }; }