hexo-renderer-mdx 1.0.0

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.
@@ -0,0 +1,162 @@
1
+ # Contributing to hexo-renderer-mdx
2
+
3
+ Thank you for your interest in contributing! This document provides guidelines for contributing to the project.
4
+
5
+ ## Getting Started
6
+
7
+ 1. Fork the repository
8
+ 2. Clone your fork:
9
+ ```bash
10
+ git clone https://github.com/YOUR_USERNAME/hexo-renderer-mdx.git
11
+ cd hexo-renderer-mdx
12
+ ```
13
+ 3. Install dependencies:
14
+ ```bash
15
+ npm install
16
+ ```
17
+
18
+ ## Development
19
+
20
+ ### Running Tests
21
+
22
+ ```bash
23
+ npm test
24
+ ```
25
+
26
+ This runs both basic and advanced test suites that validate:
27
+ - MDX compilation
28
+ - Markdown rendering
29
+ - JSX element rendering
30
+ - Custom components
31
+ - Dynamic content
32
+ - Front matter handling
33
+
34
+ ### Adding New Features
35
+
36
+ 1. Create a new branch:
37
+ ```bash
38
+ git checkout -b feature/your-feature-name
39
+ ```
40
+
41
+ 2. Make your changes to `index.js`
42
+
43
+ 3. Add tests if applicable:
44
+ - Add test cases to `test/` directory
45
+ - Update `test.js` or `test-advanced.js` as needed
46
+
47
+ 4. Run tests to ensure nothing is broken:
48
+ ```bash
49
+ npm test
50
+ ```
51
+
52
+ 5. Commit your changes:
53
+ ```bash
54
+ git add .
55
+ git commit -m "Add: your feature description"
56
+ ```
57
+
58
+ 6. Push to your fork:
59
+ ```bash
60
+ git push origin feature/your-feature-name
61
+ ```
62
+
63
+ 7. Open a Pull Request
64
+
65
+ ## Code Style
66
+
67
+ - Use single quotes for strings
68
+ - Use 2 spaces for indentation
69
+ - Add JSDoc comments for functions
70
+ - Follow existing code patterns
71
+
72
+ ## Pull Request Guidelines
73
+
74
+ - Keep PRs focused on a single feature or fix
75
+ - Update documentation if needed
76
+ - Ensure all tests pass
77
+ - Add tests for new features
78
+ - Update README.md if adding user-facing changes
79
+
80
+ ## Reporting Issues
81
+
82
+ When reporting issues, please include:
83
+ - Hexo version
84
+ - Node.js version
85
+ - hexo-renderer-mdx version
86
+ - Steps to reproduce
87
+ - Expected vs actual behavior
88
+ - Error messages (if any)
89
+
90
+ ## Publishing (Maintainers Only)
91
+
92
+ ### Setup - Trusted Publishing
93
+
94
+ This package uses **Trusted Publishing** (OIDC) for secure, token-free publishing to npm.
95
+
96
+ 1. **Create an npm account** at https://www.npmjs.com if you don't have one
97
+
98
+ 2. **Configure Trusted Publishing on npm**:
99
+ - Publish the package manually first time: `npm publish --access public`
100
+ - Go to https://www.npmjs.com/package/hexo-renderer-mdx/access
101
+ - Navigate to "Publishing" → "Trusted Publishers"
102
+ - Click "Add trusted publisher"
103
+ - Fill in the details:
104
+ - **Provider**: GitHub Actions
105
+ - **Repository owner**: Bryan0324
106
+ - **Repository name**: hexo-renderer-mdx
107
+ - **Workflow file**: publish.yml
108
+ - **Environment name**: (leave empty)
109
+ - Click "Add"
110
+
111
+ No npm tokens needed! The workflow uses OpenID Connect (OIDC) to authenticate securely.
112
+
113
+ ### Publishing a New Version
114
+
115
+ 1. **Update version** in `package.json`:
116
+ ```bash
117
+ npm version patch # for bug fixes (1.0.0 → 1.0.1)
118
+ npm version minor # for new features (1.0.0 → 1.1.0)
119
+ npm version major # for breaking changes (1.0.0 → 2.0.0)
120
+ ```
121
+
122
+ 2. **Push changes and tags**:
123
+ ```bash
124
+ git push
125
+ git push --tags
126
+ ```
127
+
128
+ 3. **Create a GitHub Release**:
129
+ - Go to https://github.com/Bryan0324/hexo-renderer-mdx/releases
130
+ - Click "Draft a new release"
131
+ - Choose the tag you just created
132
+ - Add release title (e.g., "v1.0.1")
133
+ - Add release notes describing changes
134
+ - Click "Publish release"
135
+
136
+ 4. **GitHub Action will automatically**:
137
+ - Checkout the code
138
+ - Install dependencies
139
+ - Run tests
140
+ - Publish to npm with provenance using Trusted Publishing
141
+ - Make the package publicly available
142
+
143
+ 5. **Verify publication**:
144
+ - Check https://www.npmjs.com/package/hexo-renderer-mdx
145
+ - Verify the new version is listed
146
+ - Check that provenance is displayed
147
+
148
+ ### Manual Publishing (If Needed)
149
+
150
+ If you need to publish manually:
151
+
152
+ ```bash
153
+ # Login to npm
154
+ npm login
155
+
156
+ # Publish
157
+ npm publish --access public
158
+ ```
159
+
160
+ ## License
161
+
162
+ By contributing, you agree that your contributions will be licensed under the MIT License.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 grass-cat//Bryan0324
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,378 @@
1
+ # hexo-renderer-mdx
2
+
3
+ A [Hexo](https://hexo.io/) renderer plugin for [MDX](https://mdxjs.com/) - Markdown with JSX support. This plugin allows you to write Hexo posts and pages using MDX, enabling you to embed React components directly in your markdown content.
4
+
5
+ ## Features
6
+
7
+ - 🚀 Full MDX support with JSX syntax
8
+ - ⚛️ React component integration
9
+ - 📝 Markdown compatibility
10
+ - 🎨 Custom component support
11
+ - 🔥 Fast compilation with @mdx-js/mdx
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install hexo-renderer-mdx --save
17
+ ```
18
+
19
+ or with yarn:
20
+
21
+ ```bash
22
+ yarn add hexo-renderer-mdx
23
+ ```
24
+
25
+ ## How to Use
26
+
27
+ ### Quick Start
28
+
29
+ 1. **Install the plugin** in your Hexo blog directory:
30
+
31
+ ```bash
32
+ npm install hexo-renderer-mdx --save
33
+ ```
34
+
35
+ 2. **Create your first MDX post** in `source/_posts/my-first-mdx-post.mdx`:
36
+
37
+ ```mdx
38
+ ---
39
+ title: My First MDX Post
40
+ date: 2026-01-06
41
+ tags: [hexo, mdx]
42
+ ---
43
+
44
+ # Hello from MDX!
45
+
46
+ This is regular markdown **with bold text**.
47
+
48
+ <div style={{
49
+ padding: '20px',
50
+ backgroundColor: '#e3f2fd',
51
+ borderRadius: '8px',
52
+ marginTop: '20px'
53
+ }}>
54
+ 🎉 This is a styled JSX element!
55
+ </div>
56
+ ```
57
+
58
+ 3. **Generate your site**:
59
+
60
+ ```bash
61
+ hexo generate
62
+ ```
63
+
64
+ 4. **Preview locally**:
65
+
66
+ ```bash
67
+ hexo server
68
+ ```
69
+
70
+ 5. **View your post** at `http://localhost:4000`
71
+
72
+ That's it! The plugin automatically processes all `.mdx` files in your Hexo blog.
73
+
74
+ ### Step-by-Step Guide
75
+
76
+ #### 1. Setting Up a New Hexo Blog (Optional)
77
+
78
+ If you don't have a Hexo blog yet:
79
+
80
+ ```bash
81
+ npm install -g hexo-cli
82
+ hexo init my-blog
83
+ cd my-blog
84
+ npm install
85
+ ```
86
+
87
+ #### 2. Installing the Plugin
88
+
89
+ In your Hexo blog directory:
90
+
91
+ ```bash
92
+ npm install hexo-renderer-mdx --save
93
+ ```
94
+
95
+ No additional configuration is needed - the plugin registers automatically!
96
+
97
+ #### 3. Creating MDX Files
98
+
99
+ MDX files work just like regular Markdown files, but with JSX support. Create files in:
100
+ - `source/_posts/` for blog posts
101
+ - `source/` for pages
102
+
103
+ **File naming**: Use `.mdx` extension (e.g., `my-post.mdx`)
104
+
105
+ **Front matter**: Same as regular Hexo posts:
106
+
107
+ ```mdx
108
+ ---
109
+ title: Post Title
110
+ date: 2026-01-06
111
+ categories:
112
+ - Technology
113
+ tags:
114
+ - hexo
115
+ - mdx
116
+ ---
117
+
118
+ Your content here...
119
+ ```
120
+
121
+ #### 4. Using MDX Features
122
+
123
+ **Standard Markdown** - All markdown features work:
124
+
125
+ ```mdx
126
+ # Heading 1
127
+ ## Heading 2
128
+
129
+ **Bold**, *italic*, ~~strikethrough~~
130
+
131
+ - List item 1
132
+ - List item 2
133
+
134
+ [Link text](https://example.com)
135
+ ```
136
+
137
+ **Inline JSX** - Add HTML/JSX elements anywhere:
138
+
139
+ ```mdx
140
+ <div className="custom-class" style={{ color: 'blue' }}>
141
+ Custom styled content
142
+ </div>
143
+ ```
144
+
145
+ **Custom Components** - Define and use React components:
146
+
147
+ ```mdx
148
+ export const Alert = ({ children, type = 'info' }) => (
149
+ <div style={{
150
+ padding: '15px',
151
+ backgroundColor: type === 'warning' ? '#fff3cd' : '#d1ecf1',
152
+ borderRadius: '5px',
153
+ margin: '20px 0'
154
+ }}>
155
+ {children}
156
+ </div>
157
+ );
158
+
159
+ <Alert type="warning">
160
+ This is a custom alert component!
161
+ </Alert>
162
+ ```
163
+
164
+ **Dynamic Content** - Use JavaScript expressions:
165
+
166
+ ```mdx
167
+ <div>
168
+ {['React', 'Vue', 'Angular'].map(framework => (
169
+ <p key={framework}>I ❤️ {framework}</p>
170
+ ))}
171
+ </div>
172
+ ```
173
+
174
+ #### 5. Building and Deploying
175
+
176
+ Build your site as usual:
177
+
178
+ ```bash
179
+ # Generate static files
180
+ hexo generate
181
+
182
+ # Deploy (if configured)
183
+ hexo deploy
184
+ ```
185
+
186
+ The MDX files are compiled to static HTML - no JavaScript runtime needed on your site!
187
+
188
+ ## Usage
189
+
190
+ After installation, you can create `.mdx` files in your `source/_posts` or `source` directory.
191
+
192
+ ### Basic Example
193
+
194
+ Create a file `source/_posts/hello-mdx.mdx`:
195
+
196
+ ```mdx
197
+ ---
198
+ title: Hello MDX
199
+ date: 2026-01-06
200
+ tags:
201
+ - mdx
202
+ - react
203
+ ---
204
+
205
+ # Hello MDX!
206
+
207
+ This is a regular markdown paragraph.
208
+
209
+ You can use JSX directly:
210
+
211
+ <div style={{ padding: '20px', backgroundColor: '#f0f0f0' }}>
212
+ <h2>Custom Component</h2>
213
+ <p>This is rendered using JSX!</p>
214
+ </div>
215
+
216
+ And back to markdown...
217
+
218
+ ## Features
219
+
220
+ - List item 1
221
+ - List item 2
222
+ - List item 3
223
+ ```
224
+
225
+ ### Advanced Example
226
+
227
+ You can use more complex JSX:
228
+
229
+ ```mdx
230
+ ---
231
+ title: Advanced MDX
232
+ ---
233
+
234
+ export const Button = ({ children, color = 'blue' }) => (
235
+ <button style={{
236
+ backgroundColor: color,
237
+ color: 'white',
238
+ padding: '10px 20px',
239
+ border: 'none',
240
+ borderRadius: '5px',
241
+ cursor: 'pointer'
242
+ }}>
243
+ {children}
244
+ </button>
245
+ );
246
+
247
+ # Advanced MDX Example
248
+
249
+ Here's a custom button component:
250
+
251
+ <Button color="red">Click Me!</Button>
252
+
253
+ You can use any valid JSX expression:
254
+
255
+ <div>
256
+ {['apple', 'banana', 'cherry'].map(fruit => (
257
+ <p key={fruit}>I love {fruit}s!</p>
258
+ ))}
259
+ </div>
260
+ ```
261
+
262
+ ## Configuration
263
+
264
+ The plugin works out of the box with no configuration required. However, you can customize MDX compilation options if needed.
265
+
266
+ ### Advanced Configuration
267
+
268
+ If you need to customize the MDX compiler options, you can create a `hexo-renderer-mdx` configuration in your Hexo `_config.yml`:
269
+
270
+ ```yaml
271
+ # _config.yml
272
+ mdx:
273
+ # Enable development mode for better error messages
274
+ development: false
275
+ ```
276
+
277
+ Note: The current version uses sensible defaults optimized for static site generation.
278
+
279
+ The plugin:
280
+ 1. Compiles MDX files to JavaScript functions
281
+ 2. Executes them with a React runtime
282
+ 3. Renders the result to static HTML
283
+ 4. Passes the HTML to Hexo for page generation
284
+
285
+ ## Requirements
286
+
287
+ - Node.js >= 14.0.0
288
+ - Hexo >= 5.0.0
289
+
290
+ ## Dependencies
291
+
292
+ This plugin uses:
293
+ - `@mdx-js/mdx` - MDX compiler
294
+ - `react` & `react-dom` - React runtime for server-side rendering
295
+
296
+ ## Notes
297
+
298
+ - MDX files are compiled to static HTML at build time
299
+ - Interactive React components will not be interactive in the final output (server-side rendering only)
300
+ - For interactive components, consider using a different approach or client-side hydration
301
+
302
+ ## Troubleshooting
303
+
304
+ ### MDX Compilation Errors
305
+
306
+ If you encounter compilation errors, check:
307
+ - Your JSX syntax is valid
308
+ - All tags are properly closed
309
+ - You're not using unsupported JSX features
310
+
311
+ ### Missing Dependencies
312
+
313
+ Make sure all peer dependencies are installed:
314
+
315
+ ```bash
316
+ npm install react react-dom @mdx-js/mdx --save
317
+ ```
318
+
319
+ ## Publishing
320
+
321
+ ### For Maintainers
322
+
323
+ This package uses automated publishing to npm via GitHub Actions with **Trusted Publishing** (OIDC).
324
+
325
+ **To publish a new version:**
326
+
327
+ 1. Update the version in `package.json`:
328
+ ```bash
329
+ npm version patch # or minor, or major
330
+ ```
331
+
332
+ 2. Push the changes and tags:
333
+ ```bash
334
+ git push && git push --tags
335
+ ```
336
+
337
+ 3. Create a new release on GitHub:
338
+ - Go to the repository's "Releases" page
339
+ - Click "Create a new release"
340
+ - Select the tag you just created
341
+ - Add release notes
342
+ - Click "Publish release"
343
+
344
+ 4. The GitHub Action will automatically:
345
+ - Run tests
346
+ - Publish to npm with provenance using Trusted Publishing
347
+ - Make the package publicly available
348
+
349
+ **First-time setup:**
350
+
351
+ Before publishing, you need to configure Trusted Publishing on npm:
352
+
353
+ 1. Go to https://www.npmjs.com/package/hexo-renderer-mdx/access
354
+ 2. Click "Publishing" → "Trusted Publishers"
355
+ 3. Add a new trusted publisher with:
356
+ - **Provider**: GitHub Actions
357
+ - **Repository owner**: Bryan0324
358
+ - **Repository name**: hexo-renderer-mdx
359
+ - **Workflow file**: `publish.yml`
360
+ - **Environment name**: (leave empty)
361
+
362
+ No npm tokens required! Trusted Publishing uses OpenID Connect (OIDC) for secure authentication.
363
+
364
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for detailed publishing instructions.
365
+
366
+ ## Contributing
367
+
368
+ Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
369
+
370
+ ## License
371
+
372
+ MIT
373
+
374
+ ## Related
375
+
376
+ - [Hexo](https://hexo.io/)
377
+ - [MDX](https://mdxjs.com/)
378
+ - [React](https://reactjs.org/)
package/index.js ADDED
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ const { compile } = require('@mdx-js/mdx');
4
+ const { renderToString } = require('react-dom/server');
5
+ const React = require('react');
6
+
7
+ /**
8
+ * MDX Renderer for Hexo
9
+ *
10
+ * This renderer allows you to use MDX files in your Hexo blog.
11
+ * MDX is markdown with JSX support, allowing you to embed React components.
12
+ */
13
+
14
+ /**
15
+ * Render MDX content to HTML
16
+ * @param {Object} data - The data object containing MDX content
17
+ * @param {string} data.text - The MDX content to render
18
+ * @param {string} data.path - The file path (for error reporting)
19
+ * @returns {Promise<string>} The rendered HTML
20
+ */
21
+ async function mdxRenderer(data) {
22
+ const { text, path } = data;
23
+
24
+ try {
25
+ // Strip YAML front matter if present
26
+ // Note: Hexo typically strips front matter before passing to renderers,
27
+ // but we handle it here as a safety measure for edge cases
28
+ let content = text;
29
+ const frontMatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n/;
30
+ const match = content.match(frontMatterRegex);
31
+
32
+ if (match) {
33
+ // Remove front matter from content
34
+ content = content.slice(match[0].length);
35
+ }
36
+
37
+ // Compile MDX to JavaScript with automatic JSX runtime
38
+ const compiled = await compile(content, {
39
+ outputFormat: 'function-body',
40
+ development: false,
41
+ jsxImportSource: 'react'
42
+ });
43
+
44
+ // Create a function from the compiled code
45
+ const code = String(compiled);
46
+
47
+ // The compiled code expects arguments[0] to be the jsx-runtime
48
+ const jsxRuntime = require('react/jsx-runtime');
49
+
50
+ // Create and execute the MDX module function
51
+ // Note: Using new Function() here is safe because:
52
+ // 1. Input comes from MDX files in the user's Hexo project (not untrusted external input)
53
+ // 2. MDX compilation itself validates and sanitizes the content
54
+ // 3. This is a build-time operation, not runtime user input
55
+ const fn = new Function(code);
56
+ const mdxModule = fn.call(null, jsxRuntime);
57
+
58
+ // The result has a default export which is the MDX component
59
+ const MDXContent = mdxModule.default;
60
+
61
+ // Render the component to static HTML
62
+ const html = renderToString(
63
+ React.createElement(MDXContent, {})
64
+ );
65
+
66
+ return html;
67
+ } catch (err) {
68
+ throw new Error(`MDX compilation failed for ${path}: ${err.message}`);
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Register the MDX renderer with Hexo
74
+ */
75
+ hexo.extend.renderer.register('mdx', 'html', mdxRenderer, true);
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "hexo-renderer-mdx",
3
+ "version": "1.0.0",
4
+ "description": "MDX renderer plugin for Hexo with React component support",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js && node test-advanced.js"
8
+ },
9
+ "keywords": [
10
+ "hexo",
11
+ "renderer",
12
+ "mdx",
13
+ "markdown",
14
+ "jsx",
15
+ "react"
16
+ ],
17
+ "author": "Bryan0324",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/Bryan0324/hexo-renderer-mdx.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/Bryan0324/hexo-renderer-mdx/issues"
25
+ },
26
+ "homepage": "https://github.com/Bryan0324/hexo-renderer-mdx#readme",
27
+ "dependencies": {
28
+ "@mdx-js/mdx": "^3.0.0",
29
+ "react": "^18.2.0",
30
+ "react-dom": "^18.2.0"
31
+ },
32
+ "peerDependencies": {
33
+ "hexo": ">=5.0.0"
34
+ },
35
+ "engines": {
36
+ "node": ">=14.0.0"
37
+ }
38
+ }