Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Integrating highlight.js with CRM eaxmple #1073

Closed
shivshankardayal opened this issue Apr 4, 2020 · 12 comments
Closed

Integrating highlight.js with CRM eaxmple #1073

shivshankardayal opened this issue Apr 4, 2020 · 12 comments
Labels

Comments

@shivshankardayal
Copy link

Question

How can I integrate with highlight.js for syntax highlighting with pulldown_cmark in CRM example?

@jstarry
Copy link
Member

jstarry commented Apr 4, 2020

Hey @shivshankardayal, good question!

If you're using stdweb, JS interop works with the js! macro. If you're using web-sys, you will want to use wasm-bindgen. Please let me know which one :)

Since Yew is similar to React, you can see how highlight.js works with React. See this example: https://github.com/bvaughn/react-highlight.js/blob/master/src/Highlight.js#L20

You will want to call highlightBlock for your code.

I would really like if you added highlight.js to the CRM example so that others can learn too, thank you!

@shivshankardayal
Copy link
Author

I am still a noob but I will try to do that.

@shivshankardayal
Copy link
Author

@jstarry How to build yew as cargo gives a compile time error 'Yew requires selecting either the web_sysorstd_web cargo feature', build.rs:9:9. How do I select one of these? Documentation is lacking on it.

@jstarry
Copy link
Member

jstarry commented Apr 5, 2020

@shivshankardayal check out how to use features here: https://doc.rust-lang.org/cargo/reference/features.html

There are instructions in the examples/README.md here: https://github.com/yewstack/yew/blob/8edf136da6ba1955c847c5860ec55623a27c08e9/examples/README.md#running-the-examples

Here's how you specify in Cargo.toml:

yew = { path = "../../..", features = ["services", "web_sys"] }

@shivshankardayal
Copy link
Author

@jstarry Thanks a lot. I went through highlight.js a bit. First I will integrate it with CRM library then I would try to convert highlight.js to a rust library which can be used with something like pulldown-cmark.

@jstarry
Copy link
Member

jstarry commented Apr 5, 2020

Nice! So you want to add syntax highlighting to static html instead of apply it dynamically in the browser?

@shivshankardayal
Copy link
Author

No I want a Webassembly library of highlight.js which can do it in browser.

@jstarry
Copy link
Member

jstarry commented Apr 5, 2020

I see, why do you want to rewrite highlight.js in Rust? Performance?

@shivshankardayal
Copy link
Author

It is just a purist approach. Also, it will help gain momentum for webassembly.

@jstarry
Copy link
Member

jstarry commented Apr 5, 2020

Excellent, looking forward to it 👍

@oovm
Copy link

oovm commented Oct 9, 2020

Hi, @shivshankardayal, I also need a static highlight.

How about syntect, it can be used as following:

use syntect::parsing::SyntaxSet;
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use yew::Html;

fn highlight_html(s: &str) -> String {
    let ss = SyntaxSet::load_defaults_newlines();
    let ts = ThemeSet::load_defaults();
    let syntax = ss.find_syntax_by_token("rs").unwrap_or_else(|| ss.find_syntax_plain_text());
    highlighted_html_for_string(&s, &ss, syntax, &ts.themes["base16-ocean.dark"])
}

pub fn highlight_vdom(s: &str) -> Html {
    let div = yew::utils::document().create_element("div").unwrap();
    div.set_inner_html(highlight_html(s));
    Html::VRef(div.into())
}

A better way is to re-implement the ast transformation, which can bring greater flexibility

@ranile
Copy link
Member

ranile commented Oct 10, 2020

@GalAster, using syntect in this current form on WASM is not a good idea.

   let ss = SyntaxSet::load_defaults_newlines();
   let ts = ThemeSet::load_defaults();

This will increase your WASM binary size by around 2MBs for no good reason (loading unused syntaxes and themes). You should be only loading what you need (see: trishume/syntect#135 (comment))

Also, syntect can use either rust-onig or fancy-regex.
If you use the default regex-onig feature, rust-onig will not compile on WASM target. See rust-onig/rust-onig#153.
Using fancy-regex is slow and from my experience, slow enough to noticeably block the main thread so it needs to be called from a web worker which (until trunk-rs/trunk#46 is fixed) is a hassle at best.
There's also no way of knowing if using regex-onig feature would fix the performance issue.

@yewstack yewstack locked and limited conversation to collaborators Oct 6, 2021
@mc1098 mc1098 closed this as completed Oct 6, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

5 participants