-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(mdx): add support for MDX file handling and compilation #27047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benpsnyder
wants to merge
37
commits into
oven-sh:main
Choose a base branch
from
benpsnyder:feat/mdx-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,892
−72
Open
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
bf8f5c3
feat(mdx): add support for MDX file handling and compilation
benpsnyder af5930d
feat(mdx): enhance MDX support with new features and tests
benpsnyder 8200ac8
fix(mdx): improve handling of hard and soft breaks in inline content
benpsnyder d50efea
refactor(mdx): streamline MDX object creation and improve option parsing
benpsnyder bcf787e
fix(mdx): enhance error handling and improve option parsing
benpsnyder d948994
fix(tests): add symlink for node_modules in MDX test cases
benpsnyder 30a8235
fix(tests): refactor node_modules symlink handling in MDX tests
benpsnyder c4bab2f
fix(headers): update BunLoaderType constants for YAML, MD, and MDX
benpsnyder e36a785
fix(tests): correct exit code assertion in MDX tests
benpsnyder 29316ea
Merge branch 'main' into feat/mdx-support
benpsnyder 373e1e5
Merge branch 'main' into feat/mdx-support
benpsnyder 7654783
Merge branch 'main' into feat/mdx-support
benpsnyder c3dae08
Merge branch 'main' into feat/mdx-support
benpsnyder 5d96a59
Merge branch 'main' into feat/mdx-support
benpsnyder ef31f68
Merge branch 'main' into feat/mdx-support
benpsnyder 6c39971
Merge branch 'main' into feat/mdx-support
benpsnyder 6dc8092
Enhance MDX parsing to support multiline import/export statements and…
benpsnyder f754839
Merge branch 'main' into feat/mdx-support
benpsnyder 350db9b
Merge branch 'main' into feat/mdx-support
benpsnyder 29ae53e
Merge branch 'main' into feat/mdx-support
benpsnyder 6e377cc
fix(mdx): enhance argument handling to support file-like patterns
benpsnyder f9128d8
feat(mdx): improve hostname and port parsing in argument handling
benpsnyder 0ea6b3a
refactor(mdx): move duplicate argument filtering outside loop
benpsnyder 600460a
feat(mdx): enhance temporary directory creation with unique identifier
benpsnyder 06bbd3f
refactor(mdx): update start function to use new mdxInternal structure
benpsnyder 4ea0b68
refactor(jsx_renderer): remove unnecessary character handling for braces
benpsnyder 9ff374d
refactor(mdx): streamline statement handling in extractTopLevelStatem…
benpsnyder 2b24a50
feat(mdx): enhance expression handling to support comments and templa…
benpsnyder e3b97fc
feat(mdx): enhance frontmatter parsing and jsxImportSource handling
benpsnyder 2eecd40
fix(html, mdx): correct port increment logic in server configuration
benpsnyder 1ab504b
fix(bundle): update environment check for Windows to always true
benpsnyder 06e4d7b
feat(jsx_renderer): implement expression restoration and refactor wri…
benpsnyder 8a3b6d4
feat(mdx): implement in-memory MDX compilation and improve import res…
benpsnyder d76adff
Merge branch 'main' into feat/mdx-support
benpsnyder 68324e2
Merge branch 'main' into feat/mdx-support
benpsnyder d9f8c68
Merge branch 'main' into feat/mdx-support
benpsnyder 9c9c2a5
Merge branch 'main' into feat/mdx-support
benpsnyder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| pub fn create(globalThis: *jsc.JSGlobalObject) jsc.JSValue { | ||
| const object = JSValue.createEmptyObject(globalThis, 1); | ||
| object.put( | ||
| globalThis, | ||
| bun.String.static("compile"), | ||
| jsc.JSFunction.create(globalThis, "compile", compile, 2, .{}), | ||
| ); | ||
| return object; | ||
| } | ||
|
|
||
| pub fn compile( | ||
| globalThis: *jsc.JSGlobalObject, | ||
| callframe: *jsc.CallFrame, | ||
| ) bun.JSError!jsc.JSValue { | ||
| const input_value, const opts_value = callframe.argumentsAsArray(2); | ||
|
|
||
| if (input_value.isEmptyOrUndefinedOrNull()) { | ||
| return globalThis.throwInvalidArguments("Expected a string or buffer to compile", .{}); | ||
| } | ||
|
|
||
| var arena: bun.ArenaAllocator = .init(bun.default_allocator); | ||
| defer arena.deinit(); | ||
|
|
||
| const buffer = try jsc.Node.StringOrBuffer.fromJS(globalThis, arena.allocator(), input_value) orelse { | ||
| return globalThis.throwInvalidArguments("Expected a string or buffer to compile", .{}); | ||
| }; | ||
| const input = buffer.slice(); | ||
|
|
||
| const options = try parseOptions(globalThis, arena.allocator(), opts_value); | ||
| const result = mdx.compile(input, arena.allocator(), options) catch |err| return switch (err) { | ||
| error.OutOfMemory => globalThis.throwOutOfMemory(), | ||
| error.JSError, error.JSTerminated => |e| e, | ||
| error.StackOverflow => globalThis.throwStackOverflow(), | ||
| else => globalThis.throwValue(globalThis.createSyntaxErrorInstance("MDX compile error: {s}", .{@errorName(err)})), | ||
| }; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return bun.String.createUTF8ForJS(globalThis, result); | ||
| } | ||
|
|
||
| fn parseOptions(globalThis: *jsc.JSGlobalObject, allocator: std.mem.Allocator, opts_value: JSValue) bun.JSError!mdx.MdxOptions { | ||
| var options: mdx.MdxOptions = .{}; | ||
|
|
||
| if (opts_value.isObject()) { | ||
| inline for (@typeInfo(md.Options).@"struct".fields) |field| { | ||
| comptime if (field.type != bool) continue; | ||
| const camel = comptime camelCaseOf(field.name); | ||
| if (try opts_value.getBooleanLoose(globalThis, camel)) |val| { | ||
| @field(options.md_options, field.name) = val; | ||
| } else if (comptime !std.mem.eql(u8, camel, field.name)) { | ||
| if (try opts_value.getBooleanLoose(globalThis, field.name)) |val| { | ||
| @field(options.md_options, field.name) = val; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (try opts_value.get(globalThis, "jsxImportSource")) |import_source_value| { | ||
| if (!import_source_value.isUndefinedOrNull()) { | ||
| if (!import_source_value.isString()) { | ||
| return globalThis.throwInvalidArguments("jsxImportSource must be a string", .{}); | ||
| } | ||
| const str = try import_source_value.toBunString(globalThis); | ||
| defer str.deref(); | ||
| if (!str.isEmpty()) { | ||
| const utf8 = str.toUTF8(allocator); | ||
| defer utf8.deinit(); | ||
| options.jsx_import_source = try allocator.dupe(u8, utf8.slice()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return options; | ||
| } | ||
|
|
||
| fn camelCaseOf(comptime snake: []const u8) []const u8 { | ||
| return comptime brk: { | ||
| var count: usize = 0; | ||
| for (snake) |c| { | ||
| if (c != '_') count += 1; | ||
| } | ||
| if (count == snake.len) break :brk snake; | ||
|
|
||
| var buf: [count]u8 = undefined; | ||
| var i: usize = 0; | ||
| var cap_next = false; | ||
| for (snake) |c| { | ||
| if (c == '_') { | ||
| cap_next = true; | ||
| } else { | ||
| buf[i] = if (cap_next and i != 0 and c >= 'a' and c <= 'z') c - 32 else c; | ||
| i += 1; | ||
| cap_next = false; | ||
| } | ||
| } | ||
| const final = buf; | ||
| break :brk &final; | ||
| }; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const bun = @import("bun"); | ||
| const jsc = bun.jsc; | ||
| const md = bun.md; | ||
| const mdx = bun.md.mdx; | ||
| const std = @import("std"); | ||
| const JSValue = jsc.JSValue; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.