AnkiIO 1.0.2
Build, validate, import, and export Anki-compatible decks from .NET
Loading...
Searching...
No Matches
AnkiIO.AnkiNoteType Class Referencesealed

Defines the reusable schema and rendering rules shared by a family of Anki notes. More...

Public Member Functions

 AnkiNoteType (string name, AnkiNoteTypeKind kind=AnkiNoteTypeKind.Standard, long? id=null)
 Initializes an unfrozen note type with no fields or templates and default card CSS.
AnkiNoteType AddField (string name)
 Adds a uniquely named field at the end of the field order.
AnkiNoteType AddConfiguredField (AnkiField field)
 Adds a configured field at the end of the field order.
AnkiNoteType AddTemplate (string name, string questionFormat, string answerFormat)
 Adds a card template at the next ordinal.
AnkiNoteType AddConfiguredTemplate (AnkiCardTemplate template)
 Adds a configured card template at the next card ordinal.

Properties

long Id [get]
 Gets the persisted identity by which notes and package metadata refer to this definition.
string Name [get]
 Gets the user-visible note-type name.
AnkiNoteTypeKind Kind [get]
 Gets the immutable strategy used to turn one note into cards.
string Css [get, set]
 Gets or sets CSS shared by every card rendered from this note type.
bool IsFrozen [get]
 Gets whether fields, templates, and CSS can no longer be changed.
IReadOnlyList< AnkiFieldFields [get]
 Gets fields in positional storage and Anki-editor order.
IReadOnlyList< AnkiCardTemplateTemplates [get]
 Gets templates in zero-based card-ordinal order.

Detailed Description

Defines the reusable schema and rendering rules shared by a family of Anki notes.

An Anki note type (called a “model” by some file formats) is comparable to a small schema: its ordered fields define what each note stores, its templates define the cards Standard notes generate, and its CSS styles every generated card. Reuse one instance for all notes that share that schema. Creating a new instance for every note gives each instance a different ID and can produce unnecessary duplicate note types after import.

Build the definition first, then create notes. Constructing the first AnkiNote freezes the instance permanently so later changes cannot shift positional field storage or card ordinals beneath existing notes. The Fields and Templates collections are ordered, live read-only views; their elements are immutable records. AnkiIO object graphs are not safe for concurrent mutation.

Field and template names are unique without regard to case, but note-value lookup uses the stored spelling exactly. AnkiIO validates structure and known field references before output; it does not render templates, sanitize HTML/CSS, run JavaScript, or promise that custom markup works in every Anki version.

var vocabulary = new AnkiNoteType("Vocabulary")
.AddConfiguredField(new AnkiField("German", FontSize: 24))
.AddField("Meaning")
.AddConfiguredTemplate(new AnkiCardTemplate(
"Recognition",
"{{German}}",
"{{FrontSide}}<hr id=\"answer\">{{Meaning}}"));
vocabulary.Css += ".hint { color: #777; }";
var deck = new AnkiDeck("German");
deck.AddNote(vocabulary, new Dictionary<string, string>
{
["German"] = "die Straße",
["Meaning"] = "street",
});
// vocabulary.IsFrozen is now true; reuse it for more Vocabulary notes.
Defines one study direction by mapping note fields to a card front and back.
Builds one named deck hierarchy and acts as the root for validation and export.
Definition AnkiDeck.cs:31
Configures one named input field in an AnkiNoteType.
Definition AnkiField.cs:45
AnkiNoteType(string name, AnkiNoteTypeKind kind=AnkiNoteTypeKind.Standard, long? id=null)
Initializes an unfrozen note type with no fields or templates and default card CSS.

Definition at line 45 of file AnkiNoteType.cs.

Constructor & Destructor Documentation

◆ AnkiNoteType()

AnkiIO.AnkiNoteType.AnkiNoteType ( string name,
AnkiNoteTypeKind kind = AnkiNoteTypeKind::Standard,
long? id = null )
inline

Initializes an unfrozen note type with no fields or templates and default card CSS.

Parameters
nameThe non-blank name shown in Anki's note-type manager.
kindAnkiNoteTypeKind.Standard for template-per-card generation, or AnkiNoteTypeKind.Cloze for deletion-index generation.
idAn optional stable numeric identifier for repeatable imports; when omitted, AnkiId.New generates one.

Empty definitions are allowed while assembling a model, but validation rejects a used note type with no fields or templates. For a ready-made conventional definition, use AnkiNoteTypes.

Exceptions
ArgumentExceptionname is empty or consists only of whitespace.
ArgumentNullExceptionname is null.

Definition at line 71 of file AnkiNoteType.cs.

Member Function Documentation

◆ AddConfiguredField()

AnkiNoteType AnkiIO.AnkiNoteType.AddConfiguredField ( AnkiField field)
inline

Adds a configured field at the end of the field order.

Parameters
fieldThe field definition to validate and add.
Returns
This instance for fluent construction.

The immutable record is retained as supplied, so its editor metadata remains available to serializers and package writers that support those attributes. Adding it after any note has been constructed is forbidden because that would change every note's positional field schema.

Exceptions
ArgumentNullExceptionfield or one of its required string values is null.
ArgumentExceptionThe field name or font is blank, or the name duplicates an existing field without regard to case.
ArgumentOutOfRangeExceptionAnkiField.FontSize is less than one.
InvalidOperationExceptionThe note type is frozen because a note already uses it.

Definition at line 182 of file AnkiNoteType.cs.

◆ AddConfiguredTemplate()

AnkiNoteType AnkiIO.AnkiNoteType.AddConfiguredTemplate ( AnkiCardTemplate template)
inline

Adds a configured card template at the next card ordinal.

Parameters
templateThe template definition to validate and add.
Returns
This instance for fluent construction.

AnkiIO preserves all supplied markup, including optional browser-specific formats. Known-field checking is performed on the main front/back formats by AnkiValidator.Validate(AnkiDeck) after the note type is used, but that validation is not a full template renderer or HTML security check. See AnkiCardTemplate for supported conventions and limitations.

Exceptions
ArgumentNullExceptiontemplate , AnkiCardTemplate.Name, AnkiCardTemplate.QuestionFormat, or AnkiCardTemplate.AnswerFormat is null.
ArgumentExceptionThe template name is blank or duplicates an existing template name without regard to case.
InvalidOperationExceptionThe note type is frozen because a note already uses it.

Definition at line 242 of file AnkiNoteType.cs.

◆ AddField()

AnkiNoteType AnkiIO.AnkiNoteType.AddField ( string name)
inline

Adds a uniquely named field at the end of the field order.

Parameters
nameThe non-empty field name used by notes and template references.
Returns
This instance for fluent construction.

This overload creates a field with left-to-right, non-sticky, Arial 20-pixel editor defaults. Those settings affect the editor, not study-card CSS. Use AddConfiguredField for other metadata.

Exceptions
ArgumentExceptionname is blank or duplicates a field name without regard to case.
ArgumentNullExceptionname is null.
InvalidOperationExceptionThe note type is frozen because a note already uses it.

Definition at line 155 of file AnkiNoteType.cs.

◆ AddTemplate()

AnkiNoteType AnkiIO.AnkiNoteType.AddTemplate ( string name,
string questionFormat,
string answerFormat )
inline

Adds a card template at the next ordinal.

Parameters
nameThe non-empty template name, unique without regard to case.
questionFormatNon-null Anki HTML and template markup for the front.
answerFormatNon-null Anki HTML and template markup for the back.
Returns
This instance for fluent construction.

This overload leaves browser-specific formats unset. Markup is stored verbatim and field references are validated later; AnkiIO does not render the template or suppress cards whose conditional front would be empty.

Exceptions
ArgumentNullExceptionAny argument is null.
ArgumentExceptionname is blank or duplicates a template name without regard to case.
InvalidOperationExceptionThe note type is frozen because a note already uses it.

Definition at line 210 of file AnkiNoteType.cs.

Property Documentation

◆ Css

string AnkiIO.AnkiNoteType.Css
getset

Gets or sets CSS shared by every card rendered from this note type.

Anki-compatible CSS. The default renders centered black Arial text on a white background.

The value is stored verbatim and emitted alongside the templates. It controls study-card rendering; editor-only font settings live on AnkiField. AnkiIO does not parse, sanitize, prefix, or normalize CSS, so callers accepting untrusted styles must apply their own content policy. Assignment is rejected after first use.

Exceptions
ArgumentNullExceptionThe assigned value is null.
InvalidOperationExceptionThe note type is frozen because a note already uses it.

Definition at line 107 of file AnkiNoteType.cs.

◆ Fields

IReadOnlyList<AnkiField> AnkiIO.AnkiNoteType.Fields
get

Gets fields in positional storage and Anki-editor order.

A live read-only view that reflects fields added before the note type freezes.

Use this order when importing positional values. Field-value dictionaries are keyed by exact name, but legacy Anki databases serialize their values according to this list.

Definition at line 135 of file AnkiNoteType.cs.

◆ Id

long AnkiIO.AnkiNoteType.Id
get

Gets the persisted identity by which notes and package metadata refer to this definition.

The caller-supplied identifier, or a process-generated positive identifier.

Preserve an imported ID when updating the same definition. Distinct definitions must not share an ID in one output, even if their names match; validation reports conflicting definitions. Explicit ID collision avoidance is the caller's responsibility.

Definition at line 88 of file AnkiNoteType.cs.

◆ IsFrozen

bool AnkiIO.AnkiNoteType.IsFrozen
get

Gets whether fields, templates, and CSS can no longer be changed.

true after this instance has been supplied to an AnkiNote constructor; otherwise, false.

Freezing is automatic and permanent. It protects every note that retains this shared definition from later changes to its field order, card ordinals, or rendering CSS.

Definition at line 127 of file AnkiNoteType.cs.

◆ Kind

AnkiNoteTypeKind AnkiIO.AnkiNoteType.Kind
get

Gets the immutable strategy used to turn one note into cards.

The immutable AnkiNoteTypeKind selected at construction.

Definition at line 96 of file AnkiNoteType.cs.

◆ Name

string AnkiIO.AnkiNoteType.Name
get

Gets the user-visible note-type name.

The name supplied when this instance was constructed.

Definition at line 92 of file AnkiNoteType.cs.

◆ Templates

IReadOnlyList<AnkiCardTemplate> AnkiIO.AnkiNoteType.Templates
get

Gets templates in zero-based card-ordinal order.

A live read-only view that reflects templates added before the note type freezes.

Standard notes generate one card for each entry. Cloze notes use distinct positive deletion indexes instead; their conventional definition still needs a template describing how {{cloze:Text}} renders.

Definition at line 143 of file AnkiNoteType.cs.


The documentation for this class was generated from the following file: