From 23533ac83fc173ed8fad602931266bbf761ef7e9 Mon Sep 17 00:00:00 2001 From: ElisabethJoan Date: Sun, 19 Feb 2023 23:50:51 +1000 Subject: [PATCH 1/2] Added MD Stripping to _computeTextWidth so table columns will have consistent widths when rows contain a mixture of MD formatting --- package.json | 3 ++- src/formatter.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6b4f63e..9fb9af6 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/formatter.ts b/src/formatter.ts index 9b35bd7..f33e70d 100644 --- a/src/formatter.ts +++ b/src/formatter.ts @@ -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 { /** @@ -209,8 +210,15 @@ 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; + // If text has inline link add 2 spaces to account for the link icon + if (/\[([^\]]*?)\][\[\(].*?[\]\)]/g.test(text)) { + w += 2; + } for (const char of normalized) { if (options.wideChars.has(char)) { w += 2; From d9b2fd43bc223047626964765574cf8c37231625 Mon Sep 17 00:00:00 2001 From: ElisabethJoan Date: Mon, 20 Feb 2023 22:57:04 +1000 Subject: [PATCH 2/2] Added Md Inline Link Detection and Handling to account for the link icon --- src/formatter.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/formatter.ts b/src/formatter.ts index f33e70d..608c301 100644 --- a/src/formatter.ts +++ b/src/formatter.ts @@ -215,10 +215,6 @@ export const _computeTextWidth = ( ? unformattedText.normalize('NFC') : unformattedText; let w = 0; - // If text has inline link add 2 spaces to account for the link icon - if (/\[([^\]]*?)\][\[\(].*?[\]\)]/g.test(text)) { - w += 2; - } for (const char of normalized) { if (options.wideChars.has(char)) { w += 2; @@ -284,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. * @@ -355,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 @@ -378,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, '', @@ -398,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],