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 1 commit
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
55 changes: 55 additions & 0 deletions web/src/labs/marked/parser/Table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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]+)+)/;

Check failure on line 6 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Unnecessary escape character: \|

Check failure on line 6 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Unnecessary escape character: \:

Check failure on line 6 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Unnecessary escape character: \:
Fixed Show fixed Hide fixed

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("\\|", "|"));

Check failure on line 13 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Unnecessary escape character: \|

Check failure on line 13 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Replace `s` with `(s)`
}

Check failure on line 14 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Insert `;`

const renderer = (rawStr: string) => {
const matchResult = matcher(rawStr, TABLE_REG);
if (!matchResult) {
return rawStr;
}
const headerContents = splitMarkdownTablePipes(matchResult[1]);
const bodyClasses: CSSProperties[] = splitMarkdownTablePipes(matchResult[2]).map(cell => {

Check failure on line 22 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Replace `cell` with `(cell)`
let left = cell.trim().startsWith(":");

Check failure on line 23 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

'left' is never reassigned. Use 'const' instead
let right = cell.trim().endsWith(":");

Check failure on line 24 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

'right' is never reassigned. Use 'const' instead
// github markdown spec says that by default, content is left aligned
return {
textAlign: (left && right) ? "center" : (right ? "right" : "left")

Check failure on line 27 in web/src/labs/marked/parser/Table.tsx

View workflow job for this annotation

GitHub Actions / eslint-checks

Replace `(left·&&·right)·?·"center"·:·(right·?·"right"·:·"left")` with `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 < bodyClasses.length ? bodyClasses[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