mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
979 B
TypeScript
46 lines
979 B
TypeScript
/**
|
|
* 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,
|
|
};
|