How do I enter Java code into Drafts?

I have a piece of Java code I’d like to store in Drafts, but I’m not quite sure about how to do it. I see javascript markdown but that’s not quite what I’m looking for.

Generally speaking, code is just plain text. You can store it in a draft, no problem.

Drafts does not have syntax highlighting for Java, if that is what you are looking for…it’s not a code editor, per se, but a notes app, so does not have a wide range of code language features.

1 Like

I’ve stuck javascript snippets in Drafts, tagged them as such to get them in a workspace, and used them from time to time. I don’t expect them to be formatted - and they aren’t.

The only problem is the title line would have to have // in front of it - which doesn’t look great. I don’t think this is solvable other than a ‘comment the first line and copy he whole snippet to the clipboard’ action.

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