1
0
Fork 0

add more summary elements, clean up jsdoc

pull/1014/head
Rob Herley 2022-03-01 21:14:58 -05:00
parent ac58d176ba
commit c42d30607b
No known key found for this signature in database
GPG Key ID: D1602042C3543B06
1 changed files with 75 additions and 14 deletions

View File

@ -26,10 +26,10 @@ export interface TableCell {
export class MarkdownSummary { export class MarkdownSummary {
static ENV_VAR = 'GITHUB_STEP_SUMMARY' static ENV_VAR = 'GITHUB_STEP_SUMMARY'
private buffer: string private _buffer: string
constructor() { constructor() {
this.buffer = '' this._buffer = ''
} }
/** /**
@ -55,16 +55,18 @@ export class MarkdownSummary {
} }
/** /**
* Wraps content in an html tag, adding any HTML attributes * Wraps content in an HTML tag, adding any HTML attributes
* *
* @param tag HTML tag to wrap * @param {string} tag HTML tag to wrap
* @param content content within the tag * @param {string} content content within the tag
* @param attrs key value list of html attributes to add * @param {[attribute: string]: string} attrs key-value list of HTML attributes to add
*
* @returns {string} content wrapped in HTML element
*/ */
private wrap( private wrap(
tag: string, tag: string,
content: string, content: string,
attrs: {[key: string]: string} = {} attrs: {[attribute: string]: string} = {}
): string { ): string {
const htmlAttrs = Object.entries(attrs) const htmlAttrs = Object.entries(attrs)
.map(([key, value]) => `${key}="${value}"`) .map(([key, value]) => `${key}="${value}"`)
@ -78,13 +80,22 @@ export class MarkdownSummary {
* *
* @param {boolean} [overwrite=false] (optional) replace existing content in summary file with buffer contents, default: false * @param {boolean} [overwrite=false] (optional) replace existing content in summary file with buffer contents, default: false
* *
* @returns {MarkdownSummary} markdown summary instance * @returns {Promise<MarkdownSummary>} markdown summary instance
*/ */
async write(overwrite = false): Promise<MarkdownSummary> { async write(overwrite = false): Promise<MarkdownSummary> {
const filePath = await this.filePath() const filePath = await this.filePath()
const writeFunc = overwrite ? writeFile : appendFile const writeFunc = overwrite ? writeFile : appendFile
await writeFunc(filePath, this.buffer, {encoding: 'utf8'}) await writeFunc(filePath, this._buffer, {encoding: 'utf8'})
return this.clearBuffer() return this.emptyBuffer()
}
/**
* Returns the current summary buffer as a string
*
* @returns {string} string of summary buffer
*/
stringify(): string {
return this._buffer
} }
/** /**
@ -93,7 +104,7 @@ export class MarkdownSummary {
* @returns {boolen} true if the buffer is empty * @returns {boolen} true if the buffer is empty
*/ */
isEmptyBuffer(): boolean { isEmptyBuffer(): boolean {
return this.buffer.length === 0 return this._buffer.length === 0
} }
/** /**
@ -101,8 +112,8 @@ export class MarkdownSummary {
* *
* @returns {MarkdownSummary} markdown summary instance * @returns {MarkdownSummary} markdown summary instance
*/ */
clearBuffer(): MarkdownSummary { emptyBuffer(): MarkdownSummary {
this.buffer = '' this._buffer = ''
return this return this
} }
@ -114,7 +125,7 @@ export class MarkdownSummary {
* @returns {MarkdownSummary} markdown summary instance * @returns {MarkdownSummary} markdown summary instance
*/ */
add(text: string): MarkdownSummary { add(text: string): MarkdownSummary {
this.buffer += text this._buffer += text
return this return this
} }
@ -200,4 +211,54 @@ export class MarkdownSummary {
const element = this.wrap('details', this.wrap('summary', label) + content) const element = this.wrap('details', this.wrap('summary', label) + content)
return this.add(element).addEOL() return this.add(element).addEOL()
} }
/**
* Adds an HTML image tag to the summary buffer
*
* @param {string} src path to the image you to embed
* @param {string} alt text description of the image
*
* @returns {MarkdownSummary} markdown summary instance
*/
addImage(src: string, alt: string): MarkdownSummary {
const element = this.wrap('img', '', {src, alt})
return this.add(element).addEOL()
}
/**
* Adds an HTML section heading element
*
* @param {string} text path to the image you to embed
* @param {number} [level=1] (optional) the heading level, default: 1
*
* @returns {MarkdownSummary} markdown summary instance
*/
addHeading(text: string, n = 1): MarkdownSummary {
const tag = [1, 2, 3, 4, 5, 6].includes(n) ? `h${n}` : 'h1'
const element = this.wrap(tag, text)
return this.add(element).addEOL()
}
/**
* Adds an HTML thematic break (<hr>) to the summary buffer
*
* @returns {MarkdownSummary} markdown summary instance
*/
addSeparator(): MarkdownSummary {
const element = this.wrap('hr', '')
return this.add(element).addEOL()
}
/**
* Adds an HTML blockquote to the summary buffer
*
* @returns {MarkdownSummary} markdown summary instance
*/
addQuote(text: string, cite?: string): MarkdownSummary {
const attrs = {
...(cite && {cite})
}
const element = this.wrap('blockquote', text, attrs)
return this.add(element).addEOL()
}
} }