Problem
My Anki template became a bit unwieldy. I basically have five components, that are all having their own html and css. Some have their own javascript as well. I was hoping to make it easier maintainable and looked into vue.js and svelte.
They are great to create components but the generated output is js. For Anki, I would need the output to be plain html with <script> tags at the bottom and a separate css file for the styling. Also, I couldn’t get some js related things working. Checkout the “sources” component below for an example.
Svelte Examples
I already tried svelte and vue. But due to their output, I do not think they are useful for my goal here. I’ll share a sample of my svelte code here, though, in case it makes things easier to understand:
App.svelte (contains all components)
<script>
import Question from "./components/Question.svelte";
import Divider from "./components/Divider.svelte";
import Answer from "./components/Answer.svelte";
import Remarks from "./components/Remarks.svelte";
import Sources from "./components/Sources.svelte";
</script>
<div id="all" lang="de">
<main id="flashcard">
<Question />
<Divider />
<Answer />
</main>
<Remarks />
<Sources />
</div>
<style>
#all {
--max-character-limit-per-line: 65ch; /* Text should not be longer than 65 characters to increase readability.*/
max-width: var(--max-character-limit-per-line);
margin: 0 auto;
}
#flashcard {
background-color: var(--global-background-color);
word-wrap: break-word;
min-width: 80%;
margin-top: var(--global-top-margin);
margin-bottom: 0;
margin-right: auto;
margin-left: auto;
padding: var(--global-padding);
box-shadow: var(--global-box-shadow);
border: var(--global-border);
border-radius: var(--global-border-radius);
}
</style>
Answer.svelte (contains the parts of code for my answer side)
<div class="hidden_on_front" , id="back_answer">
{`{{Back_Text}}`}
</div>
<div class="hidden_on_front" , id="back_code">
{`{{Back_Code_Block}}`}
</div>
<div class="hidden_on_front" , id="back_picture">
{`
{{#Back_Image}}
{{Back_Image}}
{{/Back_Image}}
`}
</div>
Sources.svelte (contains the sources of my card. Javascript is used to format the sources appropriatly)
<script>
import { onMount } from "svelte";
/**********************************************************************/
/** [JS] BREAK SOURCES INTO SEPARATE DIVS **/
/**********************************************************************/
var source_string = `{{Back_Sources}}`;
var individual_source_string = [];
onMount(() => {
var source_container = document.getElementById("back_sources");
if (source_container) {
individual_source_string = source_string.split("<br>");
individual_source_string.forEach((source, index) => {
const div = document.createElement("div");
div.innerHTML = "• " + source;
div.classList.add("back_sources_elements");
source_container.appendChild(div);
});
}
});
</script>
<footer>
<section class="hidden_on_front" , id="back_sources">
<h1>Quellen:</h1>
<!-- generated by JS -->
</section>
</footer>
I also tried the more ideomatic {#each ...} • Svelte Docs, but the result is always the same:
- The code works fine in my browser (chrome and librewolf) via local server
- In Anki, the js won’t run / update the card
- I have no html with
<script>tags that I could include in ankis template window
Question
How can I use a component based approach for my card templates? It’s totally fine if I have to compile it first (like with e.g. svelte) via my IDE (I use vs codium).
I’m essentially looking for something that makes maintainace much easier, while still being performant on desktop and mobile anki alike.
Thank you for your time and ideas!

