-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added validation and markdown support for tables
- Loading branch information
Showing
13 changed files
with
231 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createMarkdownRenderer } from "vitepress"; | ||
|
||
const config = globalThis.VITEPRESS_CONFIG; | ||
|
||
export default await createMarkdownRenderer( | ||
config.srcDir, | ||
config.markdown, | ||
config.site.base, | ||
config.logger | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import isObject from "../utils/isObject"; | ||
import { Table } from "../types"; | ||
|
||
import parseTableColumn from "./parseTableColumn"; | ||
import parseTableRow from "./parseTableRow"; | ||
|
||
export default function parseTable(data: unknown) { | ||
const table: Table = { | ||
columns: {}, | ||
rows: [], | ||
}; | ||
|
||
if (!isObject(data)) { | ||
throw new TypeError("Tables must be an object."); | ||
} | ||
|
||
// Columns | ||
if (!("columns" in data)) { | ||
throw new TypeError('Tables must include a "columns" field.'); | ||
} else if (!isObject(data.columns)) { | ||
throw new TypeError('Table "columns" field must be an object.'); | ||
} else { | ||
for (const columnId in data.columns) { | ||
const column = data.columns[columnId]; | ||
|
||
table.columns[columnId] = parseTableColumn(columnId, column); | ||
} | ||
} | ||
|
||
// Rows | ||
if (!("rows" in data)) { | ||
throw new TypeError('Tables must include a "rows" field.'); | ||
} else if (!Array.isArray(data.rows)) { | ||
throw new TypeError('Table "rows" field must be an array.'); | ||
} else { | ||
for (let index = 0; index < data.rows.length; index++) { | ||
const row = data.rows[index]; | ||
|
||
table.rows.push(parseTableRow(index, row, table.columns)); | ||
} | ||
} | ||
|
||
return table; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import isObject from "../utils/isObject"; | ||
import { TableColumn } from "../types"; | ||
|
||
import markdown from "./markdown"; | ||
import parseTableValue from "./parseTableValue"; | ||
|
||
export default function parseTableColumn(id: string, data: unknown) { | ||
const column: Partial<TableColumn> = {}; | ||
|
||
if (!isObject(data)) { | ||
throw new TypeError(`Table column "${id}" must be an object.`); | ||
} | ||
|
||
// Column Name | ||
if (!("name" in data)) { | ||
throw new TypeError(`Table column "${id}" must include a "name" field.`); | ||
} else if (typeof data.name !== "string") { | ||
throw new TypeError(`The "name" field of column "${id}" must be a string.`); | ||
} | ||
|
||
column.name = markdown.renderInline(data.name); | ||
|
||
// Default Value | ||
if ("default" in data) { | ||
column.default = parseTableValue(data.default); | ||
} | ||
|
||
// Text Align | ||
if ("text_align" in data) { | ||
if ( | ||
typeof data.text_align !== "string" || | ||
(data.text_align !== "left" && data.text_align !== "center" && data.text_align !== "right") | ||
) { | ||
throw new TypeError( | ||
`The "text_align" field of column "${id}" must be "left", "center" or "right".` | ||
); | ||
} | ||
|
||
column.textAlign = data.text_align; | ||
} | ||
|
||
return column as TableColumn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import isObject from "../utils/isObject"; | ||
import { Table, TableRow } from "../types"; | ||
|
||
import parseTableValue from "./parseTableValue"; | ||
|
||
export default function parseTableRow(index: number, data: unknown, columns: Table["columns"]) { | ||
const row: TableRow = {}; | ||
|
||
if (!isObject(data)) { | ||
throw new TypeError(`Table row ${index} must be an object.`); | ||
} | ||
|
||
for (const columnId in data) { | ||
if (!(columnId in columns)) { | ||
throw new TypeError( | ||
`Table row ${index} contains a value for the non-existent column "${columnId}".` | ||
); | ||
} | ||
|
||
row[columnId] = parseTableValue(data[columnId]); | ||
} | ||
|
||
return row; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { TableValue } from "../types"; | ||
|
||
import markdown from "./markdown"; | ||
|
||
export default function parseTableValue(value: unknown) { | ||
if (typeof value === "boolean") { | ||
return value; | ||
} | ||
|
||
if (typeof value === "string") { | ||
return markdown.renderInline(value); | ||
} | ||
|
||
if (typeof value === "number") { | ||
return value; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
const list: TableValue = []; | ||
|
||
for (const item of value) { | ||
if (typeof item !== "string") { | ||
throw new TypeError(`Table list items must be strings.`); | ||
} | ||
|
||
list.push(markdown.renderInline(item)); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
throw new TypeError("Table value must be a boolean, number, string or array of strings."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isObject(value: unknown): value is object { | ||
return typeof value === "object" && value !== null && !Array.isArray(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.