Escaping Special Characters ONLY in Documents (not Styles, Separators, Etc)

It would be helpful for my workflow if I could have MultiMarkdown compilation escape special characters in my documents (where I can apply formatting directly) but not in anything added during compile (e.g. separators with raw markup in them). Is there a way to set Scrivener to do this?

If you can make a minimal test project and specify your output targets it will be easier to help, given the range of possible options during compile (like you have enabled escaping due to wanting to convert RTF?). In general you can “protect” content using raw_attributes so for example:

This is proteted `\myblah()`{=latex} for latex outputs
1 Like

Sorry if I wasn’t clear. I convert the rich text in my Files to markdown for post-processing, and want to escape any “special characters” in those Files, but I also want to include raw markup in things like Styles prefixes and suffixes. Maybe I’m going about this the wrong way, but for example, I want to put a string around BLOCK QUOTE-ed text that I can postprocess later, and I don’t want those characters escaped, but I do want similar characters in the Files escaped. Is there a way to “protect” text in the Style Prefix/Suffix fields from the " Convert Rich Text to Markdown options" → “Escape Special Characters” option?

In general, I never use “Convert Richtext
” for exactly these reasons, it devolves into a complex set of when things are escaped or not. Styles can replace all RTF use, and then you don’t need to worry about escaping


Anyway, lets say you have “Convert richtext
” ON and “Escape special characters” ON — the following simulated scrivener text has a smallcaps style attached to “CUPCAKE” (which for output will be converted to [Cupcake]{.smallcaps}) and RTF italic on “ipsum dolor.”

CUPCAKE ipsum dolor.

This causes the problem you specify when compiled, the style markdown gets escaped:

\[Cupcake\]\{.smallcaps\} *ipsum dolor*

So the RTF was correctly converted BUT the style markdown was escaped, this is your problem?

If you turn ON “Treat as raw” for that style then compile generates this:

[Cupcake]{.smallcaps} *ipsum dolor*.

So now both the style markdown and the converted markdown should render fine. Separators don’t seem to be escaped so markdown is left untouched AFAICT.

1 Like

Could you expand on this? It sounds like my problem might be that I’m using the wrong tool for this job


Well, for what or why are you using RTF direct formatting?

Only a specific set of things will translate from RTF to markdown anyway, and all of those things can be better handled by Scrivener’s styles. My suggestion is turn Convert richtext
 OFF, and where you have been using RTF direct formatting, make a style. You can even bind ⌘b and ⌘i to apply the style rather than bold or italic direct formatting. Now you don’t need to worry about escaping, and you get the flexibility that if you want bold to be represented by small capitals, just reassign the style rule in the compiler


1 Like

The problem with just using Styles is that I do have literal symbols like *s in my text that need escaping to \*. Otherwise I could just turn ‘Escape Special Characters’ off completely. However, with ‘Escape Special Characters’ turned on, my ~~ strike-through syntax becomes \~\~.

Maybe the easiest option is to just put the escapes directly in the text and turn off ‘Escape Special Characters’. It’s not pretty but if there’s no easy way to protect text in the Styles field from escapement it would be a simple fix.

Why not use styles for escapes? The style can add the \ as required. Also your strike through could be a style too, that way you have no pollution of markup peppering your text, styles provide the semantic “containers” to provide your intent. This way, say you want to compile to another output, there are no escape or markdown specific characters peppering your work. This ‘separation of concerns’ gets you clean text with a flexible path to output what you need


1 Like

Maybe I explained it badly, but it sounds like you’re trying to explain how to do the reverse process that I actually want. I have a strike-through style defined to prefix/suffix ~~ around the struck-through text, so struck becomes ~~struck~~ just like bold and italics become **bold** and *italics*. The problem is that I want to have text like aster*sk render to ~~aster\*sk~~, when I can only get Scrivener to output either ~~aster*sk~~ or \~\~aster\*sk\~\~.

Ah, well then one solution is to live with the escapes in the text (only use styles for your markdown):

In this case a strike-through style is applied with the escaped \* as plain text. I’ve also added an emphasis and strong style just to show how you can make your text “look” like RTF. This compiles to:

Muffin ~~dan\*ish~~ chocolate *soufflé* pastry icing bonbon **oat cake**.

So now styles will replace all markdown, and escapes are explicit. If compile replacements could be made to optionally run before styles were transformed, we could take benefit of replacements to escape the asterisks, but at least on macOS they run after styles and so regex replacements also operate on any injected markdown. There may be some other tweaks that could be performed.

2 Likes