moderator-toolbox-nxg-for-reddit / util/wiki/schemas/shared/tokens

util/wiki/schemas/shared/tokens

Functions

canonicalizeChoiceBlocks()

canonicalizeChoiceBlocks(text): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:355

Rewrites every {choice} block in the text to its canonical form: each block separated from surrounding content by exactly one blank line, options on single - lines. Idempotent, so normalizing the NXG text and the up-converted legacy mirror both land on the same string and config reconcile sees no spurious difference. Text with no choice block is returned unchanged.

Parameters

text

string

The reason text to canonicalize.

Returns

string


decodeHtmlAngleBrackets()

decodeHtmlAngleBrackets(text): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:63

Decodes HTML-entity-encoded angle brackets so &lt;select&gt; configs are treated the same as configs that store literal <select> tags. Handles any number of &amp; prefixes (e.g. &amp;amp;amp;lt;) so configs that were entity-encoded multiple times by old toolbox or Reddit’s API round-trips are recovered correctly. Safe for surrounding markdown: SnuOwnd re-encodes any </> it sees, so the display output is identical whether we pre-decode or not.

Parameters

text

string

Returns

string


findLiteralChoiceMarker()

findLiteralChoiceMarker(text): LiteralChoiceProblem | null

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:323

Finds the first {choice} marker in the text that no control will be rendered for - a half-typed field, or one written inline in a sentence - and reports which of the two it is. Returns null when every marker in the text becomes a control.

Such a marker is not substituted, so the marker text and the option list below it both reach the removed user as written. That is the right default for unknown brace content, but a pick-one field failing this way is a mistake every time, so callers surface it to the moderator instead of leaving it to be discovered in a sent message.

Parameters

text

string

The reason text to check, already normalized to token form.

Returns

LiteralChoiceProblem | null


htmlFieldsToTokens()

htmlFieldsToTokens(text): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:517

Up-converts legacy (schema v1) reason text to token form: <input> and <textarea> elements become inline tokens, <br> becomes a paragraph break, and each <select> becomes an inline {choice} block (spliced in between blank lines so the block marker lands on its own line; the caller’s canonicalizeChoiceBlocks pass tidies the spacing).

Token-form text passes through unchanged, so this is safe (and idempotent) to apply to any reason text regardless of origin. The input must already have entity-encoded angle brackets decoded (see decodeHtmlAngleBrackets).

Parameters

text

string

The reason text to up-convert.

Returns

string


htmlSimpleFieldsToTokens()

htmlSimpleFieldsToTokens(text): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:531

Up-converts only the inline legacy fields - <input>, <textarea>, and <br> - leaving any <select> HTML untouched. Used for the removal message header and footer, which display no interactive choice controls.

Parameters

text

string

The header/footer text to up-convert.

Returns

string


inlineSelectDefinitions()

inlineSelectDefinitions(text, selects): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:415

Migrates the older v2 shape - a {select:name} reference resolving to a separate definition - into the inline {choice} form. Each reference is replaced by a block built from its definition: the prompt (if any) as a markdown line above the marker, and the options as a list below it. A reference whose definition is missing or empty is left untouched (it renders literally). Idempotent: text with no {select:name} references is unchanged.

Parameters

text

string

The reason text containing {select:name} references.

selects

LegacySelectDefinition[]

The reason’s legacy select definitions.

Returns

string


parseReasonSegments()

parseReasonSegments(text): ReasonSegment[]

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:221

Splits reason text into literal-text and interactive-token segments, in document order. Inline {input}/{textarea} tokens are matched anywhere; a {choice} marker is recognized only on its own line and consumes the consecutive markdown list lines below it as its options, skipping any blank lines between the two and stopping at the first non-list line. A marker whose next non-blank line is not a list item is left as literal text, like any other unknown brace content.

Parameters

text

string

The reason text to parse.

Returns

ReasonSegment[]


pickSubstitutionTokens()

pickSubstitutionTokens(tokens): SubstitutionTokenInfo[]

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:106

Picks a context-specific subset of substitutionTokens by token text, preserving the master list’s order. Editor UIs use this to offer only the tokens their context actually substitutes.

Parameters

tokens

string[]

Token texts including braces, e.g. '{author}'.

Returns

SubstitutionTokenInfo[]


serializeToken()

serializeToken(token): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:203

Serializes an interactive token back to its text form. A choice serializes to its block (marker + option list); inputs/textareas to their inline token.

Parameters

token

InteractiveToken

The token to serialize.

Returns

string


substituteTokenValues()

substituteTokenValues(text, values): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:387

Replaces each interactive token in reason text with the corresponding user-entered value, in document order. Used when composing the final removal message; values map 1:1 to the rendered controls because the overlay renders them in the same order.

Parameters

text

string

The token-form reason text.

values

string[]

The entered values, in token order. Missing values become ‘’.

Returns

string


tokensToHtmlFields()

tokensToHtmlFields(text): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:596

Down-converts token-form reason text to the legacy HTML form for the classic (schema v1) wiki mirror: inline tokens become HTML form elements and each {choice} block becomes a <select> element. Literal text - including paragraph breaks, which 6.x handles fine as newlines - passes through unchanged.

Parameters

text

string

The token-form reason text.

Returns

string


tokenToLegacyHtml()

tokenToLegacyHtml(token): string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:568

Serializes an interactive token to the legacy HTML element 6.x understands. A choice’s id becomes the <select> id attribute; its options become <option>s with markdown link characters backslash-escaped to survive 6.x’s markdown pass. The choice’s prompt is plain text above the block, not part of the token, so no label attribute is written.

Parameters

token

InteractiveToken

Returns

string

Interfaces

InteractiveToken

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:114

A parsed interactive token from removal reason text.

Properties

id?

optional id?: string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:122

Optional stable id, used to persist the entered/chosen value between overlay opens and preserved as the HTML id attribute on the classic mirror. Written as {input#someid: ...} for inputs/textareas and {choice#someid} for choices.

kind

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:115

options

options: string[]

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:126

The option texts for choice tokens; empty for the other kinds.

placeholder

placeholder: string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:124

Placeholder text for input/textarea tokens; always ‘’ for choice.


LegacySelectDefinition

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:399

A legacy <select> definition the older v2 schema stored separately and referenced from text as {select:name}. Only used by inlineSelectDefinitions to migrate such configs to the inline form.

Properties

name

name: string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:400

options

options: string[]

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:402

prompt?

optional prompt?: string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:401


SubstitutionTokenInfo

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:70

A substitution token ({author}, {url}, …) with a human-readable description for editor UIs.

Properties

description

description: string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:74

What the token expands to, phrased for a tooltip in the settings editor.

token

token: string

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:72

The literal token text including braces, e.g. {author}.

Type Aliases

InteractiveTokenKind

InteractiveTokenKind = "input" | "textarea" | "choice"

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:111

The kinds of interactive fill-in fields supported in reason text.


LiteralChoiceProblem

LiteralChoiceProblem = "inline" | "no-options"

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:306

Why a {choice} marker will be sent as text instead of becoming a control. The two shapes need different fixes, so callers can word their warning for the one at hand.


ReasonSegment

ReasonSegment = { text: string; type: "text"; } | { token: InteractiveToken; type: "token"; }

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:130

A piece of reason text: either literal text or an interactive token.

Variables

substitutionTokens

const substitutionTokens: SubstitutionTokenInfo[]

Defined in: extension/data/util/wiki/schemas/shared/tokens.ts:83

The substitution tokens the removal overlay replaces with context data, in the order editor UIs should offer them (common tokens first). Descriptions match the values built in util/reddit/thingInfo.ts and consumed by the overlay’s token source.