2021-12-09 21:12:49 +01:00
|
|
|
import { logger } from '../../lib/logger';
|
|
|
|
import type { ModuleApi } from '../../lib/types';
|
2024-09-08 11:15:43 +05:30
|
|
|
import { regEx } from '../../lib/util/regex';
|
2025-04-14 12:18:33 +05:45
|
|
|
import { capitalize } from '../../lib/util/string';
|
2021-12-09 21:12:49 +01:00
|
|
|
import { readFile } from '../utils';
|
|
|
|
|
|
|
|
const replaceStart =
|
2022-01-11 16:25:14 +01:00
|
|
|
'<!-- Autogenerate in https://github.com/renovatebot/renovate -->';
|
2021-12-09 21:12:49 +01:00
|
|
|
const replaceStop = '<!-- Autogenerate end -->';
|
2024-09-08 11:15:43 +05:30
|
|
|
const goodUrlRegex = regEx(/\[(.+?)\]\((.+?)\)/);
|
2021-12-09 21:12:49 +01:00
|
|
|
|
|
|
|
export function formatName(input: string): string {
|
|
|
|
return input.split('-').map(capitalize).join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDisplayName(
|
|
|
|
moduleName: string,
|
2023-11-07 12:50:29 -03:00
|
|
|
moduleDefinition: ModuleApi,
|
2021-12-09 21:12:49 +01:00
|
|
|
): string {
|
2022-02-11 08:51:51 +01:00
|
|
|
return moduleDefinition.displayName ?? formatName(moduleName);
|
2021-12-09 21:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getNameWithUrl(
|
|
|
|
moduleName: string,
|
2023-11-07 12:50:29 -03:00
|
|
|
moduleDefinition: ModuleApi,
|
2021-12-09 21:12:49 +01:00
|
|
|
): string {
|
|
|
|
const displayName = getDisplayName(moduleName, moduleDefinition);
|
|
|
|
if (moduleDefinition.url) {
|
|
|
|
return `[${displayName}](${moduleDefinition.url})`;
|
|
|
|
}
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function replaceContent(content: string, txt: string): string {
|
|
|
|
const replaceStartIndex = content.indexOf(replaceStart);
|
|
|
|
const replaceStopIndex = content.indexOf(replaceStop);
|
|
|
|
|
|
|
|
if (replaceStartIndex < 0) {
|
|
|
|
logger.error('Missing replace placeholder');
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
content.slice(0, replaceStartIndex) +
|
|
|
|
txt +
|
|
|
|
content.slice(replaceStopIndex + replaceStop.length)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatUrls(urls: string[] | null | undefined): string {
|
|
|
|
if (Array.isArray(urls) && urls.length) {
|
2024-05-29 11:09:07 +05:45
|
|
|
return `## References\n\n${urls
|
2024-09-08 11:15:43 +05:30
|
|
|
.map((url) => {
|
|
|
|
if (goodUrlRegex.test(url)) {
|
|
|
|
return ` - ${url}`;
|
|
|
|
}
|
|
|
|
return ` - [${url}](${url})`;
|
|
|
|
})
|
2021-12-09 21:12:49 +01:00
|
|
|
.join('\n')}\n\n`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function formatDescription(
|
|
|
|
type: string,
|
2023-11-07 12:50:29 -03:00
|
|
|
name: string,
|
2021-12-09 21:12:49 +01:00
|
|
|
): Promise<string> {
|
2022-03-22 13:36:27 +01:00
|
|
|
const content = await readFile(`lib/modules/${type}/${name}/readme.md`);
|
2021-12-09 21:12:49 +01:00
|
|
|
if (!content) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-05-29 11:09:07 +05:45
|
|
|
return `## Description\n\n${content}\n`;
|
2021-12-09 21:12:49 +01:00
|
|
|
}
|
2022-12-30 18:20:41 +01:00
|
|
|
|
2023-11-10 09:33:13 +01:00
|
|
|
export function getModuleLink(module: string, title?: string): string {
|
|
|
|
return `[${title ?? module}](${module}/index.md)`;
|
2022-12-30 18:20:41 +01:00
|
|
|
}
|