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

Fixed issue where Markdown syntax would cause table columns to have incorrect lengths #53

Open
wants to merge 2 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"dependencies": {
"ebnf": "1.9.0",
"lodash": "4.17.20",
"meaw": "5.0.0"
"meaw": "5.0.0",
"remove-markdown": "^0.5.0"
}
}
20 changes: 16 additions & 4 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Table } from './table';
import { TableCell } from './table-cell';
import { TableRow } from './table-row';
import { getEAW } from 'meaw';
const removeMd = require('remove-md');

export interface FormattedTable {
/**
Expand Down Expand Up @@ -209,7 +210,10 @@ export const _computeTextWidth = (
text: string,
options: TextWidthOptions,
): number => {
const normalized = options.normalize ? text.normalize('NFC') : text;
const unformattedText = removeMd(text);
const normalized = options.normalize
? unformattedText.normalize('NFC')
: unformattedText;
let w = 0;
for (const char of normalized) {
if (options.wideChars.has(char)) {
Expand Down Expand Up @@ -276,6 +280,14 @@ export const _alignText = (
*/
export const _padText = (text: string): string => ` ${text} `;

/**
* Returns true if markdown string contains inline link, else false
*
* @private
*/
const _detectMdLink = (text: string): boolean => /\[([^\]]*?)\][\[\(]https?:\/\/.*?[\]\)]/g.test(text);


/**
* Formats a table.
*
Expand Down Expand Up @@ -347,7 +359,7 @@ export const _formatTable = (
_padText(
_alignText(
cell.content,
columnWidths[j],
_detectMdLink (cell.content) ? columnWidths[j] : columnWidths[j] + 2,
options.headerAlignment === HeaderAlignment.FOLLOW
? alignments[j] === Alignment.NONE
? options.defaultAlignment
Expand All @@ -370,7 +382,7 @@ export const _formatTable = (
.getCells()
.map(
(cell, j) =>
new TableCell(_delimiterText(alignments[j], columnWidths[j])),
new TableCell(_delimiterText(alignments[j], _detectMdLink(cell.content) ? columnWidths[j] : columnWidths[j] + 2)),
),
marginLeft,
'',
Expand All @@ -390,7 +402,7 @@ export const _formatTable = (
_padText(
_alignText(
cell.content,
columnWidths[j],
_detectMdLink(cell.content) ? columnWidths[j] : columnWidths[j] + 2,
alignments[j] === Alignment.NONE
? options.defaultAlignment
: alignments[j],
Expand Down