Skip to content

Commit

Permalink
feat: allow load custom resources (#184)
Browse files Browse the repository at this point in the history
* feat: allow load custom resources

* tests for loading custom resources

* misc refactor

* generate relative paths only for html mode

* fix: md2md add meta only core resource path

* chore(rebase): change args source
  • Loading branch information
martyanovandrey authored Oct 27, 2022
1 parent 7f4e908 commit 80c6624
Show file tree
Hide file tree
Showing 55 changed files with 754 additions and 38 deletions.
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const REDIRECTS_FILENAME = 'redirects.yaml';
export const LINT_CONFIG_FILENAME = '.yfmlint';
export const SINGLE_PAGE_FILENAME = 'single-page.html';
export const SINGLE_PAGE_DATA_FILENAME = 'single-page.json';
export const CUSTOM_STYLE = 'custom-style';

export enum Stage {
NEW = 'new',
Expand All @@ -55,6 +56,11 @@ export enum IncludeMode {
LINK = 'link'
}

export enum ResourceType {
style = 'style',
script = 'script',
}

export const BUILD_FOLDER_PATH = dirname(process.mainModule?.filename || '');

export const YFM_PLUGINS = [
Expand Down
20 changes: 20 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
import {ArgvService, Includers} from './services';
import {argvValidator} from './validator';
import {prepareMapFile} from './steps/processMapFile';
import {copyFiles} from './utils';
import {Resources} from './models';

console.time(MAIN_TIMER_ID);

Expand Down Expand Up @@ -136,6 +138,11 @@ yargs
describe: 'Disable building',
type: 'boolean',
})
.option('allow-custom-resources', {
default: false,
describe: 'Allow loading custom resources',
type: 'boolean',
})
.check(argvValidator)
.example('yfm -i ./input -o ./output', '')
.demandOption(['input', 'output'], 'Please provide input and output arguments to work with this tool')
Expand Down Expand Up @@ -168,6 +175,8 @@ async function main(args: Arguments<any>) {
lintDisabled,
buildDisabled,
addMapFile,
allowCustomResources,
resources,
} = ArgvService.getConfig();

preparingTemporaryFolders(args, userOutputFolder, tmpInputFolder, tmpOutputFolder);
Expand Down Expand Up @@ -207,6 +216,17 @@ async function main(args: Arguments<any>) {
shell.cp(resolve(pathToRedirects), tmpOutputFolder);
shell.cp(resolve(pathToLintConfig), tmpOutputFolder);

if (resources && allowCustomResources) {
const resourcePaths: string[] = [];

// collect paths of all resources
Object.keys(resources).forEach((type) =>
resources[type as keyof Resources]?.forEach((path: string) => resourcePaths.push(path)));

//copy resources
copyFiles(args.input, tmpOutputFolder, resourcePaths);
}

break;
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Logger} from '@doc-tools/transform/lib/log';
import {LintConfig} from '@doc-tools/transform/lib/yfmlint';

import {FileContributors, VCSConnector, VCSConnectorConfig} from './vcs-connector/connector-models';
import {Lang, Stage, IncludeMode} from './constants';
import {Lang, Stage, IncludeMode, ResourceType} from './constants';

export type VarsPreset = 'internal'|'external';

Expand Down Expand Up @@ -33,6 +33,7 @@ interface YfmConfig {
lintDisabled: boolean;
buildDisabled: boolean;
lintConfig: LintConfig;
resources?: Resources;
}

export interface YfmArgv extends YfmConfig {
Expand All @@ -49,6 +50,7 @@ export interface YfmArgv extends YfmConfig {
contributors: boolean;
addSystemMeta: boolean;
addMapFile: boolean;
allowCustomResources: boolean;
}

export interface DocPreset {
Expand Down Expand Up @@ -158,6 +160,7 @@ export interface MetaDataOptions {
vcsConnector?: VCSConnector;
addSystemMeta?: boolean;
addSourcePath?: boolean;
resources?: Resources;
}

export interface PluginOptions {
Expand Down Expand Up @@ -221,3 +224,7 @@ export interface ResolveMd2HTMLResult {
};
lang: Lang;
}

export type Resources = {
[key in ResourceType]?: string[];
};
16 changes: 10 additions & 6 deletions src/resolvers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {ResolverOptions, YfmToc, ResolveMd2HTMLResult} from '../models';
import {ArgvService, TocService, PluginService} from '../services';
import {generateStaticMarkup, logger, transformToc, getVarsPerFile, getVarsPerRelativeFile} from '../utils';
import {PROCESSING_FINISHED, Lang} from '../constants';
import {getUpdatedMetadata} from '../services/metadata';
import {getAssetsPublicPath, getUpdatedMetadata} from '../services/metadata';

export interface FileTransformOptions {
path: string;
Expand All @@ -31,7 +31,7 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<ResolveM
const pathToFileDir: string = pathToDir === tocBase ? '' : pathToDir.replace(`${tocBase}${sep}`, '');
const relativePathToIndex = relative(pathToDir, `${tocBase}${sep}`);

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

Expand All @@ -42,12 +42,18 @@ export async function resolveMd2HTML(options: ResolverOptions): Promise<ResolveM
? await getUpdatedMetadata(metadata, content, result?.meta)
: result.meta;

let fileMeta = fileExtension === '.yaml' ? result.data.meta : updatedMetadata;

if (allowCustomResources && metadata?.resources) {
fileMeta = {...fileMeta, ...metadata?.resources};
}

const props = {
data: {
leading: inputPath.endsWith('.yaml'),
toc: transformToc(toc, pathToDir) || {},
...result,
meta: updatedMetadata,
meta: fileMeta,
},
router: {
pathname: join(relativePathToIndex, pathToFileDir, basename(outputPath)),
Expand Down Expand Up @@ -104,16 +110,14 @@ function MdFileTransformer(content: string, transformOptions: FileTransformOptio
const root = resolve(input);
const path: string = resolve(input, filePath);

/* Relative path from folder of .md file to root of user' output folder */
const assetsPublicPath = relative(dirname(path), resolve(input));

return transform(content, {
...options,
plugins,
vars,
root,
path,
assetsPublicPath,
assetsPublicPath: getAssetsPublicPath(filePath),
getVarsPerFile: getVarsPerRelativeFile,
extractTitle: true,
});
Expand Down
40 changes: 36 additions & 4 deletions src/services/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {dump} from 'js-yaml';

import {VCSConnector} from '../vcs-connector/connector-models';
import {Metadata, MetaDataOptions} from '../models';
import {Metadata, MetaDataOptions, Resources} from '../models';
import {getAuthorDetails, updateAuthorMetadataString} from './authors';
import {getFileContributorsMetadata, getFileContributorsString} from './contributors';
import {isObject} from './utils';
import {сarriage} from '../utils';
import {metadataBorder} from '../constants';
import {metadataBorder, ResourceType} from '../constants';
import {dirname, join, relative, resolve} from 'path';
import {ArgvService} from './index';

async function getContentWithUpdatedMetadata(
fileContent: string,
Expand All @@ -24,19 +28,24 @@ function getContentWithUpdatedStaticMetadata(
options?: MetaDataOptions,
systemVars?: unknown,
): string {
if (!options || (!options?.addSystemMeta || !systemVars) && !options?.addSourcePath) {
const newMetadatas: string[] = [];

if (!options || (!options?.addSystemMeta || !systemVars) && !options?.addSourcePath && !options.resources) {
return fileContent;
}

const matches = matchMetadata(fileContent);
const newMetadatas: string[] = [];

const {addSystemMeta, addSourcePath, fileData} = options;

if (addSystemMeta && systemVars && isObject(systemVars)) {
newMetadatas.push(getSystemVarsMetadataString(systemVars));
}

if (options.resources) {
newMetadatas.push(dump(options.resources));
}

if (addSourcePath && fileData.sourcePath) {
const sourcePathMetadataString = `sourcePath: ${fileData.sourcePath}`;
newMetadatas.push(sourcePathMetadataString);
Expand Down Expand Up @@ -181,8 +190,31 @@ function getSystemVarsMetadataString(systemVars: object) {
return `__system: ${JSON.stringify(systemVars)}`;
}


function getAssetsPublicPath(filePath: string) {
const {input} = ArgvService.getConfig();
const path: string = resolve(input, filePath);

/* Relative path from folder of .md file to root of user' output folder */
return relative(dirname(path), resolve(input));
}

function getResolvedResourcePaths(resources: Resources, assetsPublicPath: string) {
const metaResources = {...resources};

for (const type of Object.keys(metaResources) as Array<keyof typeof ResourceType>) {
metaResources[type] = metaResources[type]?.map((el) => {
return join(assetsPublicPath, el);
});
}

return metaResources;
}

export {
getContentWithUpdatedMetadata,
getContentWithUpdatedStaticMetadata,
getUpdatedMetadata,
getResolvedResourcePaths,
getAssetsPublicPath,
};
16 changes: 3 additions & 13 deletions src/steps/processAssets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import walkSync from 'walk-sync';
import {dirname, resolve} from 'path';
import {resolve} from 'path';
import shell from 'shelljs';
import {copyFileSync} from 'fs';

import {BUNDLE_FILENAME, BUILD_FOLDER_PATH} from '../constants';
import {ArgvService} from '../services';
import {logger} from '../utils';
import {copyFiles} from '../utils';

/**
* Processes assets files (everything except .yaml and .md files)
Expand All @@ -27,16 +26,7 @@ export function processAssets(outputBundlePath: string) {
],
});

for (const pathToAsset of assetFilePath) {
const outputDir: string = resolve(outputFolderPath, dirname(pathToAsset));
const from = resolve(inputFolderPath, pathToAsset);
const to = resolve(outputFolderPath, pathToAsset);

shell.mkdir('-p', outputDir);
copyFileSync(from, to);

logger.copy(pathToAsset);
}
copyFiles(inputFolderPath, outputFolderPath, assetFilePath);

/* Copy js bundle to user' output folder */
const sourceBundlePath = resolve(BUILD_FOLDER_PATH, BUNDLE_FILENAME);
Expand Down
Loading

0 comments on commit 80c6624

Please sign in to comment.