Markdown input [support thread]

Support thread for Markdown input addon.

3 Likes

Reworking the functionality and adding buttons and badges, most of the rough edges now filed away. Please see addon page for details.

First of all, thanks a lot for this addon. Itā€™s something Iā€™ve really missed and I planned on doing it myself this summer. So thanks a lot.

Thereā€™s one thing thatā€™s broken and another that I donā€™t like:

  1. As you can see in the screenshot, the buttons arenā€™t not placed correctly. Iā€™ve fixed it by placing the button in the correct spot and Iā€™ve added some padding (the first button in the screenshot).
    So instead of adding the MD button to .rich-text-badge (which is a button btw) it should be added to .field-state.
    CleanShot 2022-12-13 at 04.56.35

  2. Changing this wasnā€™t fun, because youā€™ve minified everything in one single js file. Please keep your addonā€™s code unminified and only minify libraries etc. Or, if possible, leave the source so that we can minify it after changing stuff.

Thanks.

Edit:
css used for the outermost span of the md button:
padding-top:0.21em;
margin-left:-0.1em;


This happens quite often too. i.e. that the editor canā€™t load because of this error.

For my own addons, Iā€™ve simply used setTimeout() with 50-150ms to fix this issue. This works great because it will set it really far back in the JS event queue.

A more elegant solution I suppose would be to use an observer.

Otherwise, you could use the exposed Svelte runtime with lifecycle.onMount. But thereā€™s really no reason to.

Thanks

The source is available on GitHub, linked in the addon description, granted the link in the addon description doesnā€™t say ā€œGitHubā€ so Iā€™ll update that later.
image

Regarding the badge - the whole insertion is rather ā€œhackyā€ (there is also some significant ā€œhackynessā€ to the focus handling as well). The current placement ā€œinside the rich text badgeā€ is the result of trying to get the shading to sort of work while at the mean time keeping alignment to the right (I canā€™t remember the exact details, I just remember I mucked about a bit). It seems to work fine on my Windows machine so I am guessing itā€™s a setup or possibly OS thing (which is interesting since itā€™s all JS so there shouldnā€™t be that much of a difference):

Current version on my machine:

Moving the badge into focus-field, no other changes
image

The same goes for the badge alignment, the first add window screenshot above is with the badge inside .rich-text-badge, the second with the badge inside .focus-field, no changes made to the shipped alignment.

Preferably I would understand hot to properly interface with the Svelte code, I see you suggested lifecycle.onMount below, Iā€™ll look into that when I have some time.

In the mean time if you are knowledgeable in Svelte I am very open to hints on how to ā€œcorrectlyā€ interface with the Svelte code (mainly in editor.ts) as the current hacks are less than ideal for both elegance and maintainability.

This seems to be either a setup/OS/machine issue or some other addon interaction? I have been running the recently released version for almost a month on my own computer and havenā€™t experienced anything like the error you are presenting. The error seems to be occurring in the Svelte code (I donā€™t ever call context.get() so the origin of the error should be somewhere else). The addon doesnā€™t create or insert the CodeMirror instances until they are about to be shown, i.e. on load only the badges and shortcuts are inserted into the DOM reduce unnecessary overhead. Are you running with "Default field state": "Markdown"? Which version of Anki are you running? I donā€™t get the same source tree at all:
image

For info I have reworked some of the interface with the Svelte code to make it work on *nix. It also seems to work in a macOS VM but if itā€™s a timing question I may not catch that given the snail pace of the VM. Badge alignment I leave aside until I get some useful input.

Sorry for not getting back to you. Iā€™m having exams soon.

Anyways, Iā€™ve tested the new version and itā€™s much better. Thanks a lot. As for the alignment, I got it perfect with this CSS (some of your inline styles like margins etc have to be removed):

.markdown-input-badge span.badge {
    top: 0.09em; <<<<<< This is because I find that it looks better when slightly lower than center.
    position: relative;
    vertical-align: var(--icon-align, middle);
}

.markdown-input-badge svg{
    margin: 0 0.35em;
    fill: currentColor;
    vertical-align: unset;
}

CleanShot 2022-12-21 at 04.04.12

instead of

CleanShot 2022-12-21 at 04.05.44

One thing that might account for the alignment issues is that the parent of this bar uses flexbox. Thus the SVG can get squeezed. Hence the vertical-align: unset; to stop this from happening.

Also, itā€™s still bugging the editor from loading sometimes but as youā€™ve stated. Itā€™s possibly some addon interaction. However, that would be the only one out of 10+ editor add-ons.

System:
Version āØ2.1.54 (b6a7760c)ā©
Python 3.9.7 Qt 6.3.1 PyQt 6.3.1
MacOS

Ok, hereā€™s an non-tersed version of field_input.js, if you use that version we may be able to get some more info on the source of the error (the large size is since it contains CM6 and unified.js with extensions to convert HTML ā‡” MD).

I have reworked the badge insertion (cloning the rich text badge and changing only the necessary things, it seems to play well with the Svelte generated badges), for me this works well on Windows, Ubuntu (VM) and macOS (VM). I have also updated the non-tersed gist. Let me know if this version causes alignment issues, also I would be interested on more info regarding the failure to load if it happens again.

Hi, sorry for the late reply as usual. Iā€™ve tested the new version, the badges are perfect now.

It still doesnā€™t always let the editor load. Iā€™ll test the gist tomorrow I hope.

Thanks a lot for your efforts and skill.

Two questions

  1. Is it possible to display both the markdown editor and the visual editor by default, simultaneously?

  2. The prev/next keyboard shortcuts are not working for me. Iā€™m referring to these:

    {
        "Field input": {
            "Next field": "Ctrl+PgDown",
            "Previous field": "Ctrl+PgUp"
        }
    }

For more context:

  • Iā€™m on MacOS and using the external magic keyboard
  • Iā€™m pressing Ctrl + fn + up (or down)
  • And Iā€™m doing this while focused on a field in markdown mode

Iā€™ve tried using a different shortcut value, but still canā€™t get it to work.

At the moment there is no setting for showing both Markdown and rich text per default, adding that setting is simple enough though. However the focusing is a bit trickier, but probably solvable, but without some trickery the focus ends up in the rich text edit. I can upload a version with config option to have both open per default, however focus will be in the rich text edit.

Regarding the next/prev field - could you run Anki from the command line and see if you get any error output? Or install AnkiWebView Inspector and see if you get any errors in the javascript console? The shortcuts themselves should fire as they are on the python side:

        shortcuts.append(
            [QKeySequence(cfg[FIELD_INPUT][SC_NEXT]),
           lambda _ed=ed: ed.web.eval('MarkdownInput.cycle_next()')]
        )
        shortcuts.append(
            [QKeySequence(cfg[FIELD_INPUT][SC_PREV]),
           lambda _ed=ed: ed.web.eval('MarkdownInput.cycle_prev()')]
        )

Thanks for the awesome add-on, but there were a few issues I want to point out:

  1. When I first installed the add-on I couldnā€™t figure out how to make it work. While the details you provide on the addon page is great for a regular user, I almost gave up on it trying to figure out where to start. My suggestion would be a step-by-step tutorial, with the following:
  • How to install the add-on
  • What changes will happen in the application because of the addon?
  • The difference between the Rich-text editor, markdown editor and HTML editor. Which one to use when.
  • Styling. Anki has separate CSS styling for the codemirror, editor and note types. What to do with each of them
  1. I couldnā€™t figure out how to get code highlighting in my cards. What should I add to my card styling section? I tried adding: @import url("_styles_for_syntax_highlighting.css"); which was used in the older Syntax Highlighting addon.

Hi, thanks for the feedback!

When you say ā€œcode highlighting in my cardsā€ where do you mean?? Do you mean when editing in a ā€œMarkdown input fieldā€ or when reviewing?

  • For the actual Markdown input fields/dialog in the Add and Browser windows, syntax highlighting (i.e. Markdown syntax) should be enabled by default (if it isnā€™t something is wrong, if so please let me know). You can change the ā€œshippedā€ styling by:
    • Create a subdirectory in the addon folder named user_files
    • Copy cm.css in the addon directory into user_files
    • Edit cm.css in user_files (the addon will look for <addon dir>/user_files/cm.css and if that doesnā€™t exist it will load <addon dir>/cm.css, note that anything that is not inside user_files will get overwritten on addon update). There is also mdi.css in the addon directory but that contains layout information so it is not recommended to edit.
  • If you mean syntax highlighting for code blocks in the Markdown editor at the moment that isnā€™t implemented but I note that it shouldnā€™t be too hard to add, from the CM dingus Markdown code block highlighting
    import {basicSetup, EditorView} from "codemirror"
    import {markdown} from "@codemirror/lang-markdown"
    import {languages} from "@codemirror/language-data"
    
    // The Markdown parser will dynamically load parsers
    // for code blocks, using @codemirror/language-data to
    // look up the appropriate dynamic import.
    let view = new EditorView({
      doc: "Hello\n\n```javascript\nlet x = 'y'\n```",
      extensions: [
        basicSetup,
        markdown({codeLanguages: languages})
      ],
      parent: document.body
    })
    
  • For the styling of the HTML on reviewing you should modify the note template you are using.
  • For the styling of the Add and Browser windows themselves (i.e. everything outside of the Markdown text area) I recommend CSS Injector - Change default editor styles.

Hello, i would like to report a bug:

On the add note editor or browser note editor, if thereā€™s a table inside a field with this addon enabled AND that field is focused, specific functions become unresponsive:

  • all buttons which changes the contents of the field (including the keyboard shortcuts) stop working. e.g. formatting and cloze
  • toggle HTML editor, toggle markdown and collapse field buttons also stop working.
  • any change inside the editor is not reflected in the actual contents of the note. i.e. you can type text inside the HTML editor but Anki will not store the changes.
    • exception for add note editor: rich text changes however do get storedā€¦ kinda
  • for browser note editor: when switching to a different note, the fields content are not updated, ā€˜sticksā€™ to the previous note. this is better observed when switching between notes where both have tables.

This occurs on Anki v2.1.63-qt6 up to Anki v2.1.65rc1 with Markdown Input addon enabled. Does not occur when it is disabled. Couldnā€™t replicate this with other addons, or vanilla Anki. I am using this on Manjaro Linux with the official release from github instead of the AUR.

I found this error whilst looking at the chrome://inspect for the editor. seems like the addon isnā€™t recognising <table> nodes correctly? i donā€™t exactly know how to traceback to the source code instead of the uglified JS.

Thanks, update is out, should address this issue (and add some other features). Let me know if you find any bugs.

2 Likes

Thank you for this addon.
Works well on Anki Version āØ24.04 (429bc9e1)ā© Python 3.9.18 Qt 6.6.2 PyQt 6.6.1,
but I do not know whether it is intended or not, but changing configs via Config button in Ankiā€™s ā€œAdd-onsā€ window changes meta.json file.
config.json file seems to be unused.

Iā€™m running Markdown input on Anki 24.04.1 in Ubuntu 22.04.4 LTS. It works near flawlessly. I want to report some little unexpected behaviors:

In the configuration of the add-on, I changed the option Default field state to Markdown because I think that makes the default input to be Markdown instead of rich text. I restarted Anki and I could notice that the default input was Markdown. However, this also caused these unexpected behaviors:

  • When I open the Add dialog, the card type selected is Basic, and the default field that is selected is Back which is shown below the Front field. I believe the correct behavior would be that Front is the selected field because that is the default behavior when running Anki without add-ons and I didnā€™t intend to change that behavior when changing the Default field state.
  • In the Browse windows, when I click on the button Preview, the menu bar that contains that buttons gets hidden and the preview is not opened. Because of this, I need to close the Browse windows and open it again, but this is fruitless because the menu will hide again as soon as I click in Preview again.

For the time being, because the Preview button is hidden as soon as I try to click it, Iā€™m using the shortcut to preview cards Ctrl+Shift+P (Iā€™m running Ubuntu 22.04.4 LTS). UPDATE: Iā€™m now trying to click the button Cards..., but because the entire menu, which contains Fields..., Cards... and Preview, is hidden, I canā€™t click it and I donā€™t know a shortcut for opening it, so I have decided not to change Default field state to Markdown and instead use the default option (that is, rich text). This means that whenever I want to type Markdown, I have to press Ctrl+m. I wish I could set Default field state to Markdown and donā€™t have the unexpected behaviors that I mentioned above.