@rasenganjs/mdx 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rasengan MDX Plugin
2
2
 
3
- [![npm version](https://badge.fury.io/js/@rasenganjs%mdx.svg)](https://badge.fury.io/js/@rasenganjs%mdx)
4
- ![NPM Downloads](https://img.shields.io/npm/dm/%40rasenganjs%mdx)
3
+ [![npm version](https://badge.fury.io/js/@rasenganjs%2Fmdx.svg)](https://badge.fury.io/js/@rasenganjs%2Fmdx)
4
+ ![NPM Downloads](https://img.shields.io/npm/dm/%40rasenganjs%2Fmdx)
5
5
  [![GitHub license](https://img.shields.io/github/license/rasengan-dev/rasengan-mdx-plugin)](https://github.com/rasengan-dev/rasengan-mdx-plugin/blob/main/LICENSE)
6
6
 
7
7
  MDX plugin for Rasengan.Js
@@ -40,7 +40,7 @@ export default defineConfig({
40
40
  // Define aliases
41
41
  vite: {
42
42
  plugins: [
43
- mdx(),
43
+ ...mdx(),
44
44
  ],
45
45
  },
46
46
  });
@@ -98,7 +98,7 @@ We also have a [Twitter](https://twitter.com/rasenganjs) account where you can f
98
98
 
99
99
  ## License
100
100
 
101
- Rasengan.js is [MIT licensed](https://github.com/rasengan-dev/rasengan-image/blob/main/LICENSE).
101
+ [MIT licensed](https://github.com/rasengan-dev/rasengan-image/blob/main/LICENSE).
102
102
 
103
103
  ## Authors
104
104
 
package/lib/index.d.ts CHANGED
@@ -1,35 +1,34 @@
1
- export * from './types/index.js';
2
- export * from "./utils/index.js";
3
- export * from './components/index.js';
4
1
  /**
5
- * A Vite plugin that transforms MDX files into a format that can be used in a RasenganJs application.
2
+ * License: MIT
6
3
  *
7
- * The plugin performs the following tasks:
8
- * - Resolves the Vite configuration and stores it for later use.
9
- * - Transforms MDX files by applying the `@mdx-js/rollup` transformation.
10
- * - Extracts frontmatter data from the MDX files and creates a `metadata` object.
11
- * - Appends the `metadata` object to the transformed MDX content.
4
+ * Copyright (c) 2024 Dilane Kombou
12
5
  *
13
- * The transformed MDX content can then be used in the RasenganJs application, with the `metadata` object providing additional information about the content.
6
+ * This package is inspired by @mdx-js/rollup to provide a custom implement of the MDX plugin for RasenganJs.
14
7
  */
15
- export default function rasengan(): {
8
+ /// <reference types="node" />
9
+ export * from './types/index.js';
10
+ import { generatePage } from "./utils/index.js";
11
+ export default function (): ({
16
12
  name: string;
17
13
  enforce: string;
18
- /**
19
- * Stores the resolved Vite configuration for later use.
20
- *
21
- * @param resolvedConfig - The resolved Vite configuration object.
22
- */
14
+ config(config: unknown, env: any): void;
23
15
  configResolved(resolvedConfig: unknown): void;
24
- /**
25
- * Transforms an MDX file by applying the `@mdx-js/rollup` transformation, extracting frontmatter data, and appending a `metadata` object to the transformed content.
26
- *
27
- * @param code - The content of the MDX file.
28
- * @param id - The ID of the MDX file.
29
- * @returns An object containing the transformed MDX code and a source map, or `null` if the file is not an MDX file.
30
- */
31
16
  transform(code: string, id: string): Promise<{
32
17
  code: string;
33
18
  map: import("rollup").SourceMapInput;
34
19
  }>;
35
- };
20
+ } | {
21
+ name: string;
22
+ config: (config: any, env: any) => {
23
+ define: {
24
+ process: NodeJS.Process;
25
+ };
26
+ build: {
27
+ rollupOptions: {
28
+ plugins: import("rollup").Plugin<any>[];
29
+ };
30
+ };
31
+ };
32
+ apply: string;
33
+ })[];
34
+ export { generatePage };
package/lib/index.js CHANGED
@@ -1,72 +1,14 @@
1
- import mdx from '@mdx-js/rollup';
2
- import { createFilter } from "@rollup/pluginutils";
3
- import matter from 'gray-matter';
4
- export * from './types/index.js';
5
- export * from "./utils/index.js";
6
- export * from './components/index.js';
7
1
  /**
8
- * A Vite plugin that transforms MDX files into a format that can be used in a RasenganJs application.
2
+ * License: MIT
9
3
  *
10
- * The plugin performs the following tasks:
11
- * - Resolves the Vite configuration and stores it for later use.
12
- * - Transforms MDX files by applying the `@mdx-js/rollup` transformation.
13
- * - Extracts frontmatter data from the MDX files and creates a `metadata` object.
14
- * - Appends the `metadata` object to the transformed MDX content.
4
+ * Copyright (c) 2024 Dilane Kombou
15
5
  *
16
- * The transformed MDX content can then be used in the RasenganJs application, with the `metadata` object providing additional information about the content.
6
+ * This package is inspired by @mdx-js/rollup to provide a custom implement of the MDX plugin for RasenganJs.
17
7
  */
18
- export default function rasengan() {
19
- let config;
20
- const filter = createFilter(/\.mdx?$/);
21
- return {
22
- name: "vite-plugin-rasengan-mdx",
23
- // Apply transformation of the mdx file before other plugins
24
- enforce: 'pre',
25
- /**
26
- * Stores the resolved Vite configuration for later use.
27
- *
28
- * @param resolvedConfig - The resolved Vite configuration object.
29
- */
30
- configResolved(resolvedConfig) {
31
- // store the resolved config
32
- config = resolvedConfig;
33
- },
34
- /**
35
- * Transforms an MDX file by applying the `@mdx-js/rollup` transformation, extracting frontmatter data, and appending a `metadata` object to the transformed content.
36
- *
37
- * @param code - The content of the MDX file.
38
- * @param id - The ID of the MDX file.
39
- * @returns An object containing the transformed MDX code and a source map, or `null` if the file is not an MDX file.
40
- */
41
- async transform(code, id) {
42
- if (!filter(id)) {
43
- return null;
44
- }
45
- const { content, data: frontmatter } = matter(code);
46
- // Apply transformation of the mdx file
47
- const result = await mdx().transform(content, id);
48
- // Extract the file name from the path
49
- const fileName = id
50
- .split("/")
51
- .pop()
52
- .replace(/.page.mdx?$/, "");
53
- // TODO: Consider other params of metadata from frontmatter
54
- const metadata = {
55
- path: frontmatter.path || `/${fileName}`,
56
- metadata: frontmatter.metadata || {
57
- title: fileName,
58
- },
59
- };
60
- return {
61
- code: `
62
- ${result.code}
63
- const metadata = ${JSON.stringify(metadata)};
64
-
65
- MDXContent.metadata = metadata;
66
- `,
67
- map: result.map,
68
- };
69
- },
70
- };
8
+ export * from './types/index.js';
9
+ import { plugin, generatePage, globalsPolyfill } from "./utils/index.js";
10
+ export default function () {
11
+ return [plugin(), globalsPolyfill()];
71
12
  }
13
+ export { generatePage };
72
14
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,gBAAgB,CAAA;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AAEtC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ;IAC/B,IAAI,MAAe,CAAC;IACpB,MAAM,MAAM,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC;IAEvC,OAAO;QACN,IAAI,EAAE,0BAA0B;QAEhC,4DAA4D;QAC5D,OAAO,EAAE,KAAK;QAEd;;;;WAIG;QACH,cAAc,CAAC,cAAuB;YACrC,4BAA4B;YAC5B,MAAM,GAAG,cAAc,CAAC;QACzB,CAAC;QAED;;;;;;WAMG;QACH,KAAK,CAAC,SAAS,CAAC,IAAY,EAAE,EAAU;YACvC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;gBACjB,OAAO,IAAI,CAAC;YACb,CAAC;YAED,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAEpD,uCAAuC;YACvC,MAAM,MAAM,GAAG,MAAM,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YAElD,sCAAsC;YACtC,MAAM,QAAQ,GAAG,EAAE;iBACjB,KAAK,CAAC,GAAG,CAAC;iBACV,GAAG,EAAE;iBACL,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;YAE7B,2DAA2D;YAC3D,MAAM,QAAQ,GAAG;gBAChB,IAAI,EAAE,WAAW,CAAC,IAAI,IAAI,IAAI,QAAQ,EAAE;gBACxC,QAAQ,EAAE,WAAW,CAAC,QAAQ,IAAI;oBACjC,KAAK,EAAE,QAAQ;iBACf;aACD,CAAC;YAEF,OAAO;gBACN,IAAI,EAAE;YACE,MAAM,CAAC,IAAI;6BACM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;;;SAG5C;gBACL,GAAG,EAAE,MAAM,CAAC,GAAG;aACf,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEzE,MAAM,CAAC,OAAO;IACZ,OAAO,CAAC,MAAM,EAAE,EAAE,eAAe,EAAE,CAAC,CAAC;AACvC,CAAC;AACD,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Creates a filter function that can be used to filter a list of IDs based on include and exclude patterns.
3
+ *
4
+ * @param include - A string or array of strings representing the patterns to include.
5
+ * @param exclude - A string or array of strings representing the patterns to exclude.
6
+ * @returns A function that takes an ID string and returns `true` if the ID should be included, `false` otherwise.
7
+ */
8
+ export default function createFilter(include: string, exclude?: string): (id: string) => boolean;
@@ -0,0 +1,23 @@
1
+ import micromatch from 'micromatch';
2
+ /**
3
+ * Creates a filter function that can be used to filter a list of IDs based on include and exclude patterns.
4
+ *
5
+ * @param include - A string or array of strings representing the patterns to include.
6
+ * @param exclude - A string or array of strings representing the patterns to exclude.
7
+ * @returns A function that takes an ID string and returns `true` if the ID should be included, `false` otherwise.
8
+ */
9
+ export default function createFilter(include, exclude) {
10
+ return function (id) {
11
+ if (typeof id !== "string")
12
+ return false;
13
+ const matcher = micromatch.matcher(include);
14
+ if (exclude) {
15
+ const excluder = micromatch.matcher(exclude);
16
+ return matcher(id) && !excluder(id);
17
+ }
18
+ else {
19
+ return matcher(id);
20
+ }
21
+ };
22
+ }
23
+ //# sourceMappingURL=create-filter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-filter.js","sourceRoot":"","sources":["../../src/utils/create-filter.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CACnC,OAAe,EACf,OAAgB;IAEhB,OAAO,UAAU,EAAU;QAC1B,IAAI,OAAO,EAAE,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAC;QAEvC,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAE5C,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAC7C,OAAO,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QACtC,CAAC;aAAM,CAAC;YACN,OAAO,OAAO,CAAC,EAAE,CAAC,CAAC;QACrB,CAAC;IACJ,CAAC,CAAC;AACH,CAAC"}
@@ -0,0 +1,20 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Provides a Vite plugin that polyfills the `process` and `Buffer` global variables for the browser.
4
+ * This is necessary for certain libraries that expect these global variables to be available.
5
+ * The plugin is applied during the build phase of the Vite build process.
6
+ */
7
+ export default function globals(): {
8
+ name: string;
9
+ config: (config: any, env: any) => {
10
+ define: {
11
+ process: NodeJS.Process;
12
+ };
13
+ build: {
14
+ rollupOptions: {
15
+ plugins: import("rollup").Plugin<any>[];
16
+ };
17
+ };
18
+ };
19
+ apply: string;
20
+ };
@@ -0,0 +1,30 @@
1
+ import inject from "@rollup/plugin-inject";
2
+ /**
3
+ * Provides a Vite plugin that polyfills the `process` and `Buffer` global variables for the browser.
4
+ * This is necessary for certain libraries that expect these global variables to be available.
5
+ * The plugin is applied during the build phase of the Vite build process.
6
+ */
7
+ export default function globals() {
8
+ return {
9
+ name: "vite-plugin-rasengan-global-polyfill",
10
+ config: function config(config, env) {
11
+ // test if we are on the browser
12
+ return {
13
+ define: {
14
+ process: process,
15
+ },
16
+ build: {
17
+ rollupOptions: {
18
+ plugins: [
19
+ inject({
20
+ Buffer: ["buffer", "Buffer"],
21
+ }),
22
+ ],
23
+ },
24
+ },
25
+ };
26
+ },
27
+ apply: "build",
28
+ };
29
+ }
30
+ //# sourceMappingURL=globals.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globals.js","sourceRoot":"","sources":["../../src/utils/globals.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAE3C;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO;IAC9B,OAAO;QACN,IAAI,EAAE,sCAAsC;QAC5C,MAAM,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG;YAClC,gCAAgC;YAChC,OAAO;gBACN,MAAM,EAAE;oBACP,OAAO,EAAE,OAAO;iBAChB;gBAED,KAAK,EAAE;oBACN,aAAa,EAAE;wBACd,OAAO,EAAE;4BACR,MAAM,CAAC;gCACN,MAAM,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC;6BAC5B,CAAC;yBACF;qBACD;iBACD;aACD,CAAC;QACH,CAAC;QAED,KAAK,EAAE,OAAO;KACd,CAAC;AACH,CAAC"}
@@ -1,2 +1,4 @@
1
1
  import generatePage from "./generate-page.js";
2
- export { generatePage };
2
+ import plugin from "./plugin.js";
3
+ import globalsPolyfill from "./globals.js";
4
+ export { generatePage, plugin, globalsPolyfill };
@@ -1,3 +1,6 @@
1
1
  import generatePage from "./generate-page.js";
2
- export { generatePage };
2
+ import plugin from "./plugin.js";
3
+ // import nodePolyfill from "./polyfills.js";
4
+ import globalsPolyfill from "./globals.js";
5
+ export { generatePage, plugin, globalsPolyfill };
3
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAE9C,OAAO,EAAE,YAAY,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAC9C,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,6CAA6C;AAC7C,OAAO,eAAe,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * A Vite plugin that transforms MDX files into a format that can be used in a RasenganJs application.
3
+ *
4
+ * The plugin performs the following tasks:
5
+ * - Resolves the Vite configuration and stores it for later use.
6
+ * - Transforms MDX files by applying the `@mdx-js/rollup` transformation.
7
+ * - Extracts frontmatter data from the MDX files and creates a `metadata` object.
8
+ * - Appends the `metadata` object to the transformed MDX content.
9
+ *
10
+ * The transformed MDX content can then be used in the RasenganJs application, with the `metadata` object providing additional information about the content.
11
+ */
12
+ export default function plugin(): {
13
+ name: string;
14
+ enforce: string;
15
+ config(config: unknown, env: any): void;
16
+ /**
17
+ * Stores the resolved Vite configuration for later use.
18
+ *
19
+ * @param resolvedConfig - The resolved Vite configuration object.
20
+ */
21
+ configResolved(resolvedConfig: unknown): void;
22
+ /**
23
+ * Transforms an MDX file by applying the `@mdx-js/rollup` transformation, extracting frontmatter data, and appending a `metadata` object to the transformed content.
24
+ *
25
+ * @param code - The content of the MDX file.
26
+ * @param id - The ID of the MDX file.
27
+ * @returns An object containing the transformed MDX code and a source map, or `null` if the file is not an MDX file.
28
+ */
29
+ transform(code: string, id: string): Promise<{
30
+ code: string;
31
+ map: import("rollup").SourceMapInput;
32
+ }>;
33
+ };
@@ -0,0 +1,73 @@
1
+ import mdx from '@mdx-js/rollup';
2
+ import matter from 'gray-matter';
3
+ import createFilter from './create-filter.js';
4
+ /**
5
+ * A Vite plugin that transforms MDX files into a format that can be used in a RasenganJs application.
6
+ *
7
+ * The plugin performs the following tasks:
8
+ * - Resolves the Vite configuration and stores it for later use.
9
+ * - Transforms MDX files by applying the `@mdx-js/rollup` transformation.
10
+ * - Extracts frontmatter data from the MDX files and creates a `metadata` object.
11
+ * - Appends the `metadata` object to the transformed MDX content.
12
+ *
13
+ * The transformed MDX content can then be used in the RasenganJs application, with the `metadata` object providing additional information about the content.
14
+ */
15
+ export default function plugin() {
16
+ let config;
17
+ const filter = createFilter("**/*.md?(x)");
18
+ const mdxInstance = mdx();
19
+ return {
20
+ name: "vite-plugin-rasengan-mdx",
21
+ // Apply transformation of the mdx file before other plugins
22
+ enforce: 'pre',
23
+ config(config, env) {
24
+ mdxInstance.config(config, env);
25
+ },
26
+ /**
27
+ * Stores the resolved Vite configuration for later use.
28
+ *
29
+ * @param resolvedConfig - The resolved Vite configuration object.
30
+ */
31
+ configResolved(resolvedConfig) {
32
+ // store the resolved config
33
+ config = resolvedConfig;
34
+ },
35
+ /**
36
+ * Transforms an MDX file by applying the `@mdx-js/rollup` transformation, extracting frontmatter data, and appending a `metadata` object to the transformed content.
37
+ *
38
+ * @param code - The content of the MDX file.
39
+ * @param id - The ID of the MDX file.
40
+ * @returns An object containing the transformed MDX code and a source map, or `null` if the file is not an MDX file.
41
+ */
42
+ async transform(code, id) {
43
+ if (!filter(id)) {
44
+ return null;
45
+ }
46
+ const { content, data: frontmatter } = matter(code);
47
+ // Apply transformation of the mdx file
48
+ const result = await mdxInstance.transform(content, id);
49
+ // Extract the file name from the path
50
+ const fileName = id
51
+ .split("/")
52
+ .pop()
53
+ .replace(/.page.mdx?$/, "");
54
+ // TODO: Consider other params of metadata from frontmatter
55
+ const metadata = {
56
+ path: frontmatter.path || `/${fileName}`,
57
+ metadata: frontmatter.metadata || {
58
+ title: fileName,
59
+ },
60
+ };
61
+ return {
62
+ code: `
63
+ ${result.code}
64
+ const metadata = ${JSON.stringify(metadata)};
65
+
66
+ MDXContent.metadata = metadata;
67
+ `,
68
+ map: result.map,
69
+ };
70
+ },
71
+ };
72
+ }
73
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/utils/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,gBAAgB,CAAA;AAChC,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM;IAC7B,IAAI,MAAe,CAAC;IACpB,MAAM,MAAM,GAAG,YAAY,CAAC,aAAa,CAAC,CAAC;IAC3C,MAAM,WAAW,GAAI,GAAG,EAAE,CAAC;IAE3B,OAAO;QACN,IAAI,EAAE,0BAA0B;QAEhC,4DAA4D;QAC5D,OAAO,EAAE,KAAK;QAEd,MAAM,CAAC,MAAe,EAAE,GAAQ;YAC/B,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QACjC,CAAC;QAED;;;;WAIG;QACH,cAAc,CAAC,cAAuB;YACrC,4BAA4B;YAC5B,MAAM,GAAG,cAAc,CAAC;QACzB,CAAC;QAED;;;;;;WAMG;QACH,KAAK,CAAC,SAAS,CAAC,IAAY,EAAE,EAAU;YACvC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;gBACjB,OAAO,IAAI,CAAC;YACb,CAAC;YAED,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAEpD,uCAAuC;YACvC,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;YAExD,sCAAsC;YACtC,MAAM,QAAQ,GAAG,EAAE;iBACjB,KAAK,CAAC,GAAG,CAAC;iBACV,GAAG,EAAE;iBACL,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;YAE7B,2DAA2D;YAC3D,MAAM,QAAQ,GAAG;gBAChB,IAAI,EAAE,WAAW,CAAC,IAAI,IAAI,IAAI,QAAQ,EAAE;gBACxC,QAAQ,EAAE,WAAW,CAAC,QAAQ,IAAI;oBACjC,KAAK,EAAE,QAAQ;iBACf;aACD,CAAC;YAEF,OAAO;gBACN,IAAI,EAAE;YACE,MAAM,CAAC,IAAI;6BACM,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;;;SAG5C;gBACL,GAAG,EAAE,MAAM,CAAC,GAAG;aACf,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * A Vite plugin that provides polyfills for Node.js modules and APIs when running in a browser environment.
4
+ * This plugin is designed to work with the Vite bundler and helps ensure compatibility with Node.js-based code
5
+ * that is being used in a browser context.
6
+ *
7
+ * The plugin configures Vite's resolver to map various Node.js modules and APIs to their browser-compatible
8
+ * counterparts, allowing the code to run seamlessly in the browser without requiring changes to the original
9
+ * Node.js-based implementation.
10
+ *
11
+ * This plugin is particularly useful when working with libraries or frameworks that rely on Node.js-specific
12
+ * functionality, such as the `process` object or various built-in modules like `fs`, `crypto`, and `http`.
13
+ */
14
+ export default function polyfill(): {
15
+ name: string;
16
+ config: (config: any, env: any) => {
17
+ define: {
18
+ "process.env": NodeJS.ProcessEnv;
19
+ };
20
+ resolve: {
21
+ alias: {
22
+ find: string;
23
+ replacement: string;
24
+ }[];
25
+ };
26
+ };
27
+ enforce: string;
28
+ apply: string;
29
+ };
@@ -0,0 +1,120 @@
1
+ /**
2
+ * A Vite plugin that provides polyfills for Node.js modules and APIs when running in a browser environment.
3
+ * This plugin is designed to work with the Vite bundler and helps ensure compatibility with Node.js-based code
4
+ * that is being used in a browser context.
5
+ *
6
+ * The plugin configures Vite's resolver to map various Node.js modules and APIs to their browser-compatible
7
+ * counterparts, allowing the code to run seamlessly in the browser without requiring changes to the original
8
+ * Node.js-based implementation.
9
+ *
10
+ * This plugin is particularly useful when working with libraries or frameworks that rely on Node.js-specific
11
+ * functionality, such as the `process` object or various built-in modules like `fs`, `crypto`, and `http`.
12
+ */
13
+ export default function polyfill() {
14
+ return {
15
+ name: "vite-plugin-rasengan-node-polyfill",
16
+ config: function config(config, env) {
17
+ // test if we are on the browser
18
+ return {
19
+ define: {
20
+ "process.env": process.env,
21
+ },
22
+ resolve: {
23
+ alias: [
24
+ // {
25
+ // find: "assert",
26
+ // replacement: "assert",
27
+ // },
28
+ // {
29
+ // find: "buffer",
30
+ // replacement: "buffer/",
31
+ // },
32
+ // {
33
+ // find: "process",
34
+ // replacement: "process/browser.js",
35
+ // },
36
+ // {
37
+ // find: "console",
38
+ // replacement: "console-browserify",
39
+ // },
40
+ {
41
+ find: "fs",
42
+ replacement: "browserify-fs",
43
+ },
44
+ // {
45
+ // find: "crypto",
46
+ // replacement: "crypto-browserify",
47
+ // },
48
+ // {
49
+ // find: "domain",
50
+ // replacement: "domain-browser",
51
+ // },
52
+ // {
53
+ // find: "events",
54
+ // replacement: "events",
55
+ // },
56
+ // {
57
+ // find: "http",
58
+ // replacement: "stream-http",
59
+ // },
60
+ // {
61
+ // find: "https",
62
+ // replacement: "https-browserify",
63
+ // },
64
+ // {
65
+ // find: "path",
66
+ // replacement: "path-browserify",
67
+ // },
68
+ // {
69
+ // find: "punycode",
70
+ // replacement: "punycode",
71
+ // },
72
+ // {
73
+ // find: "querystring",
74
+ // replacement: "querystring-es3",
75
+ // },
76
+ // {
77
+ // find: "stream",
78
+ // replacement: "stream-browserify",
79
+ // },
80
+ // {
81
+ // find: "string_decoder",
82
+ // replacement: "string_decoder",
83
+ // },
84
+ // {
85
+ // find: "sys",
86
+ // replacement: "util",
87
+ // },
88
+ // {
89
+ // find: "timers",
90
+ // replacement: "timers-browserify",
91
+ // },
92
+ // {
93
+ // find: "tty",
94
+ // replacement: "tty-browserify",
95
+ // },
96
+ // {
97
+ // find: "url",
98
+ // replacement: "url",
99
+ // },
100
+ // {
101
+ // find: "util",
102
+ // replacement: "util",
103
+ // },
104
+ // {
105
+ // find: "vm",
106
+ // replacement: "vm-browserify",
107
+ // },
108
+ // {
109
+ // find: "zlib",
110
+ // replacement: "browserify-zlib",
111
+ // },
112
+ ],
113
+ },
114
+ };
115
+ },
116
+ enforce: "pre",
117
+ apply: "build",
118
+ };
119
+ }
120
+ //# sourceMappingURL=polyfills.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polyfills.js","sourceRoot":"","sources":["../../src/utils/polyfills.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ;IAC/B,OAAO;QACN,IAAI,EAAE,oCAAoC;QAC1C,MAAM,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG;YAClC,gCAAgC;YAChC,OAAO;gBACN,MAAM,EAAE;oBACP,aAAa,EAAE,OAAO,CAAC,GAAG;iBAC1B;gBAED,OAAO,EAAE;oBACR,KAAK,EAAE;wBACN,IAAI;wBACJ,mBAAmB;wBACnB,0BAA0B;wBAC1B,KAAK;wBACL,IAAI;wBACJ,mBAAmB;wBACnB,2BAA2B;wBAC3B,KAAK;wBACL,IAAI;wBACJ,oBAAoB;wBACpB,sCAAsC;wBACtC,KAAK;wBACL,IAAI;wBACJ,oBAAoB;wBACpB,sCAAsC;wBACtC,KAAK;wBACL;4BACC,IAAI,EAAE,IAAI;4BACV,WAAW,EAAE,eAAe;yBAC5B;wBACD,IAAI;wBACJ,mBAAmB;wBACnB,qCAAqC;wBACrC,KAAK;wBACL,IAAI;wBACJ,mBAAmB;wBACnB,kCAAkC;wBAClC,KAAK;wBACL,IAAI;wBACJ,mBAAmB;wBACnB,0BAA0B;wBAC1B,KAAK;wBACL,IAAI;wBACJ,iBAAiB;wBACjB,+BAA+B;wBAC/B,KAAK;wBACL,IAAI;wBACJ,kBAAkB;wBAClB,oCAAoC;wBACpC,KAAK;wBACL,IAAI;wBACJ,iBAAiB;wBACjB,mCAAmC;wBACnC,KAAK;wBACL,IAAI;wBACJ,qBAAqB;wBACrB,4BAA4B;wBAC5B,KAAK;wBACL,IAAI;wBACJ,wBAAwB;wBACxB,mCAAmC;wBACnC,KAAK;wBACL,IAAI;wBACJ,mBAAmB;wBACnB,qCAAqC;wBACrC,KAAK;wBACL,IAAI;wBACJ,2BAA2B;wBAC3B,kCAAkC;wBAClC,KAAK;wBACL,IAAI;wBACJ,gBAAgB;wBAChB,wBAAwB;wBACxB,KAAK;wBACL,IAAI;wBACJ,mBAAmB;wBACnB,qCAAqC;wBACrC,KAAK;wBACL,IAAI;wBACJ,gBAAgB;wBAChB,kCAAkC;wBAClC,KAAK;wBACL,IAAI;wBACJ,gBAAgB;wBAChB,uBAAuB;wBACvB,KAAK;wBACL,IAAI;wBACJ,iBAAiB;wBACjB,wBAAwB;wBACxB,KAAK;wBACL,IAAI;wBACJ,eAAe;wBACf,iCAAiC;wBACjC,KAAK;wBACL,IAAI;wBACJ,iBAAiB;wBACjB,mCAAmC;wBACnC,KAAK;qBACL;iBACD;aACD,CAAC;QACH,CAAC;QACD,OAAO,EAAE,KAAK;QACd,KAAK,EAAE,OAAO;KACd,CAAC;AACH,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import micromatch from 'micromatch';
2
+ const matcher = micromatch.matcher("**/*.md?(x)");
3
+ console.log({
4
+ test: matcher("/Users/dilanekoumbou/Documents/projects/Personal/Rasengan Framework/projects/mdx/src/app/blog.page.mdx")
5
+ });
6
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.js","sourceRoot":"","sources":["../../src/utils/test.js"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;AAElD,OAAO,CAAC,GAAG,CAAC;IACV,IAAI,EAAE,OAAO,CAAC,wGAAwG,CAAC;CACxH,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rasenganjs/mdx",
3
3
  "private": false,
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "RasenganJS plugin for MDX support",
6
6
  "type": "module",
7
7
  "main": "lib/index.js",
@@ -32,18 +32,23 @@
32
32
  "deploy": "npm publish --access public"
33
33
  },
34
34
  "devDependencies": {
35
+ "@types/micromatch": "^4.0.7",
35
36
  "@types/react": "^18.0.0",
36
37
  "typescript": "^5.2.2"
37
38
  },
38
39
  "dependencies": {
39
40
  "@mdx-js/rollup": "^3.0.1",
40
- "@rollup/pluginutils": "^5.1.0",
41
+ "@rollup/plugin-inject": "^5.0.5",
42
+ "browserify-fs": "^1.0.0",
41
43
  "gray-matter": "^4.0.3",
44
+ "micromatch": "^4.0.7",
45
+ "path-browserify": "^1.0.1",
42
46
  "prism-react-renderer": "^2.3.1",
43
- "prismjs": "^1.29.0"
47
+ "prismjs": "^1.29.0",
48
+ "stream-browserify": "^3.0.0"
44
49
  },
45
50
  "peerDependencies": {
46
- "rasengan": "^1.0.0-beta.35",
51
+ "rasengan": "*",
47
52
  "react": "^18.3.1"
48
53
  }
49
54
  }
package/src/index.ts CHANGED
@@ -1,82 +1,15 @@
1
- import mdx from '@mdx-js/rollup'
2
- import { createFilter } from "@rollup/pluginutils";
3
- import matter from 'gray-matter';
4
-
5
- export * from './types/index.js';
6
- export * from "./utils/index.js";
7
- export * from './components/index.js';
8
-
9
1
  /**
10
- * A Vite plugin that transforms MDX files into a format that can be used in a RasenganJs application.
11
- *
12
- * The plugin performs the following tasks:
13
- * - Resolves the Vite configuration and stores it for later use.
14
- * - Transforms MDX files by applying the `@mdx-js/rollup` transformation.
15
- * - Extracts frontmatter data from the MDX files and creates a `metadata` object.
16
- * - Appends the `metadata` object to the transformed MDX content.
17
- *
18
- * The transformed MDX content can then be used in the RasenganJs application, with the `metadata` object providing additional information about the content.
2
+ * License: MIT
3
+ *
4
+ * Copyright (c) 2024 Dilane Kombou
5
+ *
6
+ * This package is inspired by @mdx-js/rollup to provide a custom implement of the MDX plugin for RasenganJs.
19
7
  */
20
- export default function rasengan() {
21
- let config: unknown;
22
- const filter = createFilter(/\.mdx?$/);
23
-
24
- return {
25
- name: "vite-plugin-rasengan-mdx",
26
-
27
- // Apply transformation of the mdx file before other plugins
28
- enforce: 'pre',
29
-
30
- /**
31
- * Stores the resolved Vite configuration for later use.
32
- *
33
- * @param resolvedConfig - The resolved Vite configuration object.
34
- */
35
- configResolved(resolvedConfig: unknown) {
36
- // store the resolved config
37
- config = resolvedConfig;
38
- },
39
8
 
40
- /**
41
- * Transforms an MDX file by applying the `@mdx-js/rollup` transformation, extracting frontmatter data, and appending a `metadata` object to the transformed content.
42
- *
43
- * @param code - The content of the MDX file.
44
- * @param id - The ID of the MDX file.
45
- * @returns An object containing the transformed MDX code and a source map, or `null` if the file is not an MDX file.
46
- */
47
- async transform(code: string, id: string) {
48
- if (!filter(id)) {
49
- return null;
50
- }
51
-
52
- const { content, data: frontmatter } = matter(code);
53
-
54
- // Apply transformation of the mdx file
55
- const result = await mdx().transform(content, id);
56
-
57
- // Extract the file name from the path
58
- const fileName = id
59
- .split("/")
60
- .pop()
61
- .replace(/.page.mdx?$/, "");
62
-
63
- // TODO: Consider other params of metadata from frontmatter
64
- const metadata = {
65
- path: frontmatter.path || `/${fileName}`,
66
- metadata: frontmatter.metadata || {
67
- title: fileName,
68
- },
69
- };
9
+ export * from './types/index.js';
10
+ import { plugin, generatePage, globalsPolyfill } from "./utils/index.js";
70
11
 
71
- return {
72
- code: `
73
- ${result.code}
74
- const metadata = ${JSON.stringify(metadata)};
75
-
76
- MDXContent.metadata = metadata;
77
- `,
78
- map: result.map,
79
- };
80
- },
81
- };
12
+ export default function() {
13
+ return [plugin(), globalsPolyfill()];
82
14
  }
15
+ export { generatePage };
@@ -0,0 +1,26 @@
1
+ import micromatch from 'micromatch';
2
+
3
+ /**
4
+ * Creates a filter function that can be used to filter a list of IDs based on include and exclude patterns.
5
+ *
6
+ * @param include - A string or array of strings representing the patterns to include.
7
+ * @param exclude - A string or array of strings representing the patterns to exclude.
8
+ * @returns A function that takes an ID string and returns `true` if the ID should be included, `false` otherwise.
9
+ */
10
+ export default function createFilter(
11
+ include: string,
12
+ exclude?: string
13
+ ) {
14
+ return function (id: string) {
15
+ if (typeof id !== "string") return false;
16
+
17
+ const matcher = micromatch.matcher(include);
18
+
19
+ if (exclude) {
20
+ const excluder = micromatch.matcher(exclude);
21
+ return matcher(id) && !excluder(id);
22
+ } else {
23
+ return matcher(id);
24
+ }
25
+ };
26
+ }
@@ -0,0 +1,32 @@
1
+ import inject from "@rollup/plugin-inject";
2
+
3
+ /**
4
+ * Provides a Vite plugin that polyfills the `process` and `Buffer` global variables for the browser.
5
+ * This is necessary for certain libraries that expect these global variables to be available.
6
+ * The plugin is applied during the build phase of the Vite build process.
7
+ */
8
+ export default function globals() {
9
+ return {
10
+ name: "vite-plugin-rasengan-global-polyfill",
11
+ config: function config(config, env) {
12
+ // test if we are on the browser
13
+ return {
14
+ define: {
15
+ process: process,
16
+ },
17
+
18
+ build: {
19
+ rollupOptions: {
20
+ plugins: [
21
+ inject({
22
+ Buffer: ["buffer", "Buffer"],
23
+ }),
24
+ ],
25
+ },
26
+ },
27
+ };
28
+ },
29
+
30
+ apply: "build",
31
+ };
32
+ }
@@ -1,3 +1,6 @@
1
1
  import generatePage from "./generate-page.js";
2
+ import plugin from "./plugin.js";
3
+ // import nodePolyfill from "./polyfills.js";
4
+ import globalsPolyfill from "./globals.js";
2
5
 
3
- export { generatePage };
6
+ export { generatePage, plugin, globalsPolyfill };
@@ -0,0 +1,83 @@
1
+ import mdx from '@mdx-js/rollup'
2
+ import matter from 'gray-matter';
3
+ import createFilter from './create-filter.js';
4
+
5
+ /**
6
+ * A Vite plugin that transforms MDX files into a format that can be used in a RasenganJs application.
7
+ *
8
+ * The plugin performs the following tasks:
9
+ * - Resolves the Vite configuration and stores it for later use.
10
+ * - Transforms MDX files by applying the `@mdx-js/rollup` transformation.
11
+ * - Extracts frontmatter data from the MDX files and creates a `metadata` object.
12
+ * - Appends the `metadata` object to the transformed MDX content.
13
+ *
14
+ * The transformed MDX content can then be used in the RasenganJs application, with the `metadata` object providing additional information about the content.
15
+ */
16
+ export default function plugin() {
17
+ let config: unknown;
18
+ const filter = createFilter("**/*.md?(x)");
19
+ const mdxInstance = mdx();
20
+
21
+ return {
22
+ name: "vite-plugin-rasengan-mdx",
23
+
24
+ // Apply transformation of the mdx file before other plugins
25
+ enforce: 'pre',
26
+
27
+ config(config: unknown, env: any) {
28
+ mdxInstance.config(config, env);
29
+ },
30
+
31
+ /**
32
+ * Stores the resolved Vite configuration for later use.
33
+ *
34
+ * @param resolvedConfig - The resolved Vite configuration object.
35
+ */
36
+ configResolved(resolvedConfig: unknown) {
37
+ // store the resolved config
38
+ config = resolvedConfig;
39
+ },
40
+
41
+ /**
42
+ * Transforms an MDX file by applying the `@mdx-js/rollup` transformation, extracting frontmatter data, and appending a `metadata` object to the transformed content.
43
+ *
44
+ * @param code - The content of the MDX file.
45
+ * @param id - The ID of the MDX file.
46
+ * @returns An object containing the transformed MDX code and a source map, or `null` if the file is not an MDX file.
47
+ */
48
+ async transform(code: string, id: string) {
49
+ if (!filter(id)) {
50
+ return null;
51
+ }
52
+
53
+ const { content, data: frontmatter } = matter(code);
54
+
55
+ // Apply transformation of the mdx file
56
+ const result = await mdxInstance.transform(content, id);
57
+
58
+ // Extract the file name from the path
59
+ const fileName = id
60
+ .split("/")
61
+ .pop()
62
+ .replace(/.page.mdx?$/, "");
63
+
64
+ // TODO: Consider other params of metadata from frontmatter
65
+ const metadata = {
66
+ path: frontmatter.path || `/${fileName}`,
67
+ metadata: frontmatter.metadata || {
68
+ title: fileName,
69
+ },
70
+ };
71
+
72
+ return {
73
+ code: `
74
+ ${result.code}
75
+ const metadata = ${JSON.stringify(metadata)};
76
+
77
+ MDXContent.metadata = metadata;
78
+ `,
79
+ map: result.map,
80
+ };
81
+ },
82
+ };
83
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * A Vite plugin that provides polyfills for Node.js modules and APIs when running in a browser environment.
3
+ * This plugin is designed to work with the Vite bundler and helps ensure compatibility with Node.js-based code
4
+ * that is being used in a browser context.
5
+ *
6
+ * The plugin configures Vite's resolver to map various Node.js modules and APIs to their browser-compatible
7
+ * counterparts, allowing the code to run seamlessly in the browser without requiring changes to the original
8
+ * Node.js-based implementation.
9
+ *
10
+ * This plugin is particularly useful when working with libraries or frameworks that rely on Node.js-specific
11
+ * functionality, such as the `process` object or various built-in modules like `fs`, `crypto`, and `http`.
12
+ */
13
+ export default function polyfill() {
14
+ return {
15
+ name: "vite-plugin-rasengan-node-polyfill",
16
+ config: function config(config, env) {
17
+ // test if we are on the browser
18
+ return {
19
+ define: {
20
+ "process.env": process.env,
21
+ },
22
+
23
+ resolve: {
24
+ alias: [
25
+ // {
26
+ // find: "assert",
27
+ // replacement: "assert",
28
+ // },
29
+ // {
30
+ // find: "buffer",
31
+ // replacement: "buffer/",
32
+ // },
33
+ // {
34
+ // find: "process",
35
+ // replacement: "process/browser.js",
36
+ // },
37
+ // {
38
+ // find: "console",
39
+ // replacement: "console-browserify",
40
+ // },
41
+ {
42
+ find: "fs",
43
+ replacement: "browserify-fs",
44
+ },
45
+ // {
46
+ // find: "crypto",
47
+ // replacement: "crypto-browserify",
48
+ // },
49
+ // {
50
+ // find: "domain",
51
+ // replacement: "domain-browser",
52
+ // },
53
+ // {
54
+ // find: "events",
55
+ // replacement: "events",
56
+ // },
57
+ // {
58
+ // find: "http",
59
+ // replacement: "stream-http",
60
+ // },
61
+ // {
62
+ // find: "https",
63
+ // replacement: "https-browserify",
64
+ // },
65
+ // {
66
+ // find: "path",
67
+ // replacement: "path-browserify",
68
+ // },
69
+ // {
70
+ // find: "punycode",
71
+ // replacement: "punycode",
72
+ // },
73
+ // {
74
+ // find: "querystring",
75
+ // replacement: "querystring-es3",
76
+ // },
77
+ // {
78
+ // find: "stream",
79
+ // replacement: "stream-browserify",
80
+ // },
81
+ // {
82
+ // find: "string_decoder",
83
+ // replacement: "string_decoder",
84
+ // },
85
+ // {
86
+ // find: "sys",
87
+ // replacement: "util",
88
+ // },
89
+ // {
90
+ // find: "timers",
91
+ // replacement: "timers-browserify",
92
+ // },
93
+ // {
94
+ // find: "tty",
95
+ // replacement: "tty-browserify",
96
+ // },
97
+ // {
98
+ // find: "url",
99
+ // replacement: "url",
100
+ // },
101
+ // {
102
+ // find: "util",
103
+ // replacement: "util",
104
+ // },
105
+ // {
106
+ // find: "vm",
107
+ // replacement: "vm-browserify",
108
+ // },
109
+ // {
110
+ // find: "zlib",
111
+ // replacement: "browserify-zlib",
112
+ // },
113
+ ],
114
+ },
115
+ };
116
+ },
117
+ enforce: "pre",
118
+ apply: "build",
119
+ };
120
+ }
@@ -0,0 +1,7 @@
1
+ import micromatch from 'micromatch';
2
+
3
+ const matcher = micromatch.matcher("**/*.md?(x)");
4
+
5
+ console.log({
6
+ test: matcher("/Users/dilanekoumbou/Documents/projects/Personal/Rasengan Framework/projects/mdx/src/app/blog.page.mdx")
7
+ })