chore: make readme logo absolute path (#2340)
* chore: make readme logo absolute path * Update rewrite-links.js * Revert README.md * Revert README.md * Prettify rewrite-links.js * Fix lint errors of rewrite-links.js --------- Co-authored-by: ByoungYong Kim <ryubro@users.noreply.github.com>
This commit is contained in:
parent
709cc5193a
commit
5fa385e923
1 changed files with 27 additions and 33 deletions
|
|
@ -7,7 +7,8 @@ const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const rewriteLinksConfig = {
|
const rewriteLinksConfig = {
|
||||||
githubPath: 'https://github.com/ing-bank/lion/blob/master/',
|
repoBasePath: 'https://github.com/ing-bank/lion/blob/master/',
|
||||||
|
assetBasePath: 'https://raw.githubusercontent.com/ing-bank/lion/master/',
|
||||||
monorepoRootPath: path.resolve(__dirname, '../'),
|
monorepoRootPath: path.resolve(__dirname, '../'),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -54,57 +55,50 @@ function gatherFilesFromDir(startPath, cfg = gatherFilesConfig, result = []) {
|
||||||
* @param {string} filePath - local filesystem path of md file,
|
* @param {string} filePath - local filesystem path of md file,
|
||||||
* like '/path/to/lion/packages/my-component/docs/myClass.md'
|
* like '/path/to/lion/packages/my-component/docs/myClass.md'
|
||||||
* @param {object} cfg - configurantion object
|
* @param {object} cfg - configurantion object
|
||||||
* @param {string} cfg.githubPath - root github url for the absolute result links
|
* @param {string} cfg.repoBasePath - root repository url for the absolute result links
|
||||||
|
* @param {string} cfg.assetBasePath - root asset url for the absolute result image url
|
||||||
* @param {string} cfg.monorepoRootPath - local filesystem root path of the monorepo
|
* @param {string} cfg.monorepoRootPath - local filesystem root path of the monorepo
|
||||||
* @returns {string} adjusted contents of input md file (mdContent)
|
* @returns {string} adjusted contents of input md file (mdContent)
|
||||||
*/
|
*/
|
||||||
function rewriteLinksInMdContent(mdContent, filePath, cfg = rewriteLinksConfig) {
|
function rewriteLinksInMdContent(mdContent, filePath, cfg = rewriteLinksConfig) {
|
||||||
const rewrite = (/** @type {string} */ href) => {
|
const rewrite =
|
||||||
let newHref = href;
|
(/** @type {string} */ repoBasePath, /** @type {string} */ repoRootPath) =>
|
||||||
|
(/** @type {string} */ href) => {
|
||||||
const isRelativeUrlPattern = /^(\.\/|\.\.\/)/; // starts with './' or '../'
|
const isRelativeUrlPattern = /^(\.\/|\.\.\/)/; // starts with './' or '../'
|
||||||
if (href.match(isRelativeUrlPattern)) {
|
if (!href.match(isRelativeUrlPattern)) {
|
||||||
|
return href;
|
||||||
|
}
|
||||||
|
|
||||||
const fileFolder = filePath.replace(/(.*\/).*/g, '$1');
|
const fileFolder = filePath.replace(/(.*\/).*/g, '$1');
|
||||||
const absoluteLocalPath = path.resolve(fileFolder, href);
|
const absoluteLocalPath = path.resolve(fileFolder, href);
|
||||||
// relativeFromRootPath: for instance 'packages/my-component/docs/' when
|
// relativeFromRootPath: for instance 'packages/my-component/docs/' when
|
||||||
// filePath is 'path/to/repo/packages/my-component/docs/myDoc.md'
|
// filePath is 'path/to/repo/packages/my-component/docs/myDoc.md'
|
||||||
const relativeFromRootPath = absoluteLocalPath.replace(cfg.monorepoRootPath, '').slice(1);
|
const relativeFromRootPath = absoluteLocalPath.replace(repoRootPath, '').slice(1);
|
||||||
// newRoot: https://github.com/ing-bank/lion/blob/master/packages/my-component/docs/
|
// newRoot: https://github.com/ing-bank/lion/blob/master/packages/my-component/docs/
|
||||||
newHref = cfg.githubPath + relativeFromRootPath;
|
|
||||||
}
|
return repoBasePath + relativeFromRootPath;
|
||||||
return newHref;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const mdLink = (
|
const mdLink = (
|
||||||
/** @type {string} */ href,
|
/** @type {string} */ href,
|
||||||
/** @type {string} */ title,
|
/** @type {string} */ title,
|
||||||
/** @type {string} */ text,
|
/** @type {string} */ text,
|
||||||
) => `[${text}](${rewrite(href)}${title ? ` ${title}` : ''})`;
|
) =>
|
||||||
|
`[${text}](${rewrite(cfg.repoBasePath, cfg.monorepoRootPath)(href)}${title ? ` ${title}` : ''})`;
|
||||||
|
|
||||||
/** @type {string[]} */
|
|
||||||
const resultLinks = [];
|
|
||||||
// /^!?\[(label)\]\(href(?:\s+(title))?\s*\)/
|
// /^!?\[(label)\]\(href(?:\s+(title))?\s*\)/
|
||||||
const linkPattern = '!?\\[(.*)\\]\\(([^|\\s]*)( +(.*))?\\s*\\)'; // eslint-disable-line
|
const linkPattern = '!?\\[(.*)\\]\\(([^|\\s]*)( +(.*))?\\s*\\)'; // eslint-disable-line
|
||||||
const matches = mdContent.match(new RegExp(linkPattern, 'g')) || [];
|
const linkReplaced = mdContent.replace(new RegExp(linkPattern, 'g'), (_, p1, p2, p3) =>
|
||||||
|
mdLink(p2, p3, p1),
|
||||||
|
);
|
||||||
|
|
||||||
matches.forEach(link => {
|
const imgSrcPattern = `src *= *['"](.*)['"]`;
|
||||||
let newLink = '';
|
const imageReplaced = linkReplaced.replace(
|
||||||
const parts = link.match(new RegExp(linkPattern));
|
new RegExp(imgSrcPattern, 'g'),
|
||||||
if (parts) {
|
(_, p1) => `src="${rewrite(cfg.assetBasePath, cfg.monorepoRootPath)(p1)}`,
|
||||||
newLink = mdLink(parts[2], parts[3], parts[1]);
|
);
|
||||||
}
|
|
||||||
resultLinks.push(newLink);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Now that we have our rewritten links, stitch back together the desired result
|
return imageReplaced;
|
||||||
const tokenPattern = /!?\[.*\]\([^|\s]*(?: +.*)?\s*\)/;
|
|
||||||
const tokens = mdContent.split(new RegExp(tokenPattern, 'g'));
|
|
||||||
/** @type {string[]} */
|
|
||||||
const resultTokens = [];
|
|
||||||
tokens.forEach((token, i) => {
|
|
||||||
resultTokens.push(token + (resultLinks[i] || ''));
|
|
||||||
});
|
|
||||||
const resultContent = resultTokens.join('');
|
|
||||||
return resultContent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue