Parse and display MADR v3 front matter correctly

Signed-off-by: Brian Phillips <28457+brianphillips@users.noreply.github.com>
This commit is contained in:
Brian Phillips
2023-05-24 08:57:41 -05:00
parent f1f22208b7
commit 3656c99897
6 changed files with 102 additions and 68 deletions
@@ -59,6 +59,7 @@ export const AdrReader = (props: {
const adrDecorators = decorators ?? [
adrDecoratorFactories.createRewriteRelativeLinksDecorator(),
adrDecoratorFactories.createRewriteRelativeEmbedsDecorator(),
adrDecoratorFactories.createFrontMatterFormatterDecorator(),
];
return adrDecorators.reduce(
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { parseMadrWithFrontmatter } from '@backstage/plugin-adr-common';
import { AdrContentDecorator } from './types';
/**
@@ -44,4 +45,23 @@ export const adrDecoratorFactories = Object.freeze({
),
});
},
/**
* Formats YAML front-matter into a table format (if any exists in the markdown document)
*/
createFrontMatterFormatterDecorator(): AdrContentDecorator {
return ({ content }) => {
const parsedFrontmatter = parseMadrWithFrontmatter(content);
let table = '';
const attrs = parsedFrontmatter.attributes;
if (Object.keys(attrs).length > 0) {
const stripNewLines = (val: unknown) =>
String(val).replaceAll('\n', '<br/>');
const row = (vals: string[]) => `|${vals.join('|')}|\n`;
table = `${row(Object.keys(attrs))}`;
table += `${row(Object.keys(attrs).map(() => '---'))}`;
table += `${row(Object.values(attrs).map(stripNewLines))}\n\n`;
}
return { content: table + parsedFrontmatter.content };
};
},
});