Color mode

Flashcards

Turn a flashcards code block into a study island: a small deck of cards that shows a question, reveals the answer, and can be navigated and shuffled in the browser. The build emits an accessible static list first, so the deck stays readable with no JavaScript at all.

日本語

Overview

flashcards() recognises fenced blocks

md
```flashcards
What is the capital of France? :: Paris
 
What is 2 + 2? :: 4
```

and replaces them with an interactive deck.

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { flashcardsPlugin } from "@riebeckite/plugin-flashcards";
 
export default defineConfig({
  // ...
  plugins: [flashcardsPlugin()],
});

The plugin adds style.css and a client entry. The script tag is emitted by the site layout; the plugin only declares it.

Block syntax

A block holds one or more cards. Each card is a Question :: Answer pair. Cards are separated by a blank line or by a --- line, and an answer may span multiple lines:

md
```flashcards
Which language is this plugin written in? :: TypeScript
 
What does the client render? :: An interactive deck
---
Describe the fallback. :: A static ordered list.
It stays readable without JavaScript.
```

A block with no cards, a card without ::, or a card with an empty side is reported as a diagnostic (invalid-flashcards) and left as a code block.

Output

At build time the block becomes

html
<div class="rb-flashcards" data-flashcards data-flashcards-count="2">
  <script type="application/json" data-flashcards-payload>
    {"cards":[{"front":"...","back":"..."}]}
  </script>
  <ol class="rb-flashcards__list" data-flashcards-fallback>
    <li class="rb-flashcards__item">
      <span class="rb-flashcards__front">...</span>
      <span class="rb-flashcards__back">...</span>
    </li>
  </ol>
</div>

The payload is inert and escaped, so card text containing <, >, or & can never close the script element. The ordered list is the no-JavaScript fallback.

Client behaviour

initFlashcards() reads the payload of every [data-flashcards] element and adds an interactive deck above the fallback. It supports:

  • Reveal / hide the answer
  • Previous and next card with wrap-around
  • Shuffle
  • A live card counter
  • Keyboard control: Space reveals, ArrowLeft / ArrowRight navigate

On success the root gets data-flashcards="ready" and the static list is hidden. If the payload is missing or invalid the client leaves the fallback untouched. No options are passed from the build to the client; the deck reads data-flashcards-shuffle when it is present.

Options

Option Type Default Description
className string "rb-flashcards" Root CSS class for the deck
language string "flashcards" Fence language to target
shuffle boolean false Start the deck in a shuffled order
fallback boolean true Emit the static ordered list

Exports

  • flashcards(options?) / flashcardsPlugin(options?) — plugin factory
  • initFlashcards(root?) — client initializer
  • parseFlashcards(source) — parse a block into cards
  • splitFlashcardGroups(source) — split a block into card groups
  • remarkFlashcards(options?) — the remark transform on its own
  • renderFlashcards(cards, options) / renderFlashcardsPayload(cards) / renderFlashcardsFallback(cards, className?) — build-time rendering helpers
  • resolveFlashcardsOptions(options?), createFlashcardsRuntime(options?)
  • Types: FlashcardsOptions, FlashcardsCard, FlashcardsPayload, ResolvedFlashcardsOptions

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/flashcards/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Flashcards
4 +
5 + Turn a `flashcards` code block into a study island: a small deck of cards that
6 + shows a question, reveals the answer, and can be navigated and shuffled in the
7 + browser. The build emits an accessible static list first, so the deck stays
8 + readable with no JavaScript at all.
9 +
10 + [日本語](./flashcards.md)
11 +
12 + ## Overview
13 +
14 + `flashcards()` recognises fenced blocks
15 +
16 + ````md
17 + ```flashcards
18 + What is the capital of France? :: Paris
19 +
20 + What is 2 + 2? :: 4
21 + ```
22 + ````
23 +
24 + and replaces them with an interactive deck.
25 +
26 + ## Usage
27 +
28 + ```ts
29 + import { defineConfig } from "@riebeckite/core";
30 + import { flashcardsPlugin } from "@riebeckite/plugin-flashcards";
31 +
32 + export default defineConfig({
33 + // ...
34 + plugins: [flashcardsPlugin()],
35 + });
36 + ```
37 +
38 + The plugin adds `style.css` and a client entry. The script tag is emitted by the
39 + site layout; the plugin only declares it.
40 +
41 + ## Block syntax
42 +
43 + A block holds one or more cards. Each card is a `Question :: Answer` pair. Cards
44 + are separated by a blank line or by a `---` line, and an answer may span
45 + multiple lines:
46 +
47 + ````md
48 + ```flashcards
49 + Which language is this plugin written in? :: TypeScript
50 +
51 + What does the client render? :: An interactive deck
52 + ---
53 + Describe the fallback. :: A static ordered list.
54 + It stays readable without JavaScript.
55 + ```
56 + ````
57 +
58 + A block with no cards, a card without `::`, or a card with an empty side is
59 + reported as a diagnostic (`invalid-flashcards`) and left as a code block.
60 +
61 + ## Output
62 +
63 + At build time the block becomes
64 +
65 + ```html
66 + <div class="rb-flashcards" data-flashcards data-flashcards-count="2">
67 + <script type="application/json" data-flashcards-payload>
68 + {"cards":[{"front":"...","back":"..."}]}
69 + </script>
70 + <ol class="rb-flashcards__list" data-flashcards-fallback>
71 + <li class="rb-flashcards__item">
72 + <span class="rb-flashcards__front">...</span>
73 + <span class="rb-flashcards__back">...</span>
74 + </li>
75 + </ol>
76 + </div>
77 + ```
78 +
79 + The payload is inert and escaped, so card text containing `<`, `>`, or `&` can
80 + never close the script element. The ordered list is the no-JavaScript fallback.
81 +
82 + ## Client behaviour
83 +
84 + `initFlashcards()` reads the payload of every `[data-flashcards]` element and
85 + adds an interactive deck above the fallback. It supports:
86 +
87 + - Reveal / hide the answer
88 + - Previous and next card with wrap-around
89 + - Shuffle
90 + - A live card counter
91 + - Keyboard control: `Space` reveals, `ArrowLeft` / `ArrowRight` navigate
92 +
93 + On success the root gets `data-flashcards="ready"` and the static list is
94 + hidden. If the payload is missing or invalid the client leaves the fallback
95 + untouched. No options are passed from the build to the client; the deck reads
96 + `data-flashcards-shuffle` when it is present.
97 +
98 + ## Options
99 +
100 + | Option | Type | Default | Description |
101 + | ------ | ---- | ------- | ----------- |
102 + | `className` | `string` | `"rb-flashcards"` | Root CSS class for the deck |
103 + | `language` | `string` | `"flashcards"` | Fence language to target |
104 + | `shuffle` | `boolean` | `false` | Start the deck in a shuffled order |
105 + | `fallback` | `boolean` | `true` | Emit the static ordered list |
106 +
107 + ## Exports
108 +
109 + - `flashcards(options?)` / `flashcardsPlugin(options?)` — plugin factory
110 + - `initFlashcards(root?)` — client initializer
111 + - `parseFlashcards(source)` — parse a block into cards
112 + - `splitFlashcardGroups(source)` — split a block into card groups
113 + - `remarkFlashcards(options?)` — the remark transform on its own
114 + - `renderFlashcards(cards, options)` / `renderFlashcardsPayload(cards)` /
115 + `renderFlashcardsFallback(cards, className?)` — build-time rendering helpers
116 + - `resolveFlashcardsOptions(options?)`, `createFlashcardsRuntime(options?)`
117 + - Types: `FlashcardsOptions`, `FlashcardsCard`, `FlashcardsPayload`,
118 + `ResolvedFlashcardsOptions`
119 +
120 + ## See also
121 +
122 + - [Plugin guide](../reference/plugin-api.en.md)
123 +