From 302a5b31d819896c86b269e0b53de7cf1fa88322 Mon Sep 17 00:00:00 2001 From: Rob Herley Date: Wed, 2 Mar 2022 12:10:01 -0500 Subject: [PATCH] summary: add link/anchor element --- packages/core/__tests__/markdown-summary.test.ts | 12 ++++++++++++ packages/core/src/markdown-summary.ts | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packages/core/__tests__/markdown-summary.test.ts b/packages/core/__tests__/markdown-summary.test.ts index efb317f0..9b6ac037 100644 --- a/packages/core/__tests__/markdown-summary.test.ts +++ b/packages/core/__tests__/markdown-summary.test.ts @@ -60,6 +60,10 @@ const fixtures = { quote: { text: 'Where the world builds software', cite: 'https://github.com/about' + }, + link: { + text: 'GitHub', + href: 'https://github.com/' } } @@ -237,4 +241,12 @@ describe('@actions/core/src/markdown-summary', () => { const expected = `
Where the world builds software
${os.EOL}` await assertSummary(expected) }) + + it('adds a link with href', async () => { + await markdownSummary + .addLink(fixtures.link.text, fixtures.link.href) + .write() + const expected = `GitHub${os.EOL}` + await assertSummary(expected) + }) }) diff --git a/packages/core/src/markdown-summary.ts b/packages/core/src/markdown-summary.ts index 461fef0b..352767ca 100644 --- a/packages/core/src/markdown-summary.ts +++ b/packages/core/src/markdown-summary.ts @@ -5,6 +5,7 @@ const {access, appendFile, writeFile} = promises export const SUMMARY_ENV_VAR = 'GITHUB_STEP_SUMMARY' export type SummaryTableRow = (SummaryTableCell | string)[] + export interface SummaryTableCell { /** * Cell content @@ -312,6 +313,19 @@ class MarkdownSummary { const element = this.wrap('blockquote', text, attrs) return this.add(element).addEOL() } + + /** + * Adds an HTML anchor tag to the summary buffer + * + * @param {string} text link text/content + * @param {string} href hyperlink + * + * @returns {MarkdownSummary} markdown summary instance + */ + addLink(text: string, href: string): MarkdownSummary { + const element = this.wrap('a', text, {href}) + return this.add(element).addEOL() + } } // singleton export