How do I enter Java code into Drafts?

The original poster is referring to Java rather than JavaScript, but I have JavaScript code in Drafts and I do see syntax highlighting.

E.g.

It doesn’t have to, my example above doesn’t. If you need to search for Drafts by title in a list or explicitly by title in a more general search, then having a suitable title is absolutely beneficial if not essential. If you are not doing that, then it becomes a nice to have.

By adding the title as a JavaScript comment you can then copy the whole draft content without any issue for reuse elsewhere. In doing so you may also be bringing across a redundant title.

You could also manually copy everything but the first line, or have an action to copy the draft body (all but the first line/title). This I think provides a ‘cleaner’ approach.

Note also that you could use and have a plain text title as you are not going to be grabbing it for use outside of Drafts. You could even have a modified Drafts JavaScript syntax to help you highlight the first line in some way or even give it a leading hash (as draft titles effectively ignore any leading octothorpes) and reuse some Markdown syntax to make your custom JavaScript-Draft syntax.

As Greg has already noted, you can store any regular text in a draft. Plain Text would probably be the best inbuilt option. Here’s how I have plain text files to display, using a Java function as an example.

There are a number of downloadable syntaxes in the Drafts Directory that can apply some syntax highlighting in Drafts. None of them are for Java code.

You can build your own syntax. Probably not something to immediately undertake if you are new to Drafts, and syntax highlighting for code is generally more complex than for mark up languages like the various flavours of Markdown. This is largely because of the range of syntax and keywords that you might wish to accommodate. Note also that the more complex the sytnax highlighting, the more processing it takes to render.

It isn’t the same format, but the following GitHub repo has definitions for use with VIM. Take note of the list of keywords and the regular expressions used for matching. It could give you a head start, and/or illustrate the complexity of creating a syntax highlighter for Java.

Hope that helps.

1 Like