Code Block Syntax Highlighting - Text

I have been using ```text to create code blocks that have no syntax highlighting. I am now using Joplin 1.0.142 (Win10 x64) and have noticed that ```text now applies some kind of syntax highlighting. For instance it picks out odd words like “service” and highlights them orange. I also noticed that in blocks that contain a list of URLs “https://” is intially highlighted blue with the remainder of the url green and then after a few entries the whole url including https is green. Shouldn’t the text code block be without syntax highlighting?

2 Likes

That’s a good point. I’ve also noticed several issues with syntax highlighting.

But to answer your question: yes, a text code block should be without highlighting.

1 Like

Should I file this? It’s a really REALLY annoying bug.

For some stuff I used ````tex` which worked for me, but since that usually refers to LaTeX documents… I am sure the fact that I got it to work for me was just dumb luck.

Anyway. “text” should be the right qualifier. Yet, it doesn’t render correctly.

1 Like

Unfortunately text does not work. After some research I found out that one has to use plaintext instead. :frowning:

1 Like

This is not really a bug actually. There are two types of blocks in Joplin:

  • Fenced code blocks, that start with ``` and that are meant to contain syntax highlighted code. If that’s code you have, you should use this.
  • Preformatted text blocks, which are indented with four spaces or a tab, and are meant to display preformatted text (same as HTML PRE tag).

The problem is when you used fenced code block when all you want is preformatted text. In that case it will try to auto-detect code, and will obviously wrongly guess since it’s not code inside.

Perhaps we could indeed add a qualifier to fenced code block that would make it behave like preformatted text block. Is “text” a standard? What does StackExchange use for example?

1 Like

highlightjs uses plaintext. Maybe someone can open a feature request with them to make text an alias of plaintext

3 Likes

I was trying to find out how to do a code-block in a note and saw this post about using a triple tick to begin plain text code block. It seems to be working for me, but the other posts indicate there is still a problem. I am using version 1.0.167. Has this be resolved? Can I start any code-block with and end it with `
I don’t need highlighting in the code, I just want the code-block to be plain text.
Thanks.

Joplin doesn’t seem to have a problem that needs resolving and it works fine for me.

This is a simplified summary…

There is inline code which is where you surround a word or words within a line of text with a single backtick so `my phrase` gives my phrase.

You can achieve the same for a line by indenting the line with four spaces or a tab so:

    my phrase

Shows as

my phrase

Neither of these has any kind of code highlighting. They are just pre-formatted text blocks.

There are also code blocks where you start with three backticks and end with three backticks, with the backticks being on their own line. This is a “fenced code block”. This enables you to mark swathes of text as code. This code block however can have code highlighting to make the code easier to read

Of course unless you specify what type of code you have “fenced” the interpreter can only guess so you can tell it by adding a keyword after the first set of three backticks.

For instance

```plaintext

will tell the interpreter that everything that follows up to the closing set of backticks is plain text. (The interpreter Joplin uses appears to use plaintext whereas others, such as this forum, use text.)

```bash

will tell the interpreter that everything that follows up to the closing set of backticks is a bash script and it will highlight the code as such.

So, in Joplin

```plaintext
#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING
```

Shows as

#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING

But

```bash
#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING
```

Shows as

#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING

(the colours may vary in Joplin to what you see above but it will basically do the the same thing)

Other keywords I have seen mentioned on the web include,

apache
bash
coffeescript
cpp
cs
css
diff
go
html
http
ini
java
javascript
json
makefile
markdown
nginx
objectivec
perl
php
plaintext
python
ruby
shell
sql
xml

I have not tried them all so I cannot say if the interpreter Joplin uses understands all of them. Just try it and see!

Of course if you just want to ensure all your fenced blocks are plain text just put plaintext after the first three backticks and you are done.

3 Likes

Here’s a list of all the “languages” supported by highlight.js:

2 Likes

Laurent mentioned in another topic that not all are highlight languages are included, but only 20 or so.

1 Like

Laurent mentioned in another topic that not all are highlight languages are included, but only 20 or so.

Is there a way to change / add which languages are included. Almost all the code blocks for me are snippets of PowerShell code, which is supported by highlight.js but doesn't seem to be recognized in Joplin. I assume it isn't one of the 20 languages selected by Joplin.

1 Like

Bumping this as I too have a ton of Powershell code snippets as well and I would love to see them nicely displayed in the editor!

I presume that Editor.tsx has the full list of supported languages, as determined from the PYPL Popularity list (per the comment on line 36), though some languages like Java are not included, so I might be missing something there.

In addition, after some extra testing for the screenshot above, shell looks fine in the editor but doesn't render correctly in the Markdown viewer. Yet bash (and sh & zsh), which is not on the list in Editor.tsx, works just fine in both places. console, an alias of shell per the Highlight.js Supported Languages doc, also doesn't work. Perhaps this is a bug?

:man_shrugging:

I will note that, while there are an infinite number of use cases for a tool like Joplin, I would venture to guess that it's use as full IDE is not a high on the list; instead I am sure many people have notes that include code snippets of all types of languages, many of which won't ever make it to the PYPL list.
Stack Overflow, for example, changed the wording in their Developer Survey from "Programming Languages" to "Programming, Scripting, and Markup Languages" in 2018 to allow for a more inclusive response to their prompt. The result was the scripting language entry "Bash/Shell/Powershell*" making it's debut on the list in 2018 at ~40%. (*: 'Powershell' was added on to the entry in 2019)

I ultimately don't know exactly how the coloring is working in the editor but I certainly know it would be nice if all the languages in Highlight.js were supported!

2 Likes

i too use joplin to write down a lot of powershell code snippets, alas i don't think this would get much attention. and writing backticks every time becomes a chore now, not to mention having to explicitly write the syntax language for each code section .
i'm probably going to move on to vscode with notebooks for my md + code snipping needs, but i still want the same files available for joplin of course. going to look into importing or something else... let's see what already exist for tooling.

1 Like

This post is essentially a request for modifying Joplin documentation. Pls advise if I should post elsewhere.

There are likely many use-cases for notes that require plain text monospace formatting without any other formatting. I found @dpoulton 's post https://discourse.joplinapp.org/t/code-block-syntax-highlighting-text/2085/8 very useful in solving my needs for a particular use-case of this type (recording chords and lyrics). After using this post to solve my problem, I realised that a little updating of Joplin's markdown guide would reduce repetitive queries and user-frustration. I suggest these changes to the "Code block" section of that guide:

Change

Language is normally auto-detected, but it can also be specified:

to

Language is normally auto-detected, and the code will be formatted accordingly. But the language can also be specified:

Then, swap the sql example for the bash example in dpoulton's above-mentioned post, which shows obvious automated text formatting. Also, after the sql/bash example, I suggest adding the text:

For plain monospace text without additional formatting, specify 'plaintext' as the language.

Then, repeat dpoulton's bash example, but specify the language as "plaintext".

if someone googles this (as me), look in Default Formatter for Code Block as it seems GitHub - github/linguist: Language Savant. If your repository's language is being reported incorrectly, send us a pull request! is the used library, so linguist/languages.yml at master · github/linguist · GitHub is your list. (in my case, I figures out that RouterOS is supported :slight_smile: )

Would anyone be able to comment if this (linguist/languages.yml at master · github-linguist/linguist · GitHub) is currently the master list of supported languages for code blocks in Joplin?

It would be great to submit a PR to add the URL to Markdown Guide | Joplin