Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 7, 2021
1 parent 0e75cdb commit 1feab0b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
68 changes: 33 additions & 35 deletions dev/lib/html.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/**
* @typedef {import('micromark-util-types').HtmlExtension} HtmlExtension
*/

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

const alignment = {
null: '',
none: '',
left: ' align="left"',
right: ' align="right"',
center: ' align="center"'
Expand All @@ -17,21 +14,24 @@ const alignment = {
export const gfmTableHtml = {
enter: {
table(token) {
/** @type {Array<Align>} */
// @ts-expect-error Custom.
const tableAlign = token._align
this.lineEndingIfNeeded()
this.tag('<table>')
// @ts-expect-error Custom.
this.setData('tableAlign', token._align)
this.setData('tableAlign', tableAlign)
},
tableBody() {
// Clear slurping line ending from the delimiter row.
this.setData('slurpOneLineEnding')
this.tag('<tbody>')
},
tableData() {
/** @type {string|undefined} */
const align =
// @ts-expect-error Custom.
alignment[this.getData('tableAlign')[this.getData('tableColumn')]]
const tableAlign = /** @type {Array<Align>} */ (
this.getData('tableAlign')
)
const tableColumn = /** @type {number} */ (this.getData('tableColumn'))
const align = alignment[tableAlign[tableColumn]]

if (align === undefined) {
// Capture results to ignore them.
Expand All @@ -46,13 +46,14 @@ export const gfmTableHtml = {
this.tag('<thead>')
},
tableHeader() {
this.lineEndingIfNeeded()
this.tag(
'<th' +
// @ts-expect-error Custom.
alignment[this.getData('tableAlign')[this.getData('tableColumn')]] +
'>'
const tableAlign = /** @type {Array<Align>} */ (
this.getData('tableAlign')
)
const tableColumn = /** @type {number} */ (this.getData('tableColumn'))
const align = alignment[tableAlign[tableColumn]]

this.lineEndingIfNeeded()
this.tag('<th' + align + '>')
},
tableRow() {
this.setData('tableColumn', 0)
Expand Down Expand Up @@ -85,14 +86,14 @@ export const gfmTableHtml = {
this.tag('</tbody>')
},
tableData() {
/** @type {number} */
// @ts-expect-error Custom.
const column = this.getData('tableColumn')
const tableAlign = /** @type {Array<Align>} */ (
this.getData('tableAlign')
)
const tableColumn = /** @type {number} */ (this.getData('tableColumn'))

// @ts-expect-error Custom.
if (column in this.getData('tableAlign')) {
if (tableColumn in tableAlign) {
this.tag('</td>')
this.setData('tableColumn', column + 1)
this.setData('tableColumn', tableColumn + 1)
} else {
// Stop capturing.
this.resume()
Expand All @@ -105,26 +106,23 @@ export const gfmTableHtml = {
// Slurp the line ending from the delimiter row.
},
tableHeader() {
const tableColumn = /** @type {number} */ (this.getData('tableColumn'))
this.tag('</th>')
// @ts-expect-error Custom.
this.setData('tableColumn', this.getData('tableColumn') + 1)
this.setData('tableColumn', 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')
const tableAlign = /** @type {Array<Align>} */ (
this.getData('tableAlign')
)
let tableColumn = /** @type {number} */ (this.getData('tableColumn'))

while (column < align.length) {
while (tableColumn < tableAlign.length) {
this.lineEndingIfNeeded()
// @ts-expect-error `null` is fine as an index.
this.tag('<td' + alignment[align[column]] + '></td>')
column++
this.tag('<td' + alignment[tableAlign[tableColumn]] + '></td>')
tableColumn++
}

this.setData('tableColumn', column)
this.setData('tableColumn', tableColumn)
this.lineEndingIfNeeded()
this.tag('</tr>')
}
Expand Down
6 changes: 3 additions & 3 deletions dev/lib/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/**
* @typedef {'left'|'center'|'right'|null} Align
* @typedef {'left'|'center'|'right'|'none'} Align
*/

import {ok as assert} from 'uvu/assert'
Expand Down Expand Up @@ -159,7 +159,7 @@ function resolveTable(events, context) {
/** @type {Tokenizer} */
function tokenizeTable(effects, ok, nok) {
const self = this
/** @type {Align[]} */
/** @type {Array<Align>} */
const align = []
let tableHeaderCount = 0
/** @type {boolean|undefined} */
Expand Down Expand Up @@ -305,7 +305,7 @@ function tokenizeTable(effects, ok, nok) {
effects.enter('tableDelimiterFiller')
effects.consume(code)
hasDash = true
align.push(null)
align.push('none')
return inFillerDelimiter
}

Expand Down

0 comments on commit 1feab0b

Please sign in to comment.