feat: improve navigation of adr plugin
Signed-off-by: David Weber <david.weber@w3tec.ch>
This commit is contained in:
@@ -14,12 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { DateTime } from 'luxon';
|
||||
import { marked } from 'marked';
|
||||
import { stringifyEntityRef } from '@backstage/catalog-model';
|
||||
import { MADR_DATE_FORMAT } from '@backstage/plugin-adr-common';
|
||||
|
||||
import { AdrParser } from './types';
|
||||
import { madrParser } from './madrParser';
|
||||
|
||||
const applyArgsToFormat = (
|
||||
format: string,
|
||||
@@ -65,43 +64,12 @@ export const createMadrParser = (
|
||||
const dateFormat = options.dateFormat ?? MADR_DATE_FORMAT;
|
||||
|
||||
return async ({ entity, content, path }) => {
|
||||
const tokens = marked.lexer(content);
|
||||
if (!tokens.length) {
|
||||
throw new Error('ADR has no content');
|
||||
}
|
||||
|
||||
// First h1 header should contain ADR title
|
||||
const adrTitle = (
|
||||
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
|
||||
.find(t => /^status:/i.test(t.text))
|
||||
?.text.replace(/^status:/i, '')
|
||||
.trim()
|
||||
.toLocaleLowerCase('en-US');
|
||||
const adrDateTime = DateTime.fromFormat(
|
||||
listTokens
|
||||
.find(t => /^date:/i.test(t.text))
|
||||
?.text.replace(/^date:/i, '')
|
||||
.trim() ?? '',
|
||||
dateFormat,
|
||||
);
|
||||
const adrDate = adrDateTime.isValid
|
||||
? adrDateTime.toFormat(MADR_DATE_FORMAT)
|
||||
: undefined;
|
||||
|
||||
const madr = madrParser(content, dateFormat);
|
||||
return {
|
||||
title: adrTitle ?? path.replace(/\.md$/, ''),
|
||||
title: madr.title ?? path.replace(/\.md$/, ''),
|
||||
text: content,
|
||||
status: adrStatus,
|
||||
date: adrDate,
|
||||
status: madr.status,
|
||||
date: madr.date,
|
||||
entityRef: stringifyEntityRef(entity),
|
||||
entityTitle: entity.metadata.title,
|
||||
location: applyArgsToFormat(locationTemplate, {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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 = (
|
||||
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
|
||||
?.find(t => /^status:/i.test(t.text))
|
||||
?.text.replace(/^status:/i, '')
|
||||
.trim()
|
||||
.toLocaleLowerCase('en-US');
|
||||
|
||||
const adrDateTime = DateTime.fromFormat(
|
||||
listTokens
|
||||
?.find(t => /^date:/i.test(t.text))
|
||||
?.text.replace(/^date:/i, '')
|
||||
.trim() ?? '',
|
||||
dateFormat,
|
||||
);
|
||||
const adrDate = adrDateTime?.isValid
|
||||
? adrDateTime.toFormat(MADR_DATE_FORMAT)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
title: adrTitle,
|
||||
status: adrStatus,
|
||||
date: adrDate,
|
||||
};
|
||||
};
|
||||
@@ -156,8 +156,8 @@ describe('createRouter', () => {
|
||||
data: [
|
||||
{
|
||||
type: 'file',
|
||||
name: 'testFile001.txt',
|
||||
path: 'folder/testFile001.txt',
|
||||
name: 'testFile002.txt',
|
||||
path: 'testFile002.txt',
|
||||
},
|
||||
{
|
||||
type: 'file',
|
||||
@@ -166,8 +166,8 @@ describe('createRouter', () => {
|
||||
},
|
||||
{
|
||||
type: 'file',
|
||||
name: 'testFile002.txt',
|
||||
path: 'testFile002.txt',
|
||||
name: 'testFile001.txt',
|
||||
path: 'folder/testFile001.txt',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -19,6 +19,7 @@ import { NotModifiedError, stringifyError } from '@backstage/errors';
|
||||
import { Logger } from 'winston';
|
||||
import express from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import { madrParser } from '../search/madrParser';
|
||||
|
||||
/** @public */
|
||||
export type AdrRouterOptions = {
|
||||
@@ -59,13 +60,20 @@ export async function createRouter(
|
||||
etag: cachedTree?.etag,
|
||||
});
|
||||
const files = await treeGetResponse.files();
|
||||
const data = files.map(file => {
|
||||
return {
|
||||
type: 'file',
|
||||
name: file.path.substring(file.path.lastIndexOf('/') + 1),
|
||||
path: file.path,
|
||||
};
|
||||
});
|
||||
const data = await Promise.all(
|
||||
files
|
||||
.map(async file => {
|
||||
const fileContent = await file.content();
|
||||
const adrInfo = madrParser(fileContent.toString());
|
||||
return {
|
||||
type: 'file',
|
||||
name: file.path.substring(file.path.lastIndexOf('/') + 1),
|
||||
path: file.path,
|
||||
...adrInfo,
|
||||
};
|
||||
})
|
||||
.reverse(),
|
||||
);
|
||||
|
||||
await cacheClient.set(urlToProcess, {
|
||||
data,
|
||||
|
||||
Reference in New Issue
Block a user