Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 7, 2021
1 parent 3470156 commit 14f4f11
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
27 changes: 27 additions & 0 deletions dev/lib/html.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
/**
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
*/

/**
* @typedef {import('./syntax.js').Align} Align
*/

const alignment = {
null: '',
left: ' align="left"',
right: ' align="right"',
center: ' align="center"'
}

/** @type {HtmlExtension} */
export const gfmTableHtml = {
enter: {
table(token) {
this.lineEndingIfNeeded()
this.tag('<table>')
// @ts-expect-error Custom.
this.setData('tableAlign', token._align)
},
tableBody() {
Expand All @@ -18,7 +28,9 @@ export const gfmTableHtml = {
this.tag('<tbody>')
},
tableData() {
/** @type {string|undefined} */
const align =
// @ts-expect-error Custom.
alignment[this.getData('tableAlign')[this.getData('tableColumn')]]

if (align === undefined) {
Expand All @@ -37,6 +49,7 @@ export const gfmTableHtml = {
this.lineEndingIfNeeded()
this.tag(
'<th' +
// @ts-expect-error Custom.
alignment[this.getData('tableAlign')[this.getData('tableColumn')]] +
'>'
)
Expand Down Expand Up @@ -72,8 +85,11 @@ export const gfmTableHtml = {
this.tag('</tbody>')
},
tableData() {
/** @type {number} */
// @ts-expect-error Custom.
const column = this.getData('tableColumn')

// @ts-expect-error Custom.
if (column in this.getData('tableAlign')) {
this.tag('</td>')
this.setData('tableColumn', column + 1)
Expand All @@ -90,14 +106,20 @@ export const gfmTableHtml = {
},
tableHeader() {
this.tag('</th>')
// @ts-expect-error Custom.
this.setData('tableColumn', this.getData('tableColumn') + 1)
},
tableRow() {
/** @type {Align[]} */
// @ts-expect-error Custom.
const align = this.getData('tableAlign')
/** @type {number} */
// @ts-expect-error Custom.
let column = this.getData('tableColumn')

while (column < align.length) {
this.lineEndingIfNeeded()
// @ts-expect-error `null` is fine as an index.
this.tag('<td' + alignment[align[column]] + '></td>')
column++
}
Expand All @@ -109,6 +131,11 @@ export const gfmTableHtml = {
}
}

/**
* @param {string} $0
* @param {string} $1
* @returns {string}
*/
function replace($0, $1) {
// Pipes work, backslashes don’t (but can’t escape pipes).
return $1 === '|' ? $1 : $0
Expand Down
Loading

0 comments on commit 14f4f11

Please sign in to comment.