Format part of a row as code

Hello,

Would you know how to format only a portion of a row as code (i.e. with the Courier font) ? As you know, using the

 
tags formats the entire row so I was wondering whether there was a workaround.

Thanks!

1 Like

I think you post is missing a part/piece of it?

You can make a piece/part to the text as Courier New by using HTML.

Just press Ctrl+Shift+X while editing a field and add an HTML tag around the text you would like to change the font. For example:

This text has 
the <span style="font-family:Courier; color:Blue; font-size:20px;">font 
Courier, is Blue, and 20px.</span>
1 Like

Thank you for your prompt answer. I was hoping to tweak the behaviour of the “code” button of the editor… Maybe I will tweak the behaviour of another button that I do not use like Strikethrough. Thanks!

It would be possible to code a simple add-on which inserts HTML around selected text (via hotkey/button) by taking a look at how the MathJax/formatting/etc insertion works.

Given that you are learning how to code, it might seem like a nice challenge to learn to code :wink:

I would probably recommend using a style specified in the style of the card, even if it means adjusting all your card types accordingly. Note that you can save custom style files in the media and import them via:

@import url("_style.css");

Aha, you got me on this one!

I second this! I was just about to create a new post but got a suggestion that the topic already exists.

Have you found a fix or an add-on that does this? It should be really easy to fix for someone who knows the plugin system (me myself have never touched it). All we need is a button (or a few…) that allows for custom mark-up, e.g. pressing the button would automatically surround your highlight with any tag of your choosing, in my case I would HAMMER that button so I don’t have to constantly manually write <code>, which autocompletes to <code></code>, then I’m forced to cut </code> and place it after the word…so very tedious.

Please please please, plugin-coders, create this add-on! =)

New Format Pack - AnkiWeb offers a code button.

You can also take a look at Custom Styles (font color, background colour, classes) - AnkiWeb, which allows you to surround text with anything you like and also set custom shortcuts.

Thanks for suggestions!

But the “New Format Pack” seems to only have the code block button, something which is already included in the “Mini Format Pack”, at least for me. I want to wrap inline text, e.g. a single word.

I think I’ll pass on your second tip, the instructions are filled with, "scary warning"s. It’s not worth it for the simple thing I want. It seems I have to create my own add-on, to be continued…

Those are mostly unwarranted :slight_smile: I guess the @ijgnd had some bad experiences with non-tech-savy users and felt the need to fend them off. It’s a really useful add-on, but admittedly overkill for your use case.

If you’re ready to write your own add-on, you might as well use New Format Pack as template and tweak it to insert an inline code element. Let me know if you need help with that.

Right after my answer I actually did a quick research into the Anki plugin system. I opened the main.py for the “Mini Format Pack” and took the naïve approach of modifying

    editor.web.eval("setFormat('strikeThrough')")

into

    editor.web.eval("setFormat('code')")

It didn’t work, I assume ‘code’ is not a built-in function. But then I googled “anki formatblock”, because that’s what’s used when applying the <pre></pre> wrap. I found the code for “Mini Format Pack” on Github and there’s actually a function called

def formatInlineCode(editor):
    editor.web.eval("wrap('<code>', '</code>')")

which is missing from my add-on. It seems the add-on is not synced with the lasted version on Github. Don’t know why. Anyway I took the quick fix and just plain replaced the body of my already existing function:

def formatBlockPre(editor):
    editor.web.eval("wrap('<code>', '</code>')")

It does exactly what I need…Except it adds those extremely annoying &nbsp; so if I have the word ‘float’, I end up with

&nbsp;<code>float</code>&nbsp;

I know I’m not alone in hating the extra &nbsp; that gets added everywhere. I manually always remove them and it eats a lot of time. EDIT(I know about the Find & Replace trick, but I can’t use that because sometimes I actually need the &nbsp;). They are really messing with my formatting. Maybe it’s a Qt thing.

I never use the strike-through tag, so one option is to overload the style to mimic a code-tag and don’t modify the add-on, for some reason the strike-through doesn’t add the &nbsp;. But that seems like a very ugly solution to mess with the markup. But I’m considering it.

1 Like

To familiarize myself with Svelte and the new editor API, I have created an addon that wraps the selected text with <code>. It uses range.insertNode() rather than wrap() function, so no &nbsp; is created. It’s a rush job so the quality of the code isn’t very good, but you can give it a try if you’d like.
https://github.com/hikaru-y/anki21-addon-format-button-sample/releases/tag/v0.0.1
inline-code-addon

4 Likes

Awesome, thanks! I will definitely take a look.