From 64028c88712d0f3a0d741c4849c35f888b7c9ef1 Mon Sep 17 00:00:00 2001 From: martyanov-av Date: Tue, 2 Apr 2024 22:55:07 +0300 Subject: [PATCH] fix: copy assets by root path --- src/steps/processAssets.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/steps/processAssets.ts b/src/steps/processAssets.ts index 1d1312df..572834a5 100644 --- a/src/steps/processAssets.ts +++ b/src/steps/processAssets.ts @@ -2,7 +2,7 @@ import walkSync from 'walk-sync'; import {load} from 'js-yaml'; import {readFileSync} from 'fs'; import shell from 'shelljs'; -import {join, resolve} from 'path'; +import {join, resolve, sep} from 'path'; import {ArgvService, TocService} from '../services'; import {checkPathExists, copyFiles, findAllValuesByKeys} from '../utils'; @@ -18,6 +18,7 @@ import { YFM_CONFIG_FILENAME, } from '../constants'; import {Resources} from '../models'; +import {resolveRelativePath} from '@diplodoc/transform/lib/utilsFS'; /** * Processes assets files (everything except .md files) @@ -99,17 +100,25 @@ function processAssetsMdRun({args, tmpOutputFolder}) { } const contentLinks = findAllValuesByKeys(content, LINK_KEYS); - const localMediaLinks = contentLinks.filter( - (link) => - new RegExp(/^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm).test(link) && - isLocalUrl(link), + const localMediaLinks = contentLinks.reduce( + (acc, link) => { + const linkHasMediaExt = new RegExp( + /^\S.*\.(svg|png|gif|jpg|jpeg|bmp|webp|ico)$/gm, + ).test(link); + + if (linkHasMediaExt && isLocalUrl(link) && checkPathExists(link, yamlFile)) { + const linkAbsolutePath = resolveRelativePath(yamlFile, link); + const linkRootPath = linkAbsolutePath.replace(`${inputFolderPath}${sep}`, ''); + + acc.push(linkRootPath); + } + return acc; + }, + + [], ); - copyFiles( - args.input, - tmpOutputFolder, - localMediaLinks.filter((link) => checkPathExists(link, yamlFile)), - ); + copyFiles(args.input, tmpOutputFolder, localMediaLinks); }); }