From 73b67b2e6489b2ed798df1e1782ee2bad0108452 Mon Sep 17 00:00:00 2001 From: Kaspar Emanuel Date: Wed, 26 Oct 2022 16:23:53 +0100 Subject: [PATCH] Refactor summary processing Co-authored-by: Kaspar Emanuel --- e2e/cypress/integration/readme.spec.js | 4 +- processor/src/tasks/writeKitspaceYaml.ts | 48 ++++++++++++------------ processor/src/utils.ts | 2 +- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/e2e/cypress/integration/readme.spec.js b/e2e/cypress/integration/readme.spec.js index f298e4b984..7e21a90248 100644 --- a/e2e/cypress/integration/readme.spec.js +++ b/e2e/cypress/integration/readme.spec.js @@ -158,7 +158,7 @@ describe('Readme style', () => { }) }) - it('should render :emoji: in readme and project description', () => { + it('renders :emoji: in readme and project description', () => { const username = getFakeUsername() const email = faker.unique(faker.internet.email) const password = '123456' @@ -186,7 +186,7 @@ describe('Readme style', () => { cy.get('[data-cy=readme]').should('contain.text', '🤓') }) - it('should not modify the urls for GitHub Actions badges', () => { + it('preserves URLs for GitHub Actions badges', () => { const username = getFakeUsername() const email = faker.unique(faker.internet.email) const password = '123456' diff --git a/processor/src/tasks/writeKitspaceYaml.ts b/processor/src/tasks/writeKitspaceYaml.ts index b18457d23e..482644f3bb 100644 --- a/processor/src/tasks/writeKitspaceYaml.ts +++ b/processor/src/tasks/writeKitspaceYaml.ts @@ -12,46 +12,46 @@ import remarkRehype from 'remark-rehype' import { JobData } from '../jobData.js' import * as utils from '../utils.js' - export default async function writeKitspaceYaml( job, { kitspaceYaml, outputDir }: Partial, ) { const kitspaceYamlJson = path.join(outputDir, 'kitspace-yaml.json') job.updateProgress({ status: 'in_progress', file: kitspaceYamlJson }) - const KitspaceYamlJsonLinkified = await linkifyKitspaceYaml(kitspaceYaml) + + const rendered = await renderKitspaceYamlSummaries(kitspaceYaml) return utils - .writeFile(kitspaceYamlJson, JSON.stringify(KitspaceYamlJsonLinkified, null, 2)) + .writeFile(kitspaceYamlJson, JSON.stringify(rendered, null, 2)) .then(() => job.updateProgress({ status: 'done', file: kitspaceYamlJson })) .catch(error => job.updateProgress({ status: 'failed', file: kitspaceYamlJson, error }), ) } -async function linkifyKitspaceYaml(kitspaceYaml) { +async function renderKitspaceYamlSummaries(kitspaceYaml) { if (kitspaceYaml.multi) { - const linkifiedKitspaceYaml = kitspaceYaml - Object.keys(kitspaceYaml.multi).forEach(async subProject => { - linkifiedKitspaceYaml.multi[subProject] = await renderProjectSummary(kitspaceYaml.multi[subProject]) - }) - return linkifiedKitspaceYaml + const rendered = { multi: {} } + for (const key of kitspaceYaml.multi) { + const subProject = kitspaceYaml.multi[key] + rendered.multi[subProject] = { + ...subProject, + summary: await renderSummary(subProject.summary), + } + } + return rendered } - return renderProjectSummary(kitspaceYaml) + return { ...kitspaceYaml, summary: await renderSummary(kitspaceYaml.summary) } } -async function renderProjectSummary(kitspaceYaml) { - const summary = kitspaceYaml.summary || '' - const Remarker = unified() - .use(remarkParse) - .use(remarkEmoji) - .use(remarkGfm) - .use(remarkRehype) - .use(rehypeHighlight) - .use(rehypeSanitize) - .use(rehypeStringify) - - const processedMarkdown = await Remarker.process(summary) - kitspaceYaml.summary = String(processedMarkdown) - return kitspaceYaml +const Remarker = unified() + .use(remarkParse) + .use(remarkEmoji) + .use(remarkRehype) + .use(rehypeSanitize) + .use(rehypeStringify) + +async function renderSummary(summary = ''): Promise { + const rendered = await Remarker.process(summary) + return String(rendered) } diff --git a/processor/src/utils.ts b/processor/src/utils.ts index d1079750d1..4b25274756 100644 --- a/processor/src/utils.ts +++ b/processor/src/utils.ts @@ -80,7 +80,7 @@ export function toGitHubRawUrl(url: string) { if (parsedUrl.hostname === 'github.com') { parsedUrl.hostname = 'raw.githubusercontent.com' const urlPath = parsedUrl.pathname.split('/') - // Avoid using modifying github actions status badges. + // Avoid modifying github actions status badges. const isWorkflowPath = ['workflows', 'actions'].includes(urlPath?.[3]) if (!isWorkflowPath) {