diff --git a/plugins/readme-backend/package.json b/plugins/readme-backend/package.json index f8939f5..c4a5614 100644 --- a/plugins/readme-backend/package.json +++ b/plugins/readme-backend/package.json @@ -32,6 +32,7 @@ "@backstage/backend-plugin-api": "^0.8.0", "@backstage/catalog-client": "^1.6.6", "@backstage/catalog-model": "^1.6.0", + "@backstage/config": "^1.3.2", "@backstage/errors": "^1.2.4", "@backstage/integration": "^1.14.0", "@types/express": "*", diff --git a/plugins/readme-backend/src/service/constants.ts b/plugins/readme-backend/src/service/constants.ts index 689253c..1eb706c 100644 --- a/plugins/readme-backend/src/service/constants.ts +++ b/plugins/readme-backend/src/service/constants.ts @@ -1,13 +1,28 @@ import { FileType } from './types'; +import { Config } from '@backstage/config'; // Cache placeholder for entities where no readme export const NOT_FOUND_PLACEHOLDER = 'NOT_FOUND'; export const DEFAULT_TTL = 1800 * 1000; -export const README_TYPES: FileType[] = [ - { name: 'README', type: 'text/plain' }, +const DEFAULT_README_TYPES: FileType[] = [ { name: 'README.md', type: 'text/markdown' }, + { name: 'README.MD', type: 'text/markdown' }, + { name: 'README', type: 'text/plain' }, { name: 'README.rst', type: 'text/plain' }, { name: 'README.txt', type: 'text/plain' }, - { name: 'README.MD', type: 'text/markdown' }, ]; + +export function getReadmeTypes(config: Config): FileType[] { + const readmeTypes = config.getOptionalStringArray('readme.types'); + if (!readmeTypes) { + return DEFAULT_README_TYPES; + } + return readmeTypes.map(name => { + const type = + name.endsWith('.md') || name.endsWith('.MD') + ? 'text/markdown' + : 'text/plain'; + return { name, type }; + }); +} diff --git a/plugins/readme-backend/src/service/router.ts b/plugins/readme-backend/src/service/router.ts index b7c5368..e8a9c11 100644 --- a/plugins/readme-backend/src/service/router.ts +++ b/plugins/readme-backend/src/service/router.ts @@ -17,7 +17,11 @@ import { isError, NotFoundError } from '@backstage/errors'; import express from 'express'; import Router from 'express-promise-router'; import { isSymLink } from '../lib'; -import { DEFAULT_TTL, NOT_FOUND_PLACEHOLDER, README_TYPES } from './constants'; +import { + DEFAULT_TTL, + NOT_FOUND_PLACEHOLDER, + getReadmeTypes, +} from './constants'; import { ReadmeFile } from './types'; /** @@ -97,7 +101,9 @@ export async function createRouter( return; } - for (const fileType of README_TYPES) { + const readmeTypes = getReadmeTypes(config); + + for (const fileType of readmeTypes) { const url = integration.resolveUrl({ url: fileType.name, base: source.target,