chore: remove table syntax (#669)

This commit is contained in:
boojack 2022-12-04 10:52:11 +08:00 committed by GitHub
parent 53cf6ebb79
commit 1ea74dfd0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1 additions and 95 deletions

View File

@ -157,43 +157,6 @@ console.log("hello world!")
expect(unescape(marked(t.markdown))).toBe(t.want);
}
});
test("parse table", () => {
const tests = [
{
markdown: `text above the table
| a | b | c |
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
text below the table
`,
want: `<p>text above the table</p>
<table>
<thead>
<tr>
<th>a</th><th>b</th><th>c</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr>
</tbody>
</table>
<p>text below the table</p>
`,
},
{
markdown: `| a | b | c |
| 1 | 2 | 3 |
| 4 | 5 | 6 |`,
want: `<p>| a | b | c |</p>
<p>| 1 | 2 | 3 |</p>
<p>| 4 | 5 | 6 |</p>`,
},
];
for (const t of tests) {
expect(unescape(marked(t.markdown))).toBe(t.want);
}
});
test("parse full width space", () => {
const tests = [
{

View File

@ -1,45 +0,0 @@
/**
* Match markdown table
* example:
* | a | b | c |
* |---|---|---|
* | 1 | 2 | 3 |
* | 4 | 5 | 6 |
*/
export const TABLE_REG = /^(\|.*\|)(?:(?:\n(?:\|-*)+\|))((?:\n\|.*\|)+)(\n?)/;
const renderer = (rawStr: string): string => {
const matchResult = rawStr.match(TABLE_REG);
if (!matchResult) {
return rawStr;
}
const tableHeader = matchResult[1]
.split("|")
.filter((str) => str !== "")
.map((str) => str.trim());
const tableBody = matchResult[2]
.trim()
.split("\n")
.map((str) =>
str
.split("|")
.filter((str) => str !== "")
.map((str) => str.trim())
);
return `<table>
<thead>
<tr>
${tableHeader.map((str) => `<th>${str}</th>`).join("")}
</tr>
</thead>
<tbody>
${tableBody.map((row) => `<tr>${row.map((str) => `<td>${str}</td>`).join("")}</tr>`).join("")}
</tbody>
</table>${matchResult[3]}`;
};
export default {
name: "table",
regex: TABLE_REG,
renderer,
};

View File

@ -12,7 +12,6 @@ import Emphasis from "./Emphasis";
import PlainLink from "./PlainLink";
import InlineCode from "./InlineCode";
import PlainText from "./PlainText";
import Table from "./Table";
import BoldEmphasis from "./BoldEmphasis";
import Blockquote from "./Blockquote";
import HorizontalRules from "./HorizontalRules";
@ -24,20 +23,9 @@ export { DONE_LIST_REG } from "./DoneList";
export { TAG_REG } from "./Tag";
export { IMAGE_REG } from "./Image";
export { LINK_REG } from "./Link";
export { TABLE_REG } from "./Table";
export { HORIZONTAL_RULES_REG } from "./HorizontalRules";
// The order determines the order of execution.
export const blockElementParserList = [
HorizontalRules,
Table,
CodeBlock,
Blockquote,
TodoList,
DoneList,
OrderedList,
UnorderedList,
Paragraph,
];
export const blockElementParserList = [HorizontalRules, CodeBlock, Blockquote, TodoList, DoneList, OrderedList, UnorderedList, Paragraph];
export const inlineElementParserList = [Image, BoldEmphasis, Bold, Emphasis, Link, InlineCode, PlainLink, Strikethrough, Tag, PlainText];
export const parserList = [...blockElementParserList, ...inlineElementParserList];