Skip to content

Commit

Permalink
+ rhai - add md::enclosed_block_content_or_raw (rel #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Oct 12, 2024
1 parent bde232a commit 4d912c7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/jeremychone/rust-devai"

[lints.rust]
unsafe_code = "forbid"
# unused = { level = "allow", priority = -1 } # For exploratory dev.
unused = { level = "allow", priority = -1 } # For exploratory dev.

[dependencies]
# -- Async
Expand Down
24 changes: 22 additions & 2 deletions src/script/rhai_script/rhai_modules/rhai_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ pub fn rhai_module() -> Module {

FuncRegistration::new("extract_blocks")
.in_global_namespace()
.set_into_module(&mut module, extract_blocks_with_name);
.set_into_module(&mut module, extract_blocks_with_lang);

FuncRegistration::new("outer_delimited_block_content_or_raw")
.in_global_namespace()
.set_into_module(&mut module, outer_delimited_block_content_or_raw);

module
}
Expand Down Expand Up @@ -52,10 +56,26 @@ fn extract_blocks(md_content: &str) -> RhaiResult {
/// returning only the blocks with a
/// [language identifier](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#syntax-highlighting)
/// that matches `lang_name`.
fn extract_blocks_with_name(md_content: &str, lang_name: &str) -> RhaiResult {
fn extract_blocks_with_lang(md_content: &str, lang_name: &str) -> RhaiResult {
let blocks: Vec<MdBlock> = md::MdBlocks::new(md_content, Some(lang_name)).collect();
let blocks: Vec<Dynamic> = blocks.into_iter().map(MdBlock::into_dynamic).collect();
Ok(blocks.into())
}

/// ## RHAI Documentation
/// ```rhai
/// outer_delimited_block_content_or_raw(md_content: &str) -> Vec<MdBlock>
/// ```
///
/// Without fully parsing the markdown, this function attempts to extract the content from the first triple backticks
/// until the last triple backticks.
/// If no start/end triple backticks are found, it will return the raw content.
///
/// > Note: This is useful in the genai context because often LLMs return a top block (e.g., markdown, Rust)
/// > which might have other ` ``` ` in the middle but should be interpreted as nested.
/// > (GenAI does not seem to know about the 6 ticks for top level)
fn outer_delimited_block_content_or_raw(md_content: &str) -> String {
md::outer_delimited_block_content_or_raw(md_content)
}

// endregion: --- Rhai Functions
2 changes: 2 additions & 0 deletions src/support/md/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// region: --- Modules

mod md_blocks;
mod outer_block;

pub use md_blocks::*;
pub use outer_block::*;

// endregion: --- Modules

0 comments on commit 4d912c7

Please sign in to comment.