feat: add support for MADR v3
Signed-off-by: Kumar <gaurav@kgaurav.com>
This commit is contained in:
@@ -89,7 +89,7 @@ indexBuilder.addCollator({
|
||||
|
||||
### Parsing custom ADR document formats
|
||||
|
||||
By default, the `DefaultAdrCollatorFactory` will parse and index documents that follow the [MADR v2.x standard file name and template format](https://github.com/adr/madr/tree/2.1.2). If you use a different ADR format and file name convention, you can configure `DefaultAdrCollatorFactory` with custom `adrFilePathFilterFn` and `parser` options (see type definitions for details):
|
||||
By default, the `DefaultAdrCollatorFactory` will parse and index documents that follow [MADR v3.0.0](https://github.com/adr/madr/tree/3.0.0) and [MADR v2.x](https://github.com/adr/madr/tree/2.1.2) standard file name and template format. If you use a different ADR format and file name convention, you can configure `DefaultAdrCollatorFactory` with custom `adrFilePathFilterFn` and `parser` options (see type definitions for details):
|
||||
|
||||
```ts
|
||||
DefaultAdrCollatorFactory.fromConfig({
|
||||
|
||||
@@ -18,28 +18,71 @@ import { DateTime } from 'luxon';
|
||||
import { marked } from 'marked';
|
||||
import { MADR_DATE_FORMAT } from '@backstage/plugin-adr-common';
|
||||
|
||||
export const madrParser = (content: string, dateFormat = MADR_DATE_FORMAT) => {
|
||||
const tokens = marked.lexer(content);
|
||||
if (!tokens.length) {
|
||||
throw new Error('ADR has no content');
|
||||
}
|
||||
|
||||
// First h1 header should contain ADR title
|
||||
const adrTitle = (
|
||||
const getTitle = (tokens: marked.TokensList): string | undefined => {
|
||||
return (
|
||||
tokens.find(
|
||||
t => t.type === 'heading' && t.depth === 1,
|
||||
) as marked.Tokens.Heading
|
||||
)?.text;
|
||||
};
|
||||
|
||||
// First list should contain status & date metadata (if defined)
|
||||
const listTokens = (tokens.find(t => t.type === 'list') as marked.Tokens.List)
|
||||
?.items;
|
||||
const adrStatus = listTokens
|
||||
const getStatusForV3Format = (tokens: marked.TokensList): string | undefined =>
|
||||
(
|
||||
tokens.find(
|
||||
t =>
|
||||
t.type === 'heading' &&
|
||||
t.depth === 2 &&
|
||||
t.text.toLowerCase().includes('status:'),
|
||||
) as marked.Tokens.Text
|
||||
)?.text
|
||||
.toLowerCase()
|
||||
.split('\n')
|
||||
.find(l => /^status:/.test(l))
|
||||
?.replace(/^status:/i, '')
|
||||
.trim()
|
||||
.toLocaleLowerCase('en-US');
|
||||
|
||||
const getStatusForV2Format = (tokens: marked.TokensList): string | undefined =>
|
||||
(tokens.find(t => t.type === 'list') as marked.Tokens.List)?.items
|
||||
?.find(t => /^status:/i.test(t.text))
|
||||
?.text.replace(/^status:/i, '')
|
||||
.trim()
|
||||
.toLocaleLowerCase('en-US');
|
||||
|
||||
const getDateForV3Format = (
|
||||
tokens: marked.TokensList,
|
||||
dateFormat: string,
|
||||
): string | undefined => {
|
||||
let adrDateTime: DateTime | undefined;
|
||||
const dateLine = (
|
||||
tokens.find(
|
||||
t =>
|
||||
t.type === 'heading' &&
|
||||
t.depth === 2 &&
|
||||
t.text.toLowerCase().includes('date:'),
|
||||
) as marked.Tokens.Text
|
||||
)?.text
|
||||
.toLowerCase()
|
||||
.split('\n')
|
||||
.find(l => /^date:/.test(l));
|
||||
|
||||
if (dateLine) {
|
||||
adrDateTime = DateTime.fromFormat(
|
||||
dateLine.replace(/^date:/i, '').trim(),
|
||||
dateFormat,
|
||||
);
|
||||
}
|
||||
return adrDateTime?.isValid
|
||||
? adrDateTime.toFormat(MADR_DATE_FORMAT)
|
||||
: undefined;
|
||||
};
|
||||
|
||||
const getDateForV2Format = (
|
||||
tokens: marked.TokensList,
|
||||
dateFormat: string,
|
||||
): string | undefined => {
|
||||
const listTokens = (tokens.find(t => t.type === 'list') as marked.Tokens.List)
|
||||
?.items;
|
||||
const adrDateTime = DateTime.fromFormat(
|
||||
listTokens
|
||||
?.find(t => /^date:/i.test(t.text))
|
||||
@@ -47,13 +90,30 @@ export const madrParser = (content: string, dateFormat = MADR_DATE_FORMAT) => {
|
||||
.trim() ?? '',
|
||||
dateFormat,
|
||||
);
|
||||
const adrDate = adrDateTime?.isValid
|
||||
return adrDateTime?.isValid
|
||||
? adrDateTime.toFormat(MADR_DATE_FORMAT)
|
||||
: undefined;
|
||||
};
|
||||
|
||||
const getStatus = (tokens: marked.TokensList): string | undefined => {
|
||||
return getStatusForV3Format(tokens) ?? getStatusForV2Format(tokens);
|
||||
};
|
||||
const getDate = (
|
||||
tokens: marked.TokensList,
|
||||
dateFormat: string,
|
||||
): string | undefined =>
|
||||
getDateForV3Format(tokens, dateFormat) ??
|
||||
getDateForV2Format(tokens, dateFormat);
|
||||
|
||||
export const madrParser = (content: string, dateFormat = MADR_DATE_FORMAT) => {
|
||||
const tokens = marked.lexer(content);
|
||||
if (!tokens.length) {
|
||||
throw new Error('ADR has no content');
|
||||
}
|
||||
|
||||
return {
|
||||
title: adrTitle,
|
||||
status: adrStatus,
|
||||
date: adrDate,
|
||||
title: getTitle(tokens),
|
||||
status: getStatus(tokens),
|
||||
date: getDate(tokens, dateFormat),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user