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.