mirror of
https://github.com/discourse/discourse-table-builder.git
synced 2025-02-21 02:45:50 +00:00
FIX: Special character alignment issue (#10)
* DEV: Use alternate fork of `array-to-table` * DEV: Add unit test for `arrayToTable()` method * Move `arrayToTable` so it is available for unit tests * DEV: Add assertion for creating a markdown table with special chars When a table with special characters (such as `$`) is inserted in a cell to the right of a blank cell. The alignment of the table gets ruined if the table doesn't have surrounding pipes. This check ensures this alignment is correct. Co-authored-by: Penar Musaraj <pmusaraj@gmail.com>
This commit is contained in:
parent
dacdd1cc4e
commit
7b37c6acc1
@ -3,45 +3,41 @@
|
|||||||
/**
|
/**
|
||||||
* Generate markdown table from an array of objects
|
* Generate markdown table from an array of objects
|
||||||
*
|
*
|
||||||
* @see {@link https://github.com/Nijikokun/array-to-table|GitHub}:
|
* @see {@link https://github.com/Ygilany/array-to-table|GitHub}:
|
||||||
*
|
*
|
||||||
* @param {Array} array Array of objects
|
* @param {Array} array Array of objects
|
||||||
* @param {Array} columns Optional, table column names, otherwise taken from the keys of the first object
|
* @param {String} columns Optional, table column names, otherwise taken from the keys of the first object
|
||||||
* @param {String} alignment Optional table alignment. Can be 'center' (default), 'left' or 'right'
|
|
||||||
*
|
*
|
||||||
* @return {String} Markdown table
|
* @return {String} Markdown table
|
||||||
*/
|
*/
|
||||||
export function arrayToTable(array, columns, alignment = "center") {
|
export function arrayToTable(array, columns) {
|
||||||
var table = "";
|
var table = "";
|
||||||
var separator = {
|
|
||||||
left: ":---",
|
|
||||||
right: "---:",
|
|
||||||
center: "---",
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate column list
|
// Generate column list
|
||||||
var cols = columns ? columns.split(",") : Object.keys(array[0]);
|
var cols = columns ? columns.split(",") : Object.keys(array[0]);
|
||||||
|
|
||||||
// Generate table headers
|
// Generate table headers
|
||||||
|
table += "|";
|
||||||
table += cols.join(" | ");
|
table += cols.join(" | ");
|
||||||
table += "\r\n";
|
table += "|\r\n|";
|
||||||
|
|
||||||
// Generate table header seperator
|
// Generate table header seperator
|
||||||
table += cols
|
table += cols
|
||||||
.map(function () {
|
.map(function () {
|
||||||
return separator[alignment] || separator.center;
|
return "---";
|
||||||
})
|
})
|
||||||
.join(" | ");
|
.join(" | ");
|
||||||
table += "\r\n";
|
table += "|\r\n";
|
||||||
|
|
||||||
// Generate table body
|
// Generate table body
|
||||||
array.forEach(function (item) {
|
array.forEach(function (item) {
|
||||||
|
table += "|";
|
||||||
table +=
|
table +=
|
||||||
cols
|
cols
|
||||||
.map(function (key) {
|
.map(function (key) {
|
||||||
return String(item[key] || "");
|
return String(item[key] || "");
|
||||||
})
|
})
|
||||||
.join(" | ") + "\r\n";
|
.join(" | ") + "|\r\n";
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return table
|
// Return table
|
@ -9,7 +9,7 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
|
|||||||
import Session from "discourse/models/session";
|
import Session from "discourse/models/session";
|
||||||
import loadScript from "discourse/lib/load-script";
|
import loadScript from "discourse/lib/load-script";
|
||||||
import PrettyText, { buildOptions } from "pretty-text/pretty-text";
|
import PrettyText, { buildOptions } from "pretty-text/pretty-text";
|
||||||
import { tokenRange } from "../lib/utilities";
|
import { tokenRange } from "../discourse-table-builder/lib/utilities";
|
||||||
export default apiInitializer("0.11.1", (api) => {
|
export default apiInitializer("0.11.1", (api) => {
|
||||||
const site = api.container.lookup("site:main"),
|
const site = api.container.lookup("site:main"),
|
||||||
siteSettings = api.container.lookup("site-settings:main");
|
siteSettings = api.container.lookup("site-settings:main");
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
import { action } from "@ember/object";
|
import { action } from "@ember/object";
|
||||||
import loadScript from "discourse/lib/load-script";
|
import loadScript from "discourse/lib/load-script";
|
||||||
import { arrayToTable, findTableRegex, tokenRange } from "../lib/utilities";
|
import {
|
||||||
|
arrayToTable,
|
||||||
|
findTableRegex,
|
||||||
|
tokenRange,
|
||||||
|
} from "../discourse-table-builder/lib/utilities";
|
||||||
import GlimmerComponent from "discourse/components/glimmer";
|
import GlimmerComponent from "discourse/components/glimmer";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { popupAjaxError } from "discourse/lib/ajax-error";
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||||
|
1
test/fixtures/md-table-fixture.js
vendored
Normal file
1
test/fixtures/md-table-fixture.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default `|Make | Model | Year|\r\n|--- | --- | ---|\r\n|Toyota | Supra | 1998|\r\n|Nissan | Skyline | 1999|\r\n|Honda | S2000 | 2001|\r\n`;
|
1
test/fixtures/md-table-special-chars-fixture.js
vendored
Normal file
1
test/fixtures/md-table-special-chars-fixture.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default `|Make | Model | Price|\r\n|--- | --- | ---|\r\n|Toyota | Supra | $50,000|\r\n| | Celica | $20,000|\r\n|Nissan | GTR | $80,000|\r\n`;
|
57
test/unit/lib/utilities-test.js
Normal file
57
test/unit/lib/utilities-test.js
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
|
||||||
|
import { test } from "qunit";
|
||||||
|
import mdTableFixture from "../../fixtures/md-table-fixture";
|
||||||
|
import mdTableSpecialCharsFixture from "../../fixtures/md-table-special-chars-fixture";
|
||||||
|
import { arrayToTable } from "../../../discourse-table-builder/lib/utilities";
|
||||||
|
|
||||||
|
discourseModule("Unit | Utilities", function () {
|
||||||
|
test("arrayToTable", function (assert) {
|
||||||
|
const tableData = [
|
||||||
|
{
|
||||||
|
Make: "Toyota",
|
||||||
|
Model: "Supra",
|
||||||
|
Year: "1998",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Make: "Nissan",
|
||||||
|
Model: "Skyline",
|
||||||
|
Year: "1999",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Make: "Honda",
|
||||||
|
Model: "S2000",
|
||||||
|
Year: "2001",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
arrayToTable(tableData),
|
||||||
|
mdTableFixture,
|
||||||
|
"it creates a markdown table from an array of objects (with headers as keys)"
|
||||||
|
);
|
||||||
|
|
||||||
|
const specialCharsTableData = [
|
||||||
|
{
|
||||||
|
Make: "Toyota",
|
||||||
|
Model: "Supra",
|
||||||
|
Price: "$50,000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Make: "",
|
||||||
|
Model: "Celica",
|
||||||
|
Price: "$20,000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Make: "Nissan",
|
||||||
|
Model: "GTR",
|
||||||
|
Price: "$80,000",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
arrayToTable(specialCharsTableData),
|
||||||
|
mdTableSpecialCharsFixture,
|
||||||
|
"it creates a markdown table with special characters in correct alignment"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user