This repository was archived by the owner on Mar 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlib.rs
320 lines (262 loc) · 9.95 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use rust_web_markdown::{
render_markdown, ElementAttributes, HtmlElement, Context,
CowStr,
MarkdownProps
};
pub type MdComponentProps = rust_web_markdown::MdComponentProps<View>;
pub use rust_web_markdown::{
LinkDescription, Options, ComponentCreationError
};
use web_sys::MouseEvent;
use leptos::*;
use leptos::html::AnyElement;
use std::collections::BTreeMap;
use core::ops::Range;
#[cfg(feature="debug")]
pub mod debug {
use super::*;
#[derive(Copy, Clone)]
pub struct EventInfo(pub WriteSignal<Vec<String>>);
}
#[derive(Clone, Debug)]
pub struct MarkdownMouseEvent {
/// the original mouse event triggered when a text element was clicked on
pub mouse_event: MouseEvent,
/// the corresponding range in the markdown source, as a slice of [`u8`][u8]
pub position: Range<usize>,
// TODO: add a clonable tag for the type of the element
// pub tag: pulldown_cmark::Tag<'a>,
}
/// component store.
/// It is called when therer is a `<CustomComponent>` inside the markdown source.
/// It is basically a hashmap but more efficient for a small number of items
pub struct CustomComponents(BTreeMap<&'static str,
Callback<MdComponentProps, Result<View, ComponentCreationError>>
>);
impl Default for CustomComponents {
fn default() -> Self {
Self (Default::default())
}
}
impl CustomComponents
{
pub fn new() -> Self {
Self(Default::default())
}
/// register a new component.
/// The function `component` takes a context and props of type `MdComponentProps`
/// and returns html
pub fn register<F, I>(&mut self, name: &'static str, component: F)
where F: Fn(MdComponentProps) -> Result<I, ComponentCreationError> + 'static,
I: IntoView
{
let closure = move |props| component(props).map(|x| x.into_view());
self.0.insert(name, Callback::new(closure));
}
}
impl<'a> Context<'a, 'static> for &'a __MdProps {
type View = View;
type Handler<T: 'static> = Callback<T, ()>;
type MouseEvent = MouseEvent;
fn props(self) -> rust_web_markdown::MarkdownProps<'a> {
MarkdownProps {
hard_line_breaks: self.hard_line_breaks.get(),
wikilinks: self.wikilinks.get(),
parse_options: self.parse_options.as_ref(),
theme: self.theme.as_deref(),
}
}
#[cfg(feature="debug")]
fn send_debug_info(self, info: Vec<String>) {
let set_event_info = use_context::<debug::EventInfo>();
if let Some(setter) = set_event_info {
setter.0.set(info);
}
}
fn el_with_attributes(
self,
e: HtmlElement,
inside: Self::View,
attributes: ElementAttributes<Callback<MouseEvent>>,
) -> Self::View {
let mut r: leptos::HtmlElement<AnyElement> = match e {
HtmlElement::Div => html::div().into_any(),
HtmlElement::Span => html::span().into_any(),
HtmlElement::Paragraph => html::p().into_any(),
HtmlElement::BlockQuote => html::blockquote().into_any(),
HtmlElement::Ul => html::ul().into_any(),
HtmlElement::Ol(s) => html::ol().attr("start", s).into_any(),
HtmlElement::Li => html::li().into_any(),
HtmlElement::Heading(1) => html::h1().into_any(),
HtmlElement::Heading(2) => html::h2().into_any(),
HtmlElement::Heading(3) => html::h3().into_any(),
HtmlElement::Heading(4) => html::h4().into_any(),
HtmlElement::Heading(5) => html::h5().into_any(),
HtmlElement::Heading(6) => html::h6().into_any(),
HtmlElement::Heading(_) => panic!(),
HtmlElement::Table => html::table().into_any(),
HtmlElement::Thead => html::thead().into_any(),
HtmlElement::Trow => html::tr().into_any(),
HtmlElement::Tcell => html::td().into_any(),
HtmlElement::Italics => html::i().into_any(),
HtmlElement::Bold => html::b().into_any(),
HtmlElement::StrikeThrough => html::s().into_any(),
HtmlElement::Pre => html::pre().into_any(),
HtmlElement::Code => html::code().into_any(),
};
r = r.child(inside);
if let Some(s) = attributes.style {
r = r.attr("style", s.to_string())
}
if let Some(c) = attributes.on_click {
r = r.on(ev::click, move |e| Callable::call(&c, e));
}
r = r.classes(attributes.classes.join(" "));
r.into_view()
}
fn el_span_with_inner_html(self, inner_html: String, attributes: ElementAttributes<Callback<MouseEvent>>) -> Self::View {
let mut r = view!{
<span inner_html=inner_html></span>
}.into_any();
if let Some(s) = attributes.style {
r = r.attr("style", s.to_string())
}
if let Some(c) = attributes.on_click {
r = r.on(ev::click, move |e| Callable::call(&c, e));
}
r = r.classes(attributes.classes.join(" "));
r.into_view()
}
fn el_hr(self, attributes: ElementAttributes<Callback<MouseEvent>>) -> Self::View {
let mut r = html::hr();
if let Some(s) = attributes.style {
r = r.attr("style", s.to_string())
}
if let Some(c) = attributes.on_click {
r = r.on(ev::click, move |e| Callable::call(&c, e));
}
r = r.classes(attributes.classes.join(" "));
r.into_view()
}
fn el_br(self) -> Self::View {
view! {<br/>}.into_view()
}
fn el_fragment(self, children: Vec<Self::View>) -> Self::View {
children.into_iter().collect()
}
fn el_a(self, children: Self::View, href: String) -> Self::View {
view! {<a href={href.to_string()}>{children}</a>}.into_view()
}
fn el_img(self, src: String, alt: String) -> Self::View {
view! {<img src={src.to_string()} alt={alt.to_string()}/>}.into_view()
}
fn el_text(self, text: CowStr<'a>) -> Self::View {
text.to_string().into_view()
}
fn mount_dynamic_link(self, rel: &str, href: &str, integrity: &str, crossorigin: &str) {
let document = document();
let link = document.create_element("link").unwrap();
link.set_attribute("rel", rel).unwrap();
link.set_attribute("href", href).unwrap();
link.set_attribute("integrity", integrity).unwrap();
link.set_attribute("crossorigin", crossorigin).unwrap();
document.head()
.unwrap()
.append_child(&link).unwrap();
}
fn el_input_checkbox(self, checked: bool, attributes: ElementAttributes<Callback<MouseEvent>>) -> Self::View {
let mut r = html::input()
.attr("type", "checkbox")
.attr("checked", checked)
;
if let Some(c) = attributes.on_click {
r = r.on(ev::click, move |e| Callable::call(&c, e));
}
r = r.classes(attributes.classes.join(" "));
if let Some(s) = attributes.style {
r = r.attr("style", s.to_string())
}
r.into_view()
}
fn call_handler<T: 'static>(callback: &Self::Handler<T>, input: T) {
Callable::call(callback, input)
}
fn make_md_handler(self, position: Range<usize>, stop_propagation: bool) -> Self::Handler<MouseEvent> {
match self.on_click {
Some(f) => {
let position = position.clone();
Callback::new(move |e: MouseEvent| {
if stop_propagation {
e.stop_propagation()
}
let report = MarkdownMouseEvent {
position: position.clone(),
mouse_event: e
};
Callable::call(&f, report)
})
}
None => Callback::new(move |_| ())
}
}
fn set_frontmatter(self, frontmatter: String) {
if let Some(setter) = self.frontmatter {
setter.set(frontmatter)
}
}
fn has_custom_links(self) -> bool {
self.render_links.is_some()
}
fn render_links(self, link: LinkDescription<Self::View>)
-> Result<Self::View, String> {
Ok(Callable::call(&self.render_links.unwrap(), link))
}
fn has_custom_component(self, name: &str) -> bool {
self.components.0.get(name).is_some()
}
fn render_custom_component(self, name: &str, input: MdComponentProps)
-> Result<Self::View, rust_web_markdown::ComponentCreationError> {
let f = self.components.0.get(name).unwrap();
f.call(input)
}
}
#[component]
#[allow(unused)]
pub fn __Md(
/// the markdown text to render
#[prop(into)]
src: MaybeSignal<String>,
/// 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, into)]
on_click: Option<Callback<MarkdownMouseEvent>>,
///
#[prop(optional, into)]
render_links: Option<Callback<LinkDescription<View>, leptos::View>>,
/// the name of the theme used for syntax highlighting.
/// Only the default themes of [syntect::Theme] are supported
#[prop(optional, into)]
theme: Option<String>,
/// wether to enable wikilinks support.
/// Wikilinks look like [[shortcut link]] or [[url|name]]
#[prop(optional, into)]
wikilinks: MaybeSignal<bool>,
/// wether to convert soft breaks to hard breaks.
#[prop(optional, into)]
hard_line_breaks: MaybeSignal<bool>,
/// pulldown_cmark options.
/// See [`Options`][pulldown_cmark_wikilink::Options] for reference.
#[prop(optional, into)]
parse_options: Option<Options>,
#[prop(optional, into)]
components: CustomComponents,
#[prop(optional, into)]
frontmatter: Option<WriteSignal<String>>
) -> impl IntoView {
()
}
#[allow(non_snake_case)]
pub fn Markdown(props: __MdProps) -> impl IntoView {
move || render_markdown(&props, &props.src.get())
}