|
| 1 | +import { DurationPresentationType, FormattingWrapper, ParameterType, TimestampPresentationType, UserPresentationType } from "./index.ts"; |
| 2 | + |
| 3 | +const FORMAT_PATTERN = /\{\{(?<wrapper>>|`|```)?(?<parameter>\w+)(?:#(?<presentation>\w+))?\}\}?/g; |
| 4 | + |
| 5 | +interface FormatGroups { |
| 6 | + wrapper?: ">" | "`" | "```"; |
| 7 | + parameter: string; |
| 8 | + presentation?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +export function parseTemplateTokens(template: string, params: Record<string, ParameterType>): Token[] | string { |
| 12 | + const result: Token[] = []; |
| 13 | + |
| 14 | + let match = FORMAT_PATTERN.exec(template); |
| 15 | + let literalStart = 0; |
| 16 | + |
| 17 | + while (match !== null) { |
| 18 | + if (literalStart !== match.index) { |
| 19 | + result.push({ |
| 20 | + type: TokenType.Literal, |
| 21 | + value: template.substring(literalStart, match.index), |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + const groups = match.groups as unknown as FormatGroups; |
| 26 | + const token = parseFormatToken(groups, params); |
| 27 | + |
| 28 | + if (typeof token === "string") |
| 29 | + return token; |
| 30 | + else |
| 31 | + result.push(token); |
| 32 | + |
| 33 | + literalStart = FORMAT_PATTERN.lastIndex; |
| 34 | + match = FORMAT_PATTERN.exec(template); |
| 35 | + } |
| 36 | + |
| 37 | + if (literalStart !== template.length) { |
| 38 | + result.push({ |
| 39 | + type: TokenType.Literal, |
| 40 | + value: template.substring(literalStart) |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + return result; |
| 45 | +} |
| 46 | + |
| 47 | +function parseFormatToken(input: FormatGroups, params: Record<string, ParameterType>): FormatToken | string { |
| 48 | + if (!Object.hasOwn(params, input.parameter)) |
| 49 | + return `No value named '${input.parameter}' exists`; |
| 50 | + |
| 51 | + const valueType = params[input.parameter]!; |
| 52 | + const wrapper = parseWrapper(input.wrapper); |
| 53 | + |
| 54 | + const result = { type: TokenType.Format, parameter: input.parameter, wrapper } as const; |
| 55 | + |
| 56 | + switch (valueType) { |
| 57 | + case ParameterType.User: { |
| 58 | + const presentation = parseUserPresentation(input.presentation); |
| 59 | + |
| 60 | + if (presentation === null) |
| 61 | + return `Invalid user presentation: '${input.presentation!}'`; |
| 62 | + |
| 63 | + return { ...result, valueType, presentation }; |
| 64 | + } |
| 65 | + case ParameterType.Duration: { |
| 66 | + const presentation = parseDurationPresentation(input.presentation); |
| 67 | + |
| 68 | + if (presentation === null) |
| 69 | + return `Invalid duration presentation: '${input.presentation!}'`; |
| 70 | + |
| 71 | + return { ...result, valueType, presentation }; |
| 72 | + } |
| 73 | + case ParameterType.Timestamp: |
| 74 | + const presentation = parseTimestampPresentation(input.presentation); |
| 75 | + |
| 76 | + if (presentation === null) |
| 77 | + return `Invalid timestamp presentation: '${input.presentation!}'`; |
| 78 | + |
| 79 | + return { ...result, valueType, presentation }; |
| 80 | + case ParameterType.RawString: |
| 81 | + case ParameterType.MarkdownString: |
| 82 | + return { ...result, valueType }; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function parseWrapper(input: FormatGroups["wrapper"]): FormattingWrapper | null { |
| 87 | + switch (input) { |
| 88 | + case ">": return FormattingWrapper.BlockQuote; |
| 89 | + case "`": return FormattingWrapper.InlineCodeblock; |
| 90 | + case "```": return FormattingWrapper.MultilineCodeblock; |
| 91 | + case undefined: return null; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function parseUserPresentation(input: string | undefined): UserPresentationType | null { |
| 96 | + switch (input) { |
| 97 | + case "tag_mention": case undefined: return UserPresentationType.TagMention; |
| 98 | + case "tag_mention_bold": return UserPresentationType.TagMentionBold; |
| 99 | + case "tag": return UserPresentationType.Tag; |
| 100 | + case "mention": return UserPresentationType.Mention; |
| 101 | + case "id": return UserPresentationType.ID; |
| 102 | + case "url": return UserPresentationType.URL; |
| 103 | + default: return null; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function parseDurationPresentation(input: string | undefined): DurationPresentationType | null { |
| 108 | + switch (input) { |
| 109 | + case "readable": case undefined: return DurationPresentationType.Readable; |
| 110 | + case "seconds": case undefined: return DurationPresentationType.Seconds; |
| 111 | + case "milliseconds": case undefined: return DurationPresentationType.Milliseconds; |
| 112 | + default: return null; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function parseTimestampPresentation(input: string | undefined): TimestampPresentationType | null { |
| 117 | + switch (input) { |
| 118 | + case "date_time": case "f": case undefined: return TimestampPresentationType.DateTime; |
| 119 | + case "date_time_long": case "F": case undefined: return TimestampPresentationType.DateTimeLong; |
| 120 | + case "time": case "t": case undefined: return TimestampPresentationType.Time; |
| 121 | + case "time_long": case "T": case undefined: return TimestampPresentationType.TimeLong; |
| 122 | + case "date": case "d": case undefined: return TimestampPresentationType.Date; |
| 123 | + case "date_long": case "D": case undefined: return TimestampPresentationType.DateLong; |
| 124 | + case "relative": case "r": case "R": case undefined: return TimestampPresentationType.Relative; |
| 125 | + case "unix": case undefined: return TimestampPresentationType.Unix; |
| 126 | + case "unix_seconds": case undefined: return TimestampPresentationType.UnixSeconds; |
| 127 | + default: return null; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export type Token = LiteralToken | FormatToken; |
| 132 | + |
| 133 | +export enum TokenType { |
| 134 | + Literal, |
| 135 | + Format, |
| 136 | +} |
| 137 | + |
| 138 | +export interface LiteralToken { |
| 139 | + type: TokenType.Literal; |
| 140 | + value: string; |
| 141 | +} |
| 142 | + |
| 143 | +export type FormatToken = |
| 144 | + { type: TokenType.Format; parameter: string; } |
| 145 | + & ( |
| 146 | + | { valueType: ParameterType.User; presentation: UserPresentationType; } |
| 147 | + | { valueType: ParameterType.Duration; presentation: DurationPresentationType; } |
| 148 | + | { valueType: ParameterType.Timestamp; presentation: TimestampPresentationType; } |
| 149 | + | { valueType: ParameterType.MarkdownString | ParameterType.RawString; } |
| 150 | + ) |
| 151 | + & { wrapper: FormattingWrapper | null; }; |
0 commit comments