@nkardaz/typography-rules 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +911 -0
  3. package/dist/api/blacklist.d.ts +72 -0
  4. package/dist/api/htmlNodes.d.ts +30 -0
  5. package/dist/api/index.d.ts +6 -0
  6. package/dist/api/newRule.d.ts +51 -0
  7. package/dist/api/registerRule.d.ts +27 -0
  8. package/dist/api/rulesInit.d.ts +49 -0
  9. package/dist/functions/chemNotation.d.ts +10 -0
  10. package/dist/functions/clearSpaces.d.ts +16 -0
  11. package/dist/functions/index.cjs +514 -0
  12. package/dist/functions/index.d.ts +8 -0
  13. package/dist/functions/index.mjs +491 -0
  14. package/dist/functions/rubyText.d.ts +11 -0
  15. package/dist/functions/runt.d.ts +3 -0
  16. package/dist/functions/smartNumberGrouping.d.ts +25 -0
  17. package/dist/functions/smartQuotes.d.ts +29 -0
  18. package/dist/functions/wrapWithTag.d.ts +42 -0
  19. package/dist/glyphs/index.cjs +737 -0
  20. package/dist/glyphs/index.d.ts +53 -0
  21. package/dist/glyphs/index.mjs +714 -0
  22. package/dist/glyphs/proto.d.ts +11 -0
  23. package/dist/glyphs/registry.d.ts +728 -0
  24. package/dist/glyphs/types.d.ts +151 -0
  25. package/dist/helpers/index.cjs +268 -0
  26. package/dist/helpers/index.d.ts +133 -0
  27. package/dist/helpers/index.mjs +245 -0
  28. package/dist/helpers/types.d.ts +71 -0
  29. package/dist/index.cjs +985 -0
  30. package/dist/index.d.ts +5 -0
  31. package/dist/index.mjs +977 -0
  32. package/dist/style/index.d.ts +2 -0
  33. package/dist/style/main.css +16 -0
  34. package/dist/types.d.ts +223 -0
  35. package/dist/typography/aliases.d.ts +129 -0
  36. package/dist/typography/expressions/common.d.ts +29 -0
  37. package/dist/typography/expressions/en.d.ts +25 -0
  38. package/dist/typography/expressions/ru.d.ts +29 -0
  39. package/dist/typography/markup/common.d.ts +17 -0
  40. package/dist/typography/markup/en.d.ts +3 -0
  41. package/dist/typography/markup/index.d.ts +4 -0
  42. package/dist/typography/markup/ru.d.ts +3 -0
  43. package/dist/typography/sets/ang.d.ts +3 -0
  44. package/dist/typography/sets/common.d.ts +17 -0
  45. package/dist/typography/sets/en.d.ts +14 -0
  46. package/dist/typography/sets/index.d.ts +5 -0
  47. package/dist/typography/sets/ru.d.ts +16 -0
  48. package/dist/typography/store.d.ts +63 -0
  49. package/package.json +92 -0
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Internal blacklist implemented as a Trie.
3
+ *
4
+ * Supports hierarchical rule disabling:
5
+ * - Exact match: "/english/math"
6
+ * - Prefix match: "/english/math" disables "/english/math/*"
7
+ *
8
+ * Special rule:
9
+ * - "*" disables all rules globally
10
+ *
11
+ * This structure allows efficient prefix-based rule matching
12
+ * without scanning a flat Set.
13
+ */
14
+ export declare function disableRule(rule: string): void;
15
+ /**
16
+ * Re-enables a previously disabled rule.
17
+ *
18
+ * If the rule was globally disabled via "*",
19
+ * global flag will be cleared.
20
+ *
21
+ * Note: removing a prefix rule does not automatically
22
+ * restore sub-rules if they were individually disabled.
23
+ *
24
+ * @param rule Rule path to enable
25
+ */
26
+ export declare function enableRule(rule: string): void;
27
+ /**
28
+ * Toggles a rule between enabled and disabled state.
29
+ *
30
+ * If rule is currently disabled → it will be enabled.
31
+ * If rule is enabled → it will be disabled.
32
+ *
33
+ * @param rule Rule path to toggle
34
+ */
35
+ export declare function toggleRule(rule: string): void;
36
+ /**
37
+ * Checks whether a rule is disabled.
38
+ *
39
+ * A rule is considered disabled if:
40
+ * - global disable flag "*" is active
41
+ * - any parent rule in the path is disabled
42
+ * - the rule itself is explicitly disabled
43
+ *
44
+ * Example:
45
+ * "/english/math" disables:
46
+ * - "/english/math"
47
+ * - "/english/math/geometry"
48
+ *
49
+ * @param rule Rule path to check
50
+ * @returns true if rule is disabled
51
+ */
52
+ export declare function isRuleDisabled(rule: string): boolean;
53
+ /**
54
+ * Checks whether all rules are globally disabled.
55
+ *
56
+ * Global disable is represented by "*" rule.
57
+ *
58
+ * @returns true if global disable is active
59
+ */
60
+ export declare function isGloballyDisabled(): boolean;
61
+ /**
62
+ * Clears the entire rule blacklist.
63
+ *
64
+ * This removes:
65
+ * - all explicitly disabled rules in the Trie
66
+ * - global disable flag ("*")
67
+ *
68
+ * After calling this function, all rules become enabled again
69
+ * unless re-disabled explicitly.
70
+ */
71
+ export declare function clearBlacklist(): void;
72
+ //# sourceMappingURL=blacklist.d.ts.map
@@ -0,0 +1,30 @@
1
+ import type { HtmlNodeSettings, Node } from '@/types';
2
+ import type { Text } from 'mdast';
3
+ import type { MdxJsxTextElement } from 'mdast-util-mdx-jsx';
4
+ /**
5
+ * Parses a string into a tree of nodes based on a regular expression.
6
+ * * @param text The input string to parse
7
+ * @param settings Configuration containing the expression and a node factory function
8
+ * @returns An array of text and element nodes
9
+ */
10
+ export declare function htmlNode(text: string, { expression, nodes }?: HtmlNodeSettings): Node[];
11
+ /**
12
+ * Converts a node structure into an HTML string representation.
13
+ * * @param node The node to render
14
+ * @returns The resulting HTML string
15
+ */
16
+ export declare function renderNode(node: Node): string;
17
+ /**
18
+ * Renders an array of nodes into a single concatenated HTML string.
19
+ * * @param nodes Array of nodes to render
20
+ * @returns The resulting concatenated HTML string
21
+ */
22
+ export declare function renderNodes(nodes: Node[]): string;
23
+ /**
24
+ * Transforms a custom node structure into an MDAST (Markdown Abstract Syntax Tree) element,
25
+ * specifically targeting MDX JSX syntax.
26
+ * * @param n The node to transform
27
+ * @returns An MDAST Text or MdxJsxTextElement node
28
+ */
29
+ export declare function nodeToMdast(n: Node): Text | MdxJsxTextElement;
30
+ //# sourceMappingURL=htmlNodes.d.ts.map
@@ -0,0 +1,6 @@
1
+ export * from './newRule';
2
+ export * from './registerRule';
3
+ export * from './blacklist';
4
+ export * from './rulesInit';
5
+ export * from './htmlNodes';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,51 @@
1
+ import type { Rule, RuleFunction } from '@/types';
2
+ /**
3
+ * Creates a **replace** rule — every match is substituted with a literal string.
4
+ *
5
+ * The resulting rule is used in typography pipelines to transform text.
6
+ *
7
+ * @param label - Human-readable identifier for the rule
8
+ * @param rule - RegExp pattern to match against
9
+ * @param replacement - Literal string used as replacement for every match
10
+ * @param weight - Priority of the rule (higher = applied later)
11
+ *
12
+ * @returns `RegExpReplaceRule`
13
+ *
14
+ * @example
15
+ * newRule('em-dash', /--/g, '-');
16
+ */
17
+ declare function newRule(label: string, rule: RegExp, replacement: string, weight?: number): Rule;
18
+ /**
19
+ * Creates a **transform** rule — replacement is computed dynamically from the match.
20
+ *
21
+ * The resulting rule is used in typography pipelines to transform text.
22
+ *
23
+ * @param label - Human-readable identifier for the rule
24
+ * @param rule - RegExp pattern to match against
25
+ * @param transform - Callback receiving the full `RegExpExecArray`; return value replaces the matched substring
26
+ * @param weight - Priority of the rule (higher = applied later)
27
+ *
28
+ * @returns `RegExpTransformRule`
29
+ *
30
+ * @example
31
+ * newRule('wrap-digits', /\d+/g, m => `[${m[0]}]`);
32
+ */
33
+ declare function newRule(label: string, rule: RegExp, transform: (match: RegExpExecArray) => string, weight?: number): Rule;
34
+ /**
35
+ * Creates a **function** rule — custom logic with forwarded arguments.
36
+ *
37
+ * The resulting rule is used in typography pipelines to transform text.
38
+ *
39
+ * @param label - Human-readable identifier for the rule
40
+ * @param rule - Custom rule function — receives `args` at call time
41
+ * @param args - Arguments forwarded to the rule function when applied
42
+ * @param weight - Priority of the rule (higher = applied later)
43
+ *
44
+ * @returns `FunctionRule`
45
+ *
46
+ * @example
47
+ * newRule('custom', myRuleFn, ['arg1', 'arg2']);
48
+ */
49
+ declare function newRule(label: string, rule: RuleFunction, args?: unknown[], weight?: number): Rule;
50
+ export { newRule };
51
+ //# sourceMappingURL=newRule.d.ts.map
@@ -0,0 +1,27 @@
1
+ import type { Rule } from '@/types';
2
+ interface LabelTransform {
3
+ expression: RegExp;
4
+ replacement: string;
5
+ }
6
+ /**
7
+ * Registers one or more typography rules for a locale.
8
+ *
9
+ * Automatically invalidates the weighted rules cache
10
+ * for the affected locale and “common”.
11
+ *
12
+ * @param locale Locale code (e.g. "en", "de", "fr").
13
+ * @param rules A sequence of rules[].
14
+ */
15
+ declare function registerRule(locale: string, ...rules: Rule[]): void;
16
+ /**
17
+ * Registers rules for a locale, inheriting from a base locale.
18
+ *
19
+ * @param locale Locale code to register rules for (e.g. "fr", "de").
20
+ * @param base Locale code to inherit rules from (e.g. "en").
21
+ * @param options.label Optional transform applied to each inherited rule's label.
22
+ * @param options.excludes Rule labels to exclude from the base.
23
+ * @param rules Additional rules to register on top of the inherited ones.
24
+ */
25
+ declare function rulesBase(locale: string, base: string, label?: LabelTransform, excludes?: string[], ...rules: Rule[]): void;
26
+ export { registerRule, rulesBase };
27
+ //# sourceMappingURL=registerRule.d.ts.map
@@ -0,0 +1,49 @@
1
+ import type { Rule } from '@/types';
2
+ /**
3
+ * Default typography transformation rules grouped by locale.
4
+ *
5
+ * Each group defines a pipeline of RegExp and functional rules
6
+ * applied during text normalization and typographic processing.
7
+ *
8
+ * Structure:
9
+ * - common: rules applied to all locales
10
+ * - ru: Russian-specific typography rules
11
+ * - en: English-specific typography rules
12
+ *
13
+ * Rules include:
14
+ * - whitespace normalization
15
+ * - dash and punctuation correction
16
+ * - smart quotes processing
17
+ * - currency spacing rules
18
+ * - ligature substitution
19
+ */
20
+ declare const defaultTypographyRules: Record<string, Rule[]>;
21
+ declare const defaultMarkupRules: Record<string, Rule[]>;
22
+ /**
23
+ * Available typography rule groups.
24
+ *
25
+ * Represents all supported locale keys in the default ruleset.
26
+ */
27
+ export type defaultRuleKeys = keyof typeof defaultTypographyRules;
28
+ /**
29
+ * Runtime list of available typography rule groups.
30
+ */
31
+ export declare const defaultRuleKeys: (keyof typeof defaultTypographyRules)[];
32
+ /**
33
+ * Applies default typography rules to the global typography registry.
34
+ *
35
+ * If no locale is specified:
36
+ * - all rule groups are applied
37
+ *
38
+ * If locale is specified:
39
+ * - only that group is applied
40
+ *
41
+ * This function initializes the typography pipeline
42
+ * before processing text transformations.
43
+ *
44
+ * @param from Optional locale keys to apply specific rule group
45
+ */
46
+ export declare function initTypographyRules(...from: (keyof typeof defaultTypographyRules)[]): void;
47
+ export declare function initMarkupRules(...from: (keyof typeof defaultMarkupRules)[]): void;
48
+ export {};
49
+ //# sourceMappingURL=rulesInit.d.ts.map
@@ -0,0 +1,10 @@
1
+ import type { Node, TagSettings, ChemNotationSettings } from '@/types';
2
+ /**
3
+ * Parses chemical notation and converts it into MathML-like <mmultiscripts> structures.
4
+ * * @param text - The input string containing chemical notation syntax
5
+ * @param settings - Configuration for the marker and wrapper delimiters
6
+ * @param tagSettings - Optional class name and attributes for the <math> wrapper
7
+ * @returns An array of nodes including text and <math> nodes
8
+ */
9
+ export declare function chemNotation(text: string, { marker, wrapper }?: ChemNotationSettings, { className, attrs }?: TagSettings): Node[];
10
+ //# sourceMappingURL=chemNotation.d.ts.map
@@ -0,0 +1,16 @@
1
+ import type { ClearSpacesSettings } from '@/types';
2
+ /**
3
+ * Replaces multiple spaces with a single space.
4
+ *
5
+ * @param text - Input text.
6
+ *
7
+ * @param spaces - Array of space characters to remove.
8
+ *
9
+ * @returns Text with multiple spaces replaced by a single space.
10
+ *
11
+ * @example
12
+ * clearSpaces('a b c') // 'a b c'
13
+ * clearSpaces('a\u2009\u2009b\u2009\u2009c') // 'a\u2009b\u2009c' (thin space)
14
+ */
15
+ export declare function clearSpaces(text: string, { spaces }?: ClearSpacesSettings): string;
16
+ //# sourceMappingURL=clearSpaces.d.ts.map