Skip to content

Commit

Permalink
feat: support metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
v8tenko committed Jan 17, 2024
1 parent fcdb26a commit 3bece4c
Show file tree
Hide file tree
Showing 38 changed files with 669 additions and 117 deletions.
6 changes: 0 additions & 6 deletions .eslintrc

This file was deleted.

11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
"extends": ["@diplodoc/eslint-config"],
"root": true,
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
"env": {
"node": true
}
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [4.9.0](https://github.com/diplodoc-platform/cli/compare/v4.8.0...v4.9.0) (2024-01-09)


### Features

* Add updatedAt field in metadata ([#628](https://github.com/diplodoc-platform/cli/issues/628)) ([63f6f5e](https://github.com/diplodoc-platform/cli/commit/63f6f5e1300e4a233e1c547860252562b1fe9772))


### Bug Fixes

* Update latex extension ([0cd0a5d](https://github.com/diplodoc-platform/cli/commit/0cd0a5dded9b632be0b4a1c097ad0efaf8859d1c))

## [4.8.0](https://github.com/diplodoc-platform/cli/compare/v4.7.0...v4.8.0) (2023-12-28)


Expand Down
38 changes: 32 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Yandex Data UI Team <[email protected]>",
"description": "Make documentation using yfm-docs in Markdown and HTML formats",
"license": "MIT",
"version": "4.8.0",
"version": "4.9.0",
"repository": {
"type": "git",
"url": "[email protected]:diplodoc-platform/cli.git"
Expand Down Expand Up @@ -32,7 +32,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.369.0",
"@diplodoc/client": "^2.0.0",
"@diplodoc/latex-extension": "^1.0.2",
"@diplodoc/latex-extension": "^1.0.3",
"@diplodoc/markdown-translation": "^1.0.4",
"@diplodoc/mermaid-extension": "^1.2.1",
"@diplodoc/openapi-extension": "^1.4.10",
Expand Down
11 changes: 8 additions & 3 deletions src/cmd/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,13 @@ async function handler(args: Arguments<any>) {
addMapFile,
allowCustomResources,
resources,
metadata,
} = ArgvService.getConfig();

if (typeof metadata !== 'undefined' && !Array.isArray(metadata)) {
ArgvService.patch('metadata', [metadata]);
}

preparingTemporaryFolders(userOutputFolder);

await processServiceFiles();
Expand Down Expand Up @@ -242,9 +247,9 @@ async function handler(args: Arguments<any>) {
// collect paths of all resources
Object.keys(resources).forEach(
(type) =>
resources[type as keyof Resources]?.forEach((path: string) =>
resourcePaths.push(path),
),
resources[type as keyof Resources]?.forEach((path) => {
resourcePaths.push(path);
}),
);

//copy resources
Expand Down
4 changes: 3 additions & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type NestedContributorsForPathFunction = (
) => void;
export type UserByLoginFunction = (login: string) => Promise<Contributor | null>;
export type CollectionOfPluginsFunction = (output: string, options: PluginOptions) => string;
export type GetModifiedTimeByPathFunction = (filepath: string) => number | undefined;

interface YfmConfig {
varsPreset: VarsPreset;
Expand All @@ -40,6 +41,7 @@ interface YfmConfig {
buildDisabled: boolean;
lintConfig: LintConfig;
resources?: Resources;
metadata?: Record<string, string>[];
yandexCloudTranslateFolderId: string;
yandexCloudTranslateGlossaryPairs: YandexCloudTranslateGlossaryPair[];
}
Expand Down Expand Up @@ -240,7 +242,7 @@ export interface PathData {
}

export type Resources = {
[key in ResourceType]?: string[];
[type in ResourceType]?: string[];
};

export type YandexCloudTranslateGlossaryPair = {
Expand Down
8 changes: 7 additions & 1 deletion src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<ResolveM
pathToDir === tocBase ? '' : pathToDir.replace(`${tocBase}${sep}`, '');
const relativePathToIndex = relative(pathToDir, `${tocBase}${sep}`);

const {input, lang, allowCustomResources} = ArgvService.getConfig();
const {input, lang, allowCustomResources, metadata: yfmMetadata = []} = ArgvService.getConfig();
const resolvedPath: string = resolve(input, inputPath);
const content: string = readFileSync(resolvedPath, 'utf8');

Expand All @@ -61,6 +61,12 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<ResolveM

const fileMeta = fileExtension === '.yaml' ? result.data.meta ?? {} : updatedMetadata;

if (Array.isArray(fileMeta?.metadata)) {
fileMeta.metadata.push(...yfmMetadata);
} else {
fileMeta.metadata = yfmMetadata;
}

if (allowCustomResources) {
const {script, style} = metadata?.resources || {};
fileMeta.style = (fileMeta.style || []).concat(style || []).map(fixRelativePath(inputPath));
Expand Down
3 changes: 2 additions & 1 deletion src/resolvers/md2md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {ChangelogItem} from '@diplodoc/transform/lib/plugins/changelog/types';

export async function resolveMd2Md(options: ResolveMd2MdOptions): Promise<void> {
const {inputPath, outputPath, metadata} = options;
const {input, output} = ArgvService.getConfig();
const {input, output, metadata: yfmMetadata} = ArgvService.getConfig();
const resolvedInputPath = resolve(input, inputPath);
const vars = getVarsPerFile(inputPath);

const content = await getContentWithUpdatedMetadata(
readFileSync(resolvedInputPath, 'utf8'),
metadata,
vars.__system,
yfmMetadata,
);

const {result, changelogs} = transformMd2Md(content, {
Expand Down
5 changes: 5 additions & 0 deletions src/services/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ function set(argv: YfmArgv) {
_argv = argv;
}

function patch<Key extends keyof YfmArgv>(field: Key, value: YfmArgv[Key]) {
_argv[field] = value;
}

export default {
getConfig,
init,
set,
patch,
};
48 changes: 46 additions & 2 deletions src/services/contributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ async function getContributorsForNestedFiles(
return Object.assign({}, ...includesContributors);
}

function getRelativeIncludeFilePaths(fileData: FileData, includeContents: string[]): Set<string> {
function getRelativeIncludeFilePaths(
fileData: Pick<FileData, 'tmpInputFilePath'>,
includeContents: string[],
): Set<string> {
const {tmpInputFilePath} = fileData;
const relativeIncludeFilePaths: Set<string> = new Set();

Expand All @@ -115,4 +118,45 @@ function getRelativeIncludeFilePaths(fileData: FileData, includeContents: string
return relativeIncludeFilePaths;
}

export {getFileContributorsMetadata, getFileContributorsString};
async function getFileIncludes(
fileData: Pick<FileData, 'fileContent' | 'tmpInputFilePath' | 'inputFolderPathLength'>,
) {
const {fileContent, tmpInputFilePath, inputFolderPathLength} = fileData;

const results = new Set<string>();

const includeContents = fileContent.match(REGEXP_INCLUDE_CONTENTS);
if (!includeContents || includeContents.length === 0) {
return [];
}
const relativeIncludeFilePaths: Set<string> = getRelativeIncludeFilePaths(
{tmpInputFilePath},
includeContents,
);
for (const relativeIncludeFilePath of relativeIncludeFilePaths.values()) {
const relativeFilePath = relativeIncludeFilePath.substring(inputFolderPathLength + 1);
if (results.has(relativeFilePath)) continue;
results.add(relativeFilePath);

let contentIncludeFile: string;
try {
contentIncludeFile = await readFile(relativeIncludeFilePath, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
continue;
}
throw err;
}

const includedPaths = await getFileIncludes({
inputFolderPathLength,
fileContent: contentIncludeFile,
tmpInputFilePath: relativeIncludeFilePath,
});
includedPaths.forEach((path) => results.add(path));
}

return Array.from(results.values());
}

export {getFileContributorsMetadata, getFileContributorsString, getFileIncludes};
Loading

0 comments on commit 3bece4c

Please sign in to comment.