Open pilgrimlyieu opened 2 years ago
Wow thanks so much for your effort, I'll link this post to the people that asked for basic (type in the answer). Really happy that you're helping the community, sorry that I kind of abandoned this project because of my limited time
Thanks for the tutorial, @pilgrimlyieu. I followed the steps and everything is working except the syntax highlighting. The <code>
block has the correct class of language-python
but no syntax highlighting.
Thanks for the tutorial, @pilgrimlyieu. I followed the steps and everything is working except the syntax highlighting. The
<code>
block has the correct class oflanguage-python
but no syntax highlighting.
Sorry for my late reply. Because of the limited time, I can not maintain this tutorial well even I've learnt more and am about to share.
Can you give me an example or a screenshot? I've tested it and it works normally in my circumstance.
```python
import sys
print('hello world')
def test(arg1, arg2):
if arg1 != arg2:
return arg1 - arg2
print(test(2, 3))
Thanks for your reply. Your code example looks great. Here is my Anki version info and some screenshots.
Anki 2.1.54 (b6a7760c) Python 3.9.7 Qt 6.3.1 PyQt 6.3.1
Platform: Mac 12.4
Flags: frz=True ao=True sv=2
@keithadams Can you show me the source code? If _highlight.js
can not render python
code, its warning should be
Could not find the language 'python', did you forget to load/include a language module?
Is this your Code Example
source code?
```python
import sys
print('hello world')
def test(arg1, arg2):
if arg1 != arg2:
return arg1 - arg2
print(test(2, 3))
If not, can you show me your source code? Press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>X</kbd> to copy.
Is this your Code Example source code?
@pilgrimlyieu Yes. I used that exact code for the Front field.
@pilgrimlyieu Yes. I used that exact code for the Front field.
Confusing. Can you show me the code of your card(including front and back side HTML and styling CSS)? I'll have a check.
Github Gist of my Front Template, Back Template, and Styling.
Github Gist of my Front Template, Back Template, and Styling.
Works normally in my case too. Can you try to disable other addons to check if it is the issue of addons?
Yes, I disabled all addons and restarted Anki but have the same result 🤷
Yes, I disabled all addons and restarted Anki but have the same result 🤷
I have no idea temporarily. Maybe you can unzip and copy contents of this Anki-Markdown-KaTeX.zip
package to ~/collection.media
to have a try.
That worked! There must be something wrong with the files my system is pulling down. I'll take a look at the files later today and see if I can determine the culprit. I'll post my results. Thanks for your help!
That worked! There must be something wrong with the files my system is pulling down. I'll take a look at the files later today and see if I can determine the culprit. I'll post my results. Thanks for your help!
JS Example Highlighted in Anki
Glad to hear that!
I think some cached versions of the files in my "collection.media" folder were causing the issue. Maybe when I tried the plugin a while back and scripts in the card templates kept relying on these older versions.
I cleared the "collection.media" folder and tried to recreate the problem but I can't. @pilgrimlyieu's tutorial works for me now and so do the files generated after installing the plugin.
I think some cached versions of the files in my "collection.media" folder were causing the issue. Maybe when I tried the plugin a while back and scripts in the card templates kept relying on these older versions.
I cleared the "collection.media" folder and tried to recreate the problem but I can't. @pilgrimlyieu's tutorial works for me now and so do the files generated after installing the plugin.
I've tried to delete highlight.js
and highlight.css
and download a version that includes no language highlight from highlight.js, but I still can not get the same console log. Anyway, this issue is solved, maybe.
How can I change the theme for my code blocks?
How can I change the theme for my code blocks?
Sorry for my late reply. You can download a package in highlight.js, unzip it and choose a style in styles
. For example I'm using atom-one-dark-reasonable.min.css
. Put the style file to collection.media
directory. Then change code in both side.(The following code is my settings, maybe different from yours, but is similar)
var getResources = [
getCSS("_katex.css", "https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css"),
getCSS("_highlight.css", "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/atom-one-dark-reasonable.min.css"),
getScript("_highlight.js", "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"),
getScript("_katex.min.js", "https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js"),
getScript("_auto-render.js", "https://cdn.jsdelivr.net/gh/Jwrede/Anki-KaTeX-Markdown/auto-render-cdn.js"),
getScript("_markdown-it.min.js", "https://cdn.jsdelivr.net/npm/markdown-it@13.0.1/dist/markdown-it.min.js"),
getScript("_markdown-it-mark.js", "https://github.com/markdown-it/markdown-it-mark/blob/master/dist/markdown-it-mark.js")
];
Change _highlight.css
to your style file name or rename your style file name to _highlight.css
.
Here is the output.
Thank you very much!
@pilgrimlyieu I think there is a bug in the tutorial. I was getting leftover </div>
elements in my cards.
Here is the problem code:
I've added str =
to the beginning and made the regex case insensitive with gi
like the other lines.
str = str.replace(/<\/div[^>]*>/gi, "\n");
@pilgrimlyieu I think there is a bug in the tutorial. I was getting leftover
</div>
elements in my cards.
You are right, thanks! Due to my limited knowledge of JavaScript, I didn't check the code. I will add it to the tutorial.
Notice that many people are confused about how to make custom card type with KaTeX-Markdown support, I consider it important to make a tutorial to answer their confusion.
Basic
Take "Basic (type in the answer)" as an example. (Every card type can use the same method)
Open KaTeX and Markdown Basic and copy the code wrapped by
<script>
and</script>
to your card type. (Pay attention to the side of the card!)Front
Back
Basic Front (Up to now)
```html ```Basic Back (Up to now)
```html ```Cloze Front (Up to now)
```html ```Cloze Back (Up to now)
```html ```The result is like this
Front
Back
Then wrap field with html label. The format is
<div id="XXX"><pre>{{Field}}</pre></div>
Front
Back
Test markdown
Test math: $E=mc^2$
$$\mathrm{e}=\sum^{+\infty}_{i=0}\frac{1}{i!}$$
Then add your id name. In my template is
Finally add HTML label
Notice
Remember to add
<pre></pre>
label, or it'll cause lots of styling problems.If you use
{{#Field}}{{/Field}}
to wrap{{Field}}
, Notice that if{{Field}}
is empty, it'll interrupt the render of the later field.Example:
If
{{b}}
is empty,{{a}}
will render normally while{{c}}
could not.If you delete
{{#b}}{{/b}}
it works normal again.Solution:
Add
try catch
wrap to therender
function (maybe alsoshow
function) likeQ&A
See Where to store persistent KaTex macros
$
and$$
, what should I do?See Dollar sign in code block issues or Extraneous non-working HTML tags in a code block
See Extraneous non-working HTML tags in a code block
\pu{...}
syntax work normally?Related to https://github.com/Jwrede/Anki-KaTeX-Markdown/issues/17#issuecomment-922436281
Problem: There is no space between number and unit.
Solution: Remove the starred code
and add it to this function like the starred line
</div>
elements in cards.Related to https://github.com/Jwrede/Anki-KaTeX-Markdown/issues/31#issuecomment-1264120987, Thank @keithadams!
Solution:
If you have more questions, be free to ask. I'll reply you as soon as possible.
My Anki scripts & stylings is hosted in this repo. Anyone is welcome to have a look!