Skip to content

Commit

Permalink
feat: support markdown tables
Browse files Browse the repository at this point in the history
issue: #95
  • Loading branch information
hosseinmd committed Mar 23, 2021
1 parent 5ae99f2 commit 635299a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/descriptionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const NEW_PARAGRAPH_START_THREE_SPACE_SIGNATURE =
"2@^5!~#sdE!_NEW_PARAGRAPH_START_THREE_SPACE_SIGNATURE";
const CODE = "2@^5!~#sdE!_CODE";
const CODE_INDENTED = "2@^5!~#sdE!_CODE_INDENTED";
const TABLE = "2@^5!~#sdE!_TABLE";

interface DescriptionEndLineParams {
description: string;
Expand Down Expand Up @@ -75,6 +76,14 @@ function formatDescription(
},
);

const tables: string[] = [];
text = text.replace(/((\n|^)\|[\s\S]*?)((\n[^|])|$)/g, (code, _1, _2, _3) => {
code = _3 ? code.slice(0, -1) : code;

tables.push(code);
return `\n\n${TABLE}\n\n${_3 ? _3.slice(1) : ""}`;
});

/**
* Description
*
Expand Down Expand Up @@ -219,6 +228,19 @@ function formatDescription(
}, "");
}

if (tables.length > 0) {
text = text.split(TABLE).reduce((pre, cur, index) => {
let result = tables?.[index] || "";
if (result) {
result = format(result, {
...options,
parser: "markdown",
}).trim();
}
return `${pre}${cur.trim()}${result ? `\n\n${result}\n\n` : ""}`;
}, "");
}

text = text.trim().slice(tagStringLength);

return text;
Expand Down
30 changes: 30 additions & 0 deletions tests/__snapshots__/descriptions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ exports[`Long words 1`] = `
"
`;

exports[`Markdown Table 1`] = `
"/**
* Description
*
* | A | B | C |
* | --- | --- | --- |
* | C | V | B |
* | 1 | 2 | 3 |
*
* Description
*
* | A| B |C |
* |C | V | B |
* |1|2|3|
*
* End
*/
"
`;

exports[`Markdown Table 2`] = `
"/**
* | A | B | C |
* | --- | --- | --- |
* | C | V | B |
* | 1 | 2 | 3 |
*/
"
`;

exports[`New Lines with star 1`] = `
"/**
* Simplifies the token stream to ease the matching with the expected token stream.
Expand Down
40 changes: 34 additions & 6 deletions tests/descriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,39 @@ test("Long words", () => {
expect(result2).toMatchSnapshot();
});

test("Markdown Table", () => {
const result1 = subject(
`
/**
* If this is a vertical ScrollView scrolls to the bottom.
* If this is a horizontal ScrollView scrolls to the right.
*
* Use `scrollToEnd({ animated: true })` for smooth animated scrolling,
* `scrollToEnd({ animated: false })` for immediate scrolling.
* If no options are passed, `animated` defaults to true.
* description
* | A| B |C |
* | - | - | - |
* |C | V | B |
* |1|2|3|
*
* description
*
*
* | A| B |C |
* |C | V | B |
* |1|2|3|
* end
*/
`,
);

expect(result1).toMatchSnapshot();

const result2 = subject(
`
/**
* | A| B |C |
* | - | - | - |
* |C | V | B |
* |1|2|3|
*/
`,
);

expect(result2).toMatchSnapshot();
});

0 comments on commit 635299a

Please sign in to comment.