mirror of https://github.com/usememos/memos
feat: support markdown table (#294)
* feat: support markdown table * chore: update table style * test: for markdown table parsepull/296/head
parent
6c53bd00f6
commit
3d0c6004c0
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* Match markdown table
|
||||||
|
* example:
|
||||||
|
* | a | b | c |
|
||||||
|
* |---|---|---|
|
||||||
|
* | 1 | 2 | 3 |
|
||||||
|
* | 4 | 5 | 6 |
|
||||||
|
*/
|
||||||
|
export const TABLE_REG = /^(\|.*\|)(?:(?:\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>`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: "table",
|
||||||
|
regex: TABLE_REG,
|
||||||
|
renderer,
|
||||||
|
};
|
Loading…
Reference in New Issue