-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ee562d
commit 08b2399
Showing
10 changed files
with
347 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
branches: 95 | ||
functions: 95 | ||
lines: 95 | ||
statements: 95 | ||
ts: true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { table, LB, CR } from './utils/common'; | ||
export * from './utils/common'; | ||
|
||
export type ListItems<T> = Array<T>; | ||
export type ListItem = { text: string; depth?: number }; | ||
export type TaskItem = { text: string; checked?: boolean }; | ||
|
||
export default class Markdown { | ||
LINES: string[]; | ||
|
||
constructor(title: string) { | ||
this.LINES = []; | ||
if (!title) { | ||
throw new TypeError('Must Provide a title for this markdown'); | ||
} | ||
this.header(title, 1); | ||
} | ||
|
||
tableOfContent(onTop = true) { | ||
let list = []; | ||
if (!this.LINES.length) return; | ||
|
||
this.header('Table of Contents', 2); | ||
|
||
for (const line of this.LINES) { | ||
if (line.startsWith('#')) { | ||
const depth = line.split('#').filter((c) => c === '').length - 1; | ||
const text = line.match(/^\#+\s?(.*)$/)?.[1]; | ||
if (!text && !depth) continue; | ||
list.push({ text, depth }); | ||
} | ||
} | ||
|
||
this.list(list); | ||
|
||
if (onTop) { | ||
const lng = this.LINES.length; | ||
const toc = this.LINES.splice(lng - 2, 2); | ||
this.LINES.splice(1, 0, ...toc); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
render() { | ||
return this.LINES.join(CR); | ||
} | ||
|
||
addLine(data = LB): this { | ||
this.LINES.push(`${data}`); | ||
return this; | ||
} | ||
|
||
paragraph(data: string | number): this { | ||
this.addLine(`${data}`); | ||
return this; | ||
} | ||
|
||
header(title: string, n = 1): this { | ||
this.addLine(`${'#'.repeat(n)} ${title}`); | ||
return this; | ||
} | ||
|
||
image(filepath: string, altText?: string): this { | ||
const str = `![${altText || filepath}](${filepath})`; | ||
this.addLine(str); | ||
return this; | ||
} | ||
|
||
table(columns = [], rows = [], fmtFnc?): this { | ||
this.addLine(table(columns, rows, fmtFnc)); | ||
return this; | ||
} | ||
|
||
list(items: ListItems<ListItem> = [], numbered = false): this { | ||
if (!Array.isArray(items)) { | ||
throw new TypeError( | ||
'List items should be an array of { text: string, depth: number}' | ||
); | ||
} | ||
|
||
let list = []; | ||
|
||
for (let i = 0; i < items.length; i++) { | ||
let parsed; | ||
const { text, depth } = items[i]; | ||
if (numbered) parsed = `${i + 1}. ${text}`; | ||
else { | ||
parsed = `${!depth ? '' : ' '.repeat(depth)}- ${text}`; | ||
} | ||
list.push(parsed); | ||
} | ||
|
||
this.addLine(list.join('\n')); | ||
|
||
return this; | ||
} | ||
|
||
tasks(items: ListItems<TaskItem> = []): this { | ||
if (!Array.isArray(items)) { | ||
throw new TypeError( | ||
'List items should be an array of { text: string, checked: boolean }' | ||
); | ||
} | ||
|
||
let list = []; | ||
|
||
for (let i = 0; i < items.length; i++) { | ||
const { text, checked } = items[i]; | ||
const parsed = `- [${checked ? 'X' : ' '}] ${text}`; | ||
list.push(parsed); | ||
} | ||
|
||
this.addLine(list.join('\n')); | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import tap from 'tap'; | ||
import { | ||
table, | ||
link, | ||
code, | ||
inlineCode, | ||
quote, | ||
italic, | ||
bold, | ||
} from '../src/utils/common'; | ||
|
||
tap.test('italic', (t) => { | ||
t.equal(italic('ok'), '*ok*'); | ||
t.equal(italic(null), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('bold', (t) => { | ||
t.equal(bold('ok'), '**ok**'); | ||
t.equal(bold(null), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('quote', (t) => { | ||
t.equal(quote('ok'), '\n> ok'); | ||
t.equal(quote(null), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('link', (t) => { | ||
const l = 'http://google.com'; | ||
const txt = 'link'; | ||
t.equal(link(txt, l), `[${txt}](${l})`); | ||
t.equal(link(null, null), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('code', (t) => { | ||
const codeBlock = "alert('x')"; | ||
t.equal(inlineCode(codeBlock), '`' + codeBlock + '`'); | ||
t.equal(inlineCode(null), ''); | ||
t.equal(code(codeBlock), '\n\n```\n' + codeBlock + '\n```'); | ||
t.equal( | ||
code(codeBlock, 'javascript'), | ||
'\n\n```javascript\n' + codeBlock + '\n```' | ||
); | ||
t.equal(code(null), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('table', (t) => { | ||
const headers = ['id', 'name']; | ||
const rows = ['1', 'Ajeje']; | ||
let out = '| id | name |\n'; | ||
out += '| --- | --- |\n'; | ||
out += '| 1 | Ajeje |'; | ||
t.equal(table(headers, rows), out); | ||
t.end(); | ||
}); |
Oops, something went wrong.