Skip to content
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

Disable syntax highlighting by default, or on stuff that doesn't look like code #508

Closed
ara4n opened this issue Dec 13, 2015 · 7 comments · Fixed by matrix-org/matrix-react-sdk#1074

Comments

@ara4n
Copy link
Member

ara4n commented Dec 13, 2015

A crappy heuristic like "needs a curly or right-angled bracket" might be sufficient for now.

@Xe
Copy link

Xe commented Dec 13, 2015

Here is valid code that doesn't have curly or angle brackets:

print "hello world"

function hi(x)
    print("hi " .. x)
end 
main :: IO ()
main = putStrLn "Hello world!"

@ara4n
Copy link
Member Author

ara4n commented Dec 13, 2015 via email

@Xe
Copy link

Xe commented Dec 13, 2015

does it support things like github flavored markdown language tagging?

```haskell
main :: IO ()
main = putStrLn "Hello World!"
```
main :: IO ()
main = putStrLn "Hello World!"

@aperezdc
Copy link

aperezdc commented Nov 14, 2016

GFM-style languge tagging for preformatted blocks can be done with something like the following (I have this working for my webpage):

marked.setOptions({
    highlight: function (code, lang) {
        return (lang === undefined) ? code : H.highlight(lang, code).value;
    },
});

Note that the above will disable highlighting for untagged preformatted code blocks, so it might be desirable to apply some heuristic anyway when lang === undefined.

Should I send a PR for matrix-react-sdk adding this?

@ara4n
Copy link
Member Author

ara4n commented Nov 15, 2016

sure - PR would be very welcome. The highlighting is currently done by highlight.js as a non-reactish DOM pass once the page has rendered. It's completely decoupled from marked currently. If there's a nice way to do make marked do the highlighting (whilst retaining highlight.js's heuristics if no language is marked) that'd be awesome.

https://github.com/matrix-org/matrix-react-sdk/blob/master/src/components/views/messages/TextualBody.js#L72 is the current super-stupid code.

@ara4n
Copy link
Member Author

ara4n commented Nov 15, 2016

just realised that making marked do this would shift responsibility to the client sending the message rather than displaying it, which is a bit of a pain in the ass. To fix this properly we'd need to finally get the "human representation of message" stuff landed in the spec to allow native markdown to be included in events, so the receiving client can then render the markdown as it liked.

I suspect a better bet would be to instead make the existing highlight.js code 'do the right thing' for syntax highlighting.

@aperezdc
Copy link

@ara4n: Yeah, I noticed also the shift in responsibility when tying this to marked. One idea would be to make the <code> elements have a lang="javascript" attribute added by marked when it renders the HTML, and then check for that and pass it to highlight.js when doing the syntax highlighting. WDYT?

kyrias added a commit to kyrias/matrix-react-sdk that referenced this issue Jun 12, 2017
The highlight.js autodetection is finicky and often wrong, so disable
highlighting unless the language was explicitly specified.

Fixes element-hq/element-web#508.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
kyrias added a commit to kyrias/matrix-react-sdk that referenced this issue Jun 12, 2017
The highlight.js autodetection is finicky and often wrong, so disable
highlighting unless the language was explicitly specified.

Fixes element-hq/element-web#508.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
kyrias added a commit to kyrias/matrix-react-sdk that referenced this issue Jun 26, 2017
The highlight.js autodetection is finicky and often wrong, so disable
highlighting unless the language was explicitly specified, or if the
user has explicitly enabled it in the settings.

Fixes element-hq/element-web#508.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants