Skip to content

Commit

Permalink
Fix line after table, marked#1439; fix tables in a list, marked#1446
Browse files Browse the repository at this point in the history
  • Loading branch information
yahtnif committed May 6, 2019
1 parent e82ff1f commit 7e9dfe8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 21 deletions.
70 changes: 70 additions & 0 deletions __tests__/base/table
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ Cell 1 |Cell 2 |Cell 3 |Cell 4
-|-
1\|\\|2\|\\

|1|2|
|-|-|
|1|2\||

* Table in list:

| column1 | column2 |
|---------|---------|
| value1 | value2 |
| value3 | value4 |

* No leading pipe table in list:

column1 | column2
--------|--------
value1 | value2
value3 | value4



<table>
Expand Down Expand Up @@ -117,3 +135,55 @@ Cell 1 |Cell 2 |Cell 3 |Cell 4
</tr>
</tbody></table>

<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>2|</td>
</tr>
</tbody></table>
<ul>
<li><p>Table in list:</p>

<table>
<thead>
<tr>
<th>column1</th>
<th>column2</th>
</tr>
</thead>
<tbody><tr>
<td>value1</td>
<td>value2</td>
</tr>
<tr>
<td>value3</td>
<td>value4</td>
</tr>
</tbody></table>
</li>
<li><p>No leading pipe table in list:</p>

<table>
<thead>
<tr>
<th>column1</th>
<th>column2</th>
</tr>
</thead>
<tbody><tr>
<td>value1</td>
<td>value2</td>
</tr>
<tr>
<td>value3</td>
<td>value4</td>
</tr>
</tbody></table>
</li>
</ul>
30 changes: 16 additions & 14 deletions src/block-lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ export class BlockLexer {
const newRulesTop: BlockRule[] = newRules
.filter(R => R.options.priority)
.sort((a, b) => b.options.priority - a.options.priority)
const newRulesBottom: BlockRule[] = newRules.filter(R => !R.options.priority)
const newRulesBottom: BlockRule[] = newRules.filter(
R => !R.options.priority
)

mainLoop: while (nextPart) {
// newline
Expand Down Expand Up @@ -367,7 +369,7 @@ export class BlockLexer {
align: execArr[2]
.replace(/^ *|\| *$/g, '')
.split(/ *\| */) as Align[],
cells: execArr[3] ? execArr[3].replace(/\n$/, '').split('\n') : []
cells: []
}

if (item.header.length === item.align.length) {
Expand All @@ -385,10 +387,12 @@ export class BlockLexer {
}
}

let td: string[] = execArr[3].replace(/\n$/, '').split('\n')
const cells = execArr[3]
? execArr[3].replace(/\n$/, '').split('\n')
: []

for (let i = 0; i < td.length; i++) {
item.cells[i] = this.splitCells(td[i], item.header.length)
for (let i = 0; i < cells.length; i++) {
item.cells[i] = this.splitCells(cells[i], item.header.length)
}

this.tokens.push(item)
Expand Down Expand Up @@ -578,9 +582,7 @@ export class BlockLexer {
align: execArr[2]
.replace(/^ *|\| *$/g, '')
.split(/ *\| */) as Align[],
cells: execArr[3]
? execArr[3].replace(/(?: *\| *)?\n$/, '').split('\n')
: []
cells: []
}

if (item.header.length === item.align.length) {
Expand All @@ -598,13 +600,13 @@ export class BlockLexer {
}
}

const td: string[] = execArr[3]
.replace(/(?: *\| *)?\n$/, '')
.split('\n')
const cells = execArr[3]
? execArr[3].replace(/\n$/, '').split('\n')
: []

for (let i = 0; i < td.length; i++) {
for (let i = 0; i < cells.length; i++) {
item.cells[i] = this.splitCells(
td[i].replace(/^ *\| *| *\| *$/g, ''),
cells[i].replace(/^ *\| *| *\| *$/g, ''),
item.header.length
)
}
Expand Down Expand Up @@ -679,7 +681,7 @@ export class BlockLexer {
return { tokens: this.tokens, links: this.links }
}

private splitCells(tableRow: string, count?: number) {
private splitCells(tableRow: string, count?: number): string[] {
// ensure that every cell-delimiting pipe has a space
// before it to distinguish it from an escaped pipe
let row: string = tableRow.replace(/\|/g, function(match, offset, str) {
Expand Down
26 changes: 19 additions & 7 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ExtraBlockRules extends GfmBlockRules {

export type BlockRulesTypes = BaseBlockRules | GfmBlockRules | ExtraBlockRules

export type BlockRulesType = keyof(BlockRulesTypes)
export type BlockRulesType = keyof (BlockRulesTypes)

export interface Link {
href: string
Expand Down Expand Up @@ -68,14 +68,14 @@ export enum TokenType {
paragraph,
space,
table,
text,
text
}

export type Align = 'center' | 'left' | 'right'

export interface Token {
align?: Align[]
cells?: string[] | string[][]
cells?: string[][]
depth?: number
ends?: string
escaped?: boolean
Expand Down Expand Up @@ -133,9 +133,14 @@ export interface ExtraInlineRules extends BreaksInlineRules {
fnref: RegExp
}

export type InlineRulesTypes = BaseInlineRules | PedanticInlineRules | GfmInlineRules | BreaksInlineRules | ExtraInlineRules
export type InlineRulesTypes =
| BaseInlineRules
| PedanticInlineRules
| GfmInlineRules
| BreaksInlineRules
| ExtraInlineRules

export type InlineRulesType = keyof(InlineRulesTypes)
export type InlineRulesType = keyof (InlineRulesTypes)

export class Options {
baseUrl?: string = null
Expand Down Expand Up @@ -165,10 +170,17 @@ export class Options {
xhtml?: boolean = false
escape?: (html: string, encode?: boolean) => string = escape
unescape?: (html: string) => string = unescape
slug?: (str: string, isUnique?: boolean) => string = (str: string, isUnique?: boolean): string => slugger.slug(str, isUnique)
slug?: (str: string, isUnique?: boolean) => string = (
str: string,
isUnique?: boolean
): string => slugger.slug(str, isUnique)
rtrim?: (str: string, c: string, invert?: boolean) => string = rtrim
resolveUrl?: (base: string, href: string) => string = resolveUrl
cleanUrl?: (sanitize: boolean, base: string, href: string) => string = cleanUrl
cleanUrl?: (
sanitize: boolean,
base: string,
href: string
) => string = cleanUrl
/**
* If set to `true`, an inline text will not be taken in paragraph.
*
Expand Down

0 comments on commit 7e9dfe8

Please sign in to comment.