-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ciro/fix-Bun.write
- Loading branch information
Showing
82 changed files
with
5,863 additions
and
1,299 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,55 +1,25 @@ | ||
use bun_native_plugin::{define_bun_plugin, BunLoader, OnBeforeParse}; | ||
use bun_native_plugin::{anyhow, bun, define_bun_plugin, BunLoader, Result}; | ||
use mdxjs::{compile, Options as CompileOptions}; | ||
use napi_derive::napi; | ||
|
||
#[macro_use] | ||
extern crate napi; | ||
|
||
define_bun_plugin!("bun-mdx-rs"); | ||
|
||
#[no_mangle] | ||
pub extern "C" fn bun_mdx_rs( | ||
args: *const bun_native_plugin::sys::OnBeforeParseArguments, | ||
result: *mut bun_native_plugin::sys::OnBeforeParseResult, | ||
) { | ||
let args = unsafe { &*args }; | ||
|
||
let mut handle = match OnBeforeParse::from_raw(args, result) { | ||
Ok(handle) => handle, | ||
Err(_) => { | ||
return; | ||
} | ||
}; | ||
|
||
let source_str = match handle.input_source_code() { | ||
Ok(source_str) => source_str, | ||
Err(_) => { | ||
handle.log_error("Failed to fetch source code"); | ||
return; | ||
} | ||
}; | ||
#[bun] | ||
pub fn bun_mdx_rs(handle: &mut OnBeforeParse) -> Result<()> { | ||
let source_str = handle.input_source_code()?; | ||
|
||
let mut options = CompileOptions::gfm(); | ||
|
||
// Leave it as JSX for Bun to handle | ||
options.jsx = true; | ||
|
||
let path = match handle.path() { | ||
Ok(path) => path, | ||
Err(e) => { | ||
handle.log_error(&format!("Failed to get path: {:?}", e)); | ||
return; | ||
} | ||
}; | ||
let path = handle.path()?; | ||
options.filepath = Some(path.to_string()); | ||
|
||
match compile(&source_str, &options) { | ||
Ok(compiled) => { | ||
handle.set_output_source_code(compiled, BunLoader::BUN_LOADER_JSX); | ||
} | ||
Err(_) => { | ||
handle.log_error("Failed to compile MDX"); | ||
return; | ||
} | ||
} | ||
let jsx = compile(&source_str, &options) | ||
.map_err(|e| anyhow::anyhow!("Failed to compile MDX: {:?}", e))?; | ||
|
||
handle.set_output_source_code(jsx, BunLoader::BUN_LOADER_JSX); | ||
|
||
Ok(()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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.