Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix vertical bar within backtick within a table not escape issue #379

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/rules_block/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,45 @@ export default function table(state, startLine, endLine, silent) {

lineText = getLine(state, nextLine).trim();
if (lineText.indexOf('|') === -1) { break; }

// fix vertical bar within backtick within a table not escape issue
// e.g. | `a | b` | => | <code>a &#124; b</code> |
var inBacktickBlock = false
var lineTextCurArray = []
var lineTextCharArray = lineText.split('')

lineTextCharArray.forEach(function(item, index){
// first | char
if (index === 0 && item === '|') return lineTextCurArray.push(item)
// last | char
if (index === lineTextCharArray.length - 1 && item === '|') return lineTextCurArray.push(item)

// in backtick block
if (item === '`') {
if (inBacktickBlock) {
inBacktickBlock = false
lineTextCurArray.push('</code>')
} else {
inBacktickBlock = true
lineTextCurArray.push('<code>')
}
return
}

if (item === '|') {
if (inBacktickBlock) {
lineTextCurArray.push('&#124;')
return
} else {
lineTextCurArray.push(item)
return
}
}

lineTextCurArray.push(item)
})

lineText = lineTextCurArray.join('')
rows = lineText.replace(/^\||\|$/g, '').split('|');

state.tokens.push({ type: 'tr_open', level: state.level++ });
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/remarkable/tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,23 @@ Minimal one-column table test:
</tbody>
</table>
.

escape `|` char within backtick within table test:

.
| foo | bar |
| --- | --- |
| `a | b` | `a || b` |
| a | b |

.
<table>
<thead>
<tr><th>foo</th><th>bar</th></tr>
</thead>
<tbody>
<tr><td><code>a | b</code></td><td><code>a || b</code></td></tr>
<tr><td>a</td><td>b</td></tr>
</tbody>
</table>
.