Skip to content

Commit

Permalink
moved parsing to its own crate and use custom Callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
rambip committed Aug 3, 2023
1 parent fb391ef commit 1730e51
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 564 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "leptos-markdown"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
leptos = {version="0.4.6", features=["csr", "nightly"]}

pulldown-cmark = { git = "https://github.com/rhysd/pulldown-cmark.git", branch="math"}
pulldown-cmark-wikilink = {git="https://github.com/rambip/pulldown-cmark-wikilink"}
syntect = { version = "5.0.0", default-features = false, features = ["default-fancy"]}
katex = {version="0.4", default-features=false, features=["wasm-js"]}

Expand Down
2 changes: 1 addition & 1 deletion examples/onclick/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn App(cx: Scope) -> impl IntoView {

view!{cx,
<div>
<Markdown src=MARKDOWN_SOURCE on_click=Box::new(onclick)/>
<Markdown src=MARKDOWN_SOURCE on_click=onclick/>
<br/>
<hr/>
<p>{"markdown source:"}</p>
Expand Down
35 changes: 17 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![feature(slice_group_by)]
#![feature(array_into_iter_constructors)]

use leptos::*;
use leptos::html::AnyElement;

Expand All @@ -11,10 +8,10 @@ pub use render::HtmlError;

use web_sys::MouseEvent;

mod parse;
use parse::{parse, default_options};
use pulldown_cmark_wikilink::{Parser, Options, LinkType};

mod utils;
use utils::Callback;

use core::ops::Range;

Expand All @@ -32,7 +29,7 @@ pub struct LinkDescription {
pub title: String,

/// the type of link
pub link_type: pulldown_cmark::LinkType,
pub link_type: LinkType,

/// wether the link is an image
pub image: bool,
Expand All @@ -50,6 +47,7 @@ pub struct MarkdownMouseEvent {
// pub tag: pulldown_cmark::Tag<'a>,
}


#[component]
pub fn Markdown(
cx: Scope,
Expand All @@ -61,12 +59,12 @@ pub fn Markdown(
/// the callback called when a component is clicked.
/// if you want to controll what happens when a link is clicked,
/// use [`render_links`][render_links]
#[prop(optional)]
on_click: Option<Box<dyn Fn(MarkdownMouseEvent)>>,
#[prop(optional, into)]
on_click: Option<Callback<MarkdownMouseEvent, ()>>,

///
#[prop(optional)]
render_links: Option<Box<dyn Fn(LinkDescription) ->
#[prop(optional, into)]
render_links: Option<Callback<LinkDescription,
Result<HtmlElement<AnyElement>, HtmlError>>>,

/// the name of the theme used for syntax highlighting.
Expand All @@ -82,30 +80,31 @@ pub fn Markdown(
/// modify parse options.
/// It take the default parse options and returns the options you want to enanble.
/// For wikilinks, see the `wikilinks` prop.
#[prop(optional)]
parse_options: Option<Box<dyn Fn(pulldown_cmark::Options) -> pulldown_cmark::Options>>,
#[prop(optional, into)]
parse_options: Option<Callback<Options, Options>>,

) -> impl IntoView
{
let context = RenderContext::new(
cx,
theme,
on_click,
render_links
render_links,
);

let options = match parse_options {
Some(f) => f(default_options()),
None => default_options()
Some(f) => f.call(Options::all()),
None => Options::all(),
};

view! {cx,
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-3UiQGuEI4TTMaFmGIZumfRPtfKQ3trwQE2JgosJxCnGmQpL/lJdjpcHkaaFwHlcI" crossorigin="anonymous"/>
<div style="width:100%; padding-left: 10px">
{move || src.with( |x| {
let stream = parse(x, &options, wikilinks);
log!("{stream:?}");
Renderer::new(&context, &stream).collect_view(cx)
let stream : Vec<_> = Parser::new_ext(x, options, wikilinks)
.into_offset_iter()
.collect();
Renderer::new(&context, &stream).collect_view(cx)
})
}
</div>
Expand Down
Loading

0 comments on commit 1730e51

Please sign in to comment.