Skip to content

Commit

Permalink
chore(remark): normalize tables output
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Jun 15, 2024
1 parent a8f5385 commit 57aebaf
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 25 deletions.
44 changes: 44 additions & 0 deletions packages/typedoc-plugin-remark/lint.mdx.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { consola } from 'consola';
import { glob } from 'glob';
import { remark } from 'remark';
import remarkMdx from 'remark-mdx';
import { read } from 'to-vfile';

const timeStart = new Date().getTime();

let error = false;

consola.start(`Begin linting MDX...`);

lintMdx();

async function lintMdx() {
const mdFiles = await glob(`./test/out/**/*.md`);

if (mdFiles.length === 0) {
consola.error('No markdown files found');
throw new Error();
}

mdFiles.forEach(async (file) => {
const vfile = await read(file);
try {
await remark().use(remarkMdx).processSync(vfile);
} catch (e) {
error = true;
consola.error(`Error linting MDX on file ${file}`);
throw new Error(e);
}
});
}

process.on('exit', () => {
if (!error) {
consola.success(
`Finished linting MDX in ${(
(new Date().getTime() - timeStart) /
1000
).toFixed(2)} seconds`,
);
}
});
4 changes: 2 additions & 2 deletions packages/typedoc-plugin-remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"prepublishOnly": "npm run lint && npm run build",
"prebuild": "rm -rf dist && prebuild-options",
"build": "tsc",
"postbuild": "copyfiles --up 1 ./src/**/*.cjs ./dist/",
"postbuild": "copyfiles --up 1 ./src/**/*.cjs ./src/**/*.mjs ./dist/",
"pretest": "ts-node ./test/__scripts__/prepare.ts",
"test": "jest",
"test": "node ./lint.mdx.mjs && jest",
"test:update": "npm run build && npm run test -- -u"
},
"repository": {
Expand Down
23 changes: 23 additions & 0 deletions packages/typedoc-plugin-remark/src/normalize-tables.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Normalize tables to have a newline before and after the content of each cell.
*
* This is necessary to ensure that the content of each cell is properly formatted by `htmlTable` formatting option
* from typedoc-plugin-markdown is used.
*/
export default function () {
const compiler = this.compiler || this.Compiler;
this.compiler = (tree) => {
let content = compiler(tree);
content = content
.replace(/^\s*(<\/?(table|tr|th|td)>)/gm, (match, p1) => p1)
.replace(
/<td>\s*([\s\S]*?)\s*<\/td>/gm,
(match, p1) =>
`<td>\n\n${p1
.split('\n')
.map((line) => line.trim())
.join('\n')}\n\n</td>`,
);
return content;
};
}
8 changes: 5 additions & 3 deletions packages/typedoc-plugin-remark/src/options/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/**
* Describes the options declared by the plugin.
*
/*
* @privateRemarks
*
* THIS FILE IS AUTO GENERATED FROM THE OPTIONS CONFIG. DO NOT EDIT DIRECTLY
*
* @module
*/

/**
* Describes the options declared by the plugin.
*/
export interface PluginOptions {
/**
* An array of remark plugin names.
Expand Down
4 changes: 3 additions & 1 deletion packages/typedoc-plugin-remark/src/remark.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Parses contents with Remark
* - The remark eco-system is esm only, therefore we have to import its modules syncronously.
* - The remark eco-system is esm only, therefore we have to import its modules synchronously.
* - In addition we don't want Typescript to transpile this file.
*/
module.exports = {
Expand All @@ -17,6 +17,7 @@ module.exports = {
['remark-frontmatter', ['yaml']],
'remark-gfm',
'remark-mdx',
'./normalize-tables.mjs',
...userPlugins,
];
const promises = plugins.map(async (plugin) => {
Expand All @@ -37,6 +38,7 @@ module.exports = {
pluginRefs.forEach((pluginRef) => {
processor.use(pluginRef.pluginFn, pluginRef.options);
});

await processor.process(file);

writeSync(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,34 @@ exports[`Remark should parse globals page 1`] = `
#### Parameters
| Parameter | Type |
| --------- | -------- |
| \`param\` | \`string\` |
<table>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>
\`param\`
</td>
<td>
\`string\`
</td>
<td>
Default text content for \`@category\` tag.
\`\`\`ts
const x = 1;
\`\`\`
</td>
</tr>
</table>
#### Returns
Expand All @@ -51,9 +76,34 @@ exports[`Remark should parse globals page without toc 1`] = `
#### Parameters
| Parameter | Type |
| --------- | -------- |
| \`param\` | \`string\` |
<table>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>
\`param\`
</td>
<td>
\`string\`
</td>
<td>
Default text content for \`@category\` tag.
\`\`\`ts
const x = 1;
\`\`\`
</td>
</tr>
</table>
#### Returns
Expand All @@ -80,9 +130,18 @@ title: 'test'
## Type Parameters
| Type Parameter |
| -------------- |
| \`T\` |
<table>
<tr>
<th>Type Parameter</th>
</tr>
<tr>
<td>
\`T\`
</td>
</tr>
</table>
## Constructors
Expand Down Expand Up @@ -202,10 +261,18 @@ javascript
+ list item 1
#### Type Parameters
<table>
<tr>
<th>Type Parameter</th>
</tr>
<tr>
<td>
| Type Parameter |
| -------------- |
| \`T\` |
\`T\`
</td>
</tr>
</table>
#### Constructors
Expand All @@ -228,10 +295,18 @@ javascript
### SomeInterface\\<T>
#### Type Parameters
<table>
<tr>
<th>Type Parameter</th>
</tr>
<tr>
<td>
\`T\`
| Type Parameter |
| -------------- |
| \`T\` |
</td>
</tr>
</table>
#### Properties
Expand Down Expand Up @@ -260,10 +335,24 @@ javascript
> **some\\_function**(\`param\`): \`void\`
#### Parameters
<table>
<tr>
<th>Parameter</th>
<th>Type</th>
</tr>
<tr>
<td>
\`param\`
</td>
<td>
\`string\` | \`boolean\`
| Parameter | Type |
| --------- | --------------------- |
| \`param\` | \`string\` \\| \`boolean\` |
</td>
</tr>
</table>
#### Returns
Expand Down
10 changes: 10 additions & 0 deletions packages/typedoc-plugin-remark/test/stubs/module-2.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
export const someVariable = true;

/**
* @param param
*
* Default text content for `@category` tag.
*
* ```ts
* const x = 1;
* ```
*/
export function some_function(param: string) {}
2 changes: 1 addition & 1 deletion packages/typedoc-plugin-remark/test/typedoc.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"./frontmatter-plugin.cjs"
],
"readme": "none",
"parametersFormat": "table",
"parametersFormat": "htmlTable",
"disableSources": true,
"cleanOutputDir": true,
"hidePageHeader": true,
Expand Down

0 comments on commit 57aebaf

Please sign in to comment.