Skip to content

Commit

Permalink
feat: bump transform to 4.32.2, use transform metadata utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
brotheroftux committed Oct 1, 2024
1 parent 8b2dad1 commit a21fc3e
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 278 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@diplodoc/mermaid-extension": "^1.2.1",
"@diplodoc/openapi-extension": "^2.3.4",
"@diplodoc/prettier-config": "^2.0.0",
"@diplodoc/transform": "^4.31.3",
"@diplodoc/transform": "^4.32.2",
"@diplodoc/tsconfig": "^1.0.2",
"@octokit/core": "4.2.4",
"@types/async": "^3.2.15",
Expand Down
22 changes: 15 additions & 7 deletions src/services/metadata/addSourcePath.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import {parseExistingMetadata} from './parse';
import {emplaceMetadata} from './utils';
import {
emplaceFrontMatter,
separateAndExtractFrontMatter,
} from '@diplodoc/transform/lib/frontmatter';
import {normalizeLineEndings} from './utils';

export const addSourcePath = (fileContent: string, sourcePath: string) => {
const {metadata, metadataStrippedContent} = parseExistingMetadata(fileContent, sourcePath);

return emplaceMetadata(metadataStrippedContent, {
...metadata,
const {frontMatter, frontMatterStrippedContent} = separateAndExtractFrontMatter(
fileContent,
sourcePath,
});
);

return normalizeLineEndings(
emplaceFrontMatter(frontMatterStrippedContent, {
...frontMatter,
sourcePath,
}),
);
};
28 changes: 16 additions & 12 deletions src/services/metadata/enrich.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {MetaDataOptions, VarsMetadata} from '../../models';
import {mergeMetadata} from './mergeMetadata';
import {FileMetadata, parseExistingMetadata} from './parse';
import {emplaceMetadata} from './utils';
import {mergeFrontMatter} from './mergeMetadata';
import {FrontMatter} from '@diplodoc/transform/lib/frontmatter/common';
import {resolveVCSFrontMatter} from './vcsMetadata';
import {
emplaceFrontMatter,
separateAndExtractFrontMatter,
} from '@diplodoc/transform/lib/frontmatter';
import {normalizeLineEndings} from './utils';

type FrontMatterVars = {
metadataVars?: VarsMetadata;
Expand All @@ -15,8 +19,8 @@ type EnrichWithFrontMatterOptions = {
resolvedFrontMatterVars: FrontMatterVars;
};

const resolveVCSPath = (metadata: FileMetadata, relativeInputPath: string) => {
const maybePreProcessedSourcePath = metadata.sourcePath;
const resolveVCSPath = (frontMatter: FrontMatter, relativeInputPath: string) => {
const maybePreProcessedSourcePath = frontMatter.sourcePath;

return typeof maybePreProcessedSourcePath === 'string' && maybePreProcessedSourcePath.length > 0
? maybePreProcessedSourcePath
Expand All @@ -31,29 +35,29 @@ export const enrichWithFrontMatter = async ({
const {systemVars, metadataVars} = resolvedFrontMatterVars;
const {resources, addSystemMeta, shouldAlwaysAddVCSPath, pathData} = metadataOptions;

const {metadata, metadataStrippedContent} = parseExistingMetadata(
const {frontMatter, frontMatterStrippedContent} = separateAndExtractFrontMatter(
fileContent,
pathData.pathToFile,
);

const vcsFrontMatter = metadataOptions.isContributorsEnabled
? await resolveVCSFrontMatter(metadata, metadataOptions, fileContent)
? await resolveVCSFrontMatter(frontMatter, metadataOptions, fileContent)
: undefined;

const mergedMetadata = mergeMetadata({
existingMetadata: metadata,
const mergedFrontMatter = mergeFrontMatter({
existingMetadata: frontMatter,
resources,
metadataVars,
systemVars: addSystemMeta ? systemVars : undefined,
additionalMetadata: {
vcsPath:
metadata.vcsPath ??
frontMatter.vcsPath ??
(shouldAlwaysAddVCSPath
? resolveVCSPath(metadata, pathData.pathToFile)
? resolveVCSPath(frontMatter, pathData.pathToFile)
: undefined),
...vcsFrontMatter,
},
});

return emplaceMetadata(metadataStrippedContent, mergedMetadata);
return normalizeLineEndings(emplaceFrontMatter(frontMatterStrippedContent, mergedFrontMatter));
};
12 changes: 6 additions & 6 deletions src/services/metadata/mergeMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {Resources, VarsMetadata} from '../../models';
import {isObject} from '../utils';
import {FileMetadata} from './parse';
import {FrontMatter} from '@diplodoc/transform/lib/frontmatter/common';

export const mergeMetadata = ({
export const mergeFrontMatter = ({
existingMetadata,
resources,
systemVars,
metadataVars = [],
additionalMetadata,
}: {
existingMetadata: FileMetadata;
existingMetadata: FrontMatter;
metadataVars?: VarsMetadata;
resources?: Resources;
systemVars?: unknown;
additionalMetadata?: Record<string, unknown>;
}): FileMetadata => {
const mergedInnerMetadata: FileMetadata['metadata'] = [
}): FrontMatter => {
const mergedInnerMetadata: FrontMatter['metadata'] = [
...(existingMetadata.metadata ?? []),
...metadataVars,
];
Expand All @@ -28,7 +28,7 @@ export const mergeMetadata = ({
const innerMetadataToSpread =
mergedInnerMetadata.length > 0 ? {metadata: mergedInnerMetadata} : undefined;

const mergedMetadata: FileMetadata = {
const mergedMetadata: FrontMatter = {
...existingMetadata,
...resources,
...systemVarsMetadataToSpread,
Expand Down
118 changes: 0 additions & 118 deletions src/services/metadata/parse.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/services/metadata/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {carriageReturn} from '../../utils';
import {FileMetadata, serializeMetadata} from './parse';

// IMO, we should just always apply this at the end of the whole processing pipeline,
// not when dumping meta/front matter
export const normalizeLineEndings = (input: string): string =>
input.replace(/\r?\n/g, carriageReturn);

export const emplaceMetadata = (metadataStrippedContent: string, metadata: FileMetadata) =>
normalizeLineEndings(`${serializeMetadata(metadata)}${metadataStrippedContent}`);
4 changes: 2 additions & 2 deletions src/services/metadata/vcsMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../authors';
import {ContributorsServiceFileData, getFileContributors, getFileIncludes} from '../contributors';
import {isObject} from '../utils';
import {FileMetadata} from './parse';
import {FrontMatter} from '@diplodoc/transform/lib/frontmatter/common';

const getFileDataForContributorsService = (
pathData: PathData,
Expand Down Expand Up @@ -103,7 +103,7 @@ export const getVCSMetadata = async (
};

export const resolveVCSFrontMatter = async (
existingMetadata: FileMetadata,
existingMetadata: FrontMatter,
options: MetaDataOptions,
fileContent: string,
) => {
Expand Down
Loading

0 comments on commit a21fc3e

Please sign in to comment.