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

feat: tables support #2573

Merged
merged 10 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 60 additions & 0 deletions web/src/labs/marked/parser/Table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { CSSProperties } from "react";
import { inlineElementParserList } from ".";
import { marked } from "..";
import { matcher } from "../matcher";

export const TABLE_REG = /^((?:\|[^|\r\n]+)+)\|\r?\n((?:[ -:]*\|[ -:]*)+)((?:\r?\n\|[^\r\n]+)+)/;
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

const splitMarkdownTablePipes = (lineString: string) => {
// should take care of escaped pipes, like
// | aaaa | bbbb | cc\|cc |
// will return:
// ["aaaa", "bbbb", "cc|cc"]
return (lineString.match(/(?:\\\||[^|])+/g) || []).map((s) => s.replaceAll("\\|", "|"));
};

const renderer = (rawStr: string) => {
const matchResult = matcher(rawStr, TABLE_REG);
if (!matchResult) {
return rawStr;
}
const headerContents = splitMarkdownTablePipes(matchResult[1]);
const columnStyles: CSSProperties[] = splitMarkdownTablePipes(matchResult[2]).map((cell) => {
const left = cell.trim().startsWith(":");
const right = cell.trim().endsWith(":");
// github markdown spec says that by default, content is left aligned
return {
textAlign: left && right ? "center" : right ? "right" : "left",
};
});
const bodyContents = matchResult[3].split(/\r?\n/).map(splitMarkdownTablePipes);

return (
<table>
<thead>
<tr>
{headerContents.map((content, index) => (
<th key={"th-" + index}>{marked(content, [], inlineElementParserList)}</th>
))}
</tr>
</thead>
<tbody>
{bodyContents.map((bodyLine, bodyIndex) => (
<tr key={"tr-" + bodyIndex}>
{bodyLine.map((content, contentIndex) => (
<td key={"td-" + bodyIndex + "-" + contentIndex} style={contentIndex < columnStyles.length ? columnStyles[contentIndex] : {}}>
{marked(content, [], inlineElementParserList)}
</td>
))}
</tr>
))}
</tbody>
</table>
);
};

export default {
name: "table",
regexp: TABLE_REG,
renderer,
};
2 changes: 2 additions & 0 deletions web/src/labs/marked/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Paragraph from "./Paragraph";
import PlainLink from "./PlainLink";
import PlainText from "./PlainText";
import Strikethrough from "./Strikethrough";
import Table from "./Table";
import Tag from "./Tag";
import TodoList from "./TodoList";
import UnorderedList from "./UnorderedList";
Expand All @@ -32,6 +33,7 @@ export const blockElementParserList = [
CodeBlock,
Blockquote,
Heading,
Table,
TodoList,
DoneList,
OrderedList,
Expand Down