Skip to content

Commit

Permalink
tests: run content tests on all MDX files inside ./puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
fiveoutofnine committed Dec 3, 2023
1 parent 43d1502 commit d240f46
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 64 deletions.
36 changes: 36 additions & 0 deletions lib/utils/get-unique-elements-from-mdx-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------

const P1 = /(_jsxs|_jsx)\(_components.[a-zA-Z-_0-9]+/g;
const P2 = /(_jsxs|_jsx)\("[a-zA-Z-_0-9]+"/g;
const P3 = /(_jsxs|_jsx)\([a-zA-Z-_0-9]+/g;
export const JSX_ELEMENTS_PATTERN = new RegExp(`${P1.source}|${P2.source}|${P3.source}`, 'g');

// -----------------------------------------------------------------------------
// Function
// -----------------------------------------------------------------------------

const getUniqueElementsFromMDXSource = (mdxSource: string): string[] => {
const elements = mdxSource.match(JSX_ELEMENTS_PATTERN) ?? [];

return [
...new Set(
[...new Set(elements)].map((e) => {
let e_ = e;
// Remove `_jsxs(` and `_jsx(` from the beginning of the string.
if (e.startsWith('_jsxs(')) e_ = e.substring(6);
else if (e.startsWith('_jsx(')) e_ = e.substring(5);

// Remove `"` from the beginning and end of the string or remove
// `_components.` from the beginning of the string.
if (e_.startsWith('"') && e_.endsWith('"')) e_ = e_.substring(1, e_.length - 1);
else if (e_.startsWith('_components.')) e_ = e_.substring(12);

return e_;
}),
),
];
};

export default getUniqueElementsFromMDXSource;
3 changes: 2 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getUniqueElementsFromMDXSource from './get-unique-elements-from-mdx-source';
import readFile from './read-file';
import serializeToMDXSource from './serialize-to-mdx-source';

export { readFile, serializeToMDXSource };
export { getUniqueElementsFromMDXSource, readFile, serializeToMDXSource };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@mdx-js/react": "^3.0.0",
"@types/react": "^18.2.41",
"glob": "^10.3.10",
"next-mdx-remote": "^4.4.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
Loading

0 comments on commit d240f46

Please sign in to comment.