chore: a little refactoring on files
Signed-off-by: Alisson Fabiano <afabiano@eshopworld.com>
This commit is contained in:
@@ -402,17 +402,29 @@ export class AzureDevOpsApi {
|
||||
organization: string,
|
||||
projectName: string,
|
||||
repoName: string,
|
||||
): Promise<string> {
|
||||
const getContentFile = async (
|
||||
): Promise<{
|
||||
url: string;
|
||||
content: string;
|
||||
}> {
|
||||
const getFileContent = async (
|
||||
path: string,
|
||||
encoding?: BufferEncoding,
|
||||
): Promise<string> => {
|
||||
): Promise<{
|
||||
url: string;
|
||||
content: string;
|
||||
}> => {
|
||||
const url = `https://${host}/${organization}/${projectName}/_git/${repoName}?path=${path}`;
|
||||
const response = await this.urlReader.read(url);
|
||||
return Buffer.from(response).toString(encoding);
|
||||
return {
|
||||
url,
|
||||
content: Buffer.from(response).toString(encoding),
|
||||
};
|
||||
};
|
||||
const { url, content } = await getFileContent('README.md');
|
||||
return {
|
||||
url,
|
||||
content: await replaceReadme(content, getFileContent),
|
||||
};
|
||||
const readmeContent = await getContentFile('README.md');
|
||||
return await replaceReadme(readmeContent, getContentFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -194,16 +194,13 @@ export async function createRouter(
|
||||
|
||||
router.get('/readme/:projectName/:repoName', async (req, res) => {
|
||||
const { projectName, repoName } = req.params;
|
||||
const content = await azureDevOpsApi.getReadme(
|
||||
const readme = await azureDevOpsApi.getReadme(
|
||||
host,
|
||||
organization,
|
||||
projectName,
|
||||
repoName,
|
||||
);
|
||||
res.status(200).json({
|
||||
content,
|
||||
url: `https://${host}/${organization}/${projectName}/_git/${repoName}?path=README.md`,
|
||||
});
|
||||
res.status(200).json(readme);
|
||||
});
|
||||
|
||||
router.use(errorHandler());
|
||||
|
||||
@@ -206,26 +206,27 @@ export function convertPolicy(
|
||||
}
|
||||
|
||||
export async function replaceReadme(
|
||||
content: string,
|
||||
getContentFile: (path: string, encoding?: BufferEncoding) => Promise<string>,
|
||||
readme: string,
|
||||
getFileContent: (
|
||||
path: string,
|
||||
encoding?: BufferEncoding,
|
||||
) => Promise<{
|
||||
url: string;
|
||||
content: string;
|
||||
}>,
|
||||
) {
|
||||
const regExp =
|
||||
/\[([^\[\]]*)\]\((?!https?:\/\/)(.*?)(\.png|\.jpg|\.jpeg|\.gif|\.webp)(.*)\)/gim;
|
||||
const filesPath = content.match(regExp) || [];
|
||||
|
||||
if (filesPath.length === 0) return content;
|
||||
|
||||
let replacedContent = content;
|
||||
const filesPath = extractAssets(readme);
|
||||
let content = readme;
|
||||
for (const filePath of filesPath) {
|
||||
const { label, path, ext } = getPartsFromFilePath(filePath);
|
||||
const { label, path, ext } = extractPartsFromAsset(filePath);
|
||||
const mime = getMimeByExtension(ext);
|
||||
const base64 = await getContentFile(path + ext, 'base64');
|
||||
replacedContent = replacedContent.replace(
|
||||
const { content: base64 } = await getFileContent(path + ext, 'base64');
|
||||
content = content.replace(
|
||||
filePath,
|
||||
`[${label}](data:${mime};base64,${base64})`,
|
||||
);
|
||||
}
|
||||
return replacedContent;
|
||||
return content;
|
||||
}
|
||||
|
||||
function convertReviewer(
|
||||
@@ -287,7 +288,13 @@ function hasAutoComplete(pullRequest: GitPullRequest): boolean {
|
||||
return pullRequest.isDraft !== true && !!pullRequest.completionOptions;
|
||||
}
|
||||
|
||||
function getPartsFromFilePath(content: string): {
|
||||
function extractAssets(content: string) {
|
||||
const regExp =
|
||||
/\[([^\[\]]*)\]\((?!https?:\/\/)(.*?)(\.png|\.jpg|\.jpeg|\.gif|\.webp)(.*)\)/gim;
|
||||
return content.match(regExp) || [];
|
||||
}
|
||||
|
||||
function extractPartsFromAsset(content: string): {
|
||||
label: string;
|
||||
path: string;
|
||||
ext: string;
|
||||
|
||||
Reference in New Issue
Block a user