@lionrapid/core 0.1.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 (302) hide show
  1. package/README.md +106 -0
  2. package/dist/base/event-emitter.d.ts +91 -0
  3. package/dist/base/event-emitter.d.ts.map +1 -0
  4. package/dist/base/event-emitter.js +330 -0
  5. package/dist/base/event-emitter.js.map +1 -0
  6. package/dist/base/index.d.ts +5 -0
  7. package/dist/base/index.d.ts.map +1 -0
  8. package/dist/base/index.js +5 -0
  9. package/dist/base/index.js.map +1 -0
  10. package/dist/browser/auto-init.d.ts +57 -0
  11. package/dist/browser/auto-init.d.ts.map +1 -0
  12. package/dist/browser/auto-init.js +304 -0
  13. package/dist/browser/auto-init.js.map +1 -0
  14. package/dist/browser/auto-translator.d.ts +98 -0
  15. package/dist/browser/auto-translator.d.ts.map +1 -0
  16. package/dist/browser/auto-translator.js +218 -0
  17. package/dist/browser/auto-translator.js.map +1 -0
  18. package/dist/browser/block-extractor.d.ts +26 -0
  19. package/dist/browser/block-extractor.d.ts.map +1 -0
  20. package/dist/browser/block-extractor.js +143 -0
  21. package/dist/browser/block-extractor.js.map +1 -0
  22. package/dist/browser/dom-observer.d.ts +42 -0
  23. package/dist/browser/dom-observer.d.ts.map +1 -0
  24. package/dist/browser/dom-observer.js +109 -0
  25. package/dist/browser/dom-observer.js.map +1 -0
  26. package/dist/browser/editor/editor-api.d.ts +67 -0
  27. package/dist/browser/editor/editor-api.d.ts.map +1 -0
  28. package/dist/browser/editor/editor-api.js +233 -0
  29. package/dist/browser/editor/editor-api.js.map +1 -0
  30. package/dist/browser/editor/editor-init.d.ts +11 -0
  31. package/dist/browser/editor/editor-init.d.ts.map +1 -0
  32. package/dist/browser/editor/editor-init.js +181 -0
  33. package/dist/browser/editor/editor-init.js.map +1 -0
  34. package/dist/browser/editor/editor-styles.d.ts +6 -0
  35. package/dist/browser/editor/editor-styles.d.ts.map +1 -0
  36. package/dist/browser/editor/editor-styles.js +419 -0
  37. package/dist/browser/editor/editor-styles.js.map +1 -0
  38. package/dist/browser/editor/inspector.d.ts +22 -0
  39. package/dist/browser/editor/inspector.d.ts.map +1 -0
  40. package/dist/browser/editor/inspector.js +125 -0
  41. package/dist/browser/editor/inspector.js.map +1 -0
  42. package/dist/browser/editor/key-resolver.d.ts +19 -0
  43. package/dist/browser/editor/key-resolver.d.ts.map +1 -0
  44. package/dist/browser/editor/key-resolver.js +49 -0
  45. package/dist/browser/editor/key-resolver.js.map +1 -0
  46. package/dist/browser/editor/live-preview.d.ts +10 -0
  47. package/dist/browser/editor/live-preview.d.ts.map +1 -0
  48. package/dist/browser/editor/live-preview.js +57 -0
  49. package/dist/browser/editor/live-preview.js.map +1 -0
  50. package/dist/browser/editor/sidebar.d.ts +50 -0
  51. package/dist/browser/editor/sidebar.d.ts.map +1 -0
  52. package/dist/browser/editor/sidebar.js +353 -0
  53. package/dist/browser/editor/sidebar.js.map +1 -0
  54. package/dist/browser/editor/toolbar.d.ts +29 -0
  55. package/dist/browser/editor/toolbar.d.ts.map +1 -0
  56. package/dist/browser/editor/toolbar.js +131 -0
  57. package/dist/browser/editor/toolbar.js.map +1 -0
  58. package/dist/browser/exclude-filter.d.ts +21 -0
  59. package/dist/browser/exclude-filter.d.ts.map +1 -0
  60. package/dist/browser/exclude-filter.js +51 -0
  61. package/dist/browser/exclude-filter.js.map +1 -0
  62. package/dist/browser/index.d.ts +32 -0
  63. package/dist/browser/index.d.ts.map +1 -0
  64. package/dist/browser/index.js +28 -0
  65. package/dist/browser/index.js.map +1 -0
  66. package/dist/browser/language-switcher.d.ts +70 -0
  67. package/dist/browser/language-switcher.d.ts.map +1 -0
  68. package/dist/browser/language-switcher.js +339 -0
  69. package/dist/browser/language-switcher.js.map +1 -0
  70. package/dist/browser/lionrapid.auto.dev.js +5757 -0
  71. package/dist/browser/lionrapid.auto.dev.js.map +7 -0
  72. package/dist/browser/lionrapid.auto.js +5757 -0
  73. package/dist/browser/lionrapid.auto.js.map +7 -0
  74. package/dist/browser/lionrapid.auto.min.js +80 -0
  75. package/dist/browser/lionrapid.auto.min.js.map +7 -0
  76. package/dist/browser/lionrapid.editor.js +1869 -0
  77. package/dist/browser/lionrapid.editor.js.map +7 -0
  78. package/dist/browser/lionrapid.editor.min.js +415 -0
  79. package/dist/browser/lionrapid.editor.min.js.map +7 -0
  80. package/dist/browser/lionrapid.esm.js +4786 -0
  81. package/dist/browser/lionrapid.esm.js.map +7 -0
  82. package/dist/browser/lionrapid.esm.min.js +3 -0
  83. package/dist/browser/lionrapid.esm.min.js.map +7 -0
  84. package/dist/browser/lionrapid.iife.dev.js +4809 -0
  85. package/dist/browser/lionrapid.iife.dev.js.map +7 -0
  86. package/dist/browser/lionrapid.iife.js +4809 -0
  87. package/dist/browser/lionrapid.iife.js.map +7 -0
  88. package/dist/browser/lionrapid.iife.min.js +3 -0
  89. package/dist/browser/lionrapid.iife.min.js.map +7 -0
  90. package/dist/browser/lionrapid.umd.js +4822 -0
  91. package/dist/browser/lionrapid.umd.js.map +7 -0
  92. package/dist/browser/lionrapid.umd.min.js +18 -0
  93. package/dist/browser/lionrapid.umd.min.js.map +7 -0
  94. package/dist/browser/markup-parser.d.ts +13 -0
  95. package/dist/browser/markup-parser.d.ts.map +1 -0
  96. package/dist/browser/markup-parser.js +56 -0
  97. package/dist/browser/markup-parser.js.map +1 -0
  98. package/dist/browser/md5.d.ts +16 -0
  99. package/dist/browser/md5.d.ts.map +1 -0
  100. package/dist/browser/md5.js +145 -0
  101. package/dist/browser/md5.js.map +1 -0
  102. package/dist/browser/storage/cache-storage-repository.d.ts +44 -0
  103. package/dist/browser/storage/cache-storage-repository.d.ts.map +1 -0
  104. package/dist/browser/storage/cache-storage-repository.js +186 -0
  105. package/dist/browser/storage/cache-storage-repository.js.map +1 -0
  106. package/dist/browser/storage/localstorage-repository.d.ts +51 -0
  107. package/dist/browser/storage/localstorage-repository.d.ts.map +1 -0
  108. package/dist/browser/storage/localstorage-repository.js +270 -0
  109. package/dist/browser/storage/localstorage-repository.js.map +1 -0
  110. package/dist/builder.d.ts +24 -0
  111. package/dist/builder.d.ts.map +1 -0
  112. package/dist/builder.js +169 -0
  113. package/dist/builder.js.map +1 -0
  114. package/dist/cache/CacheManager.d.ts +132 -0
  115. package/dist/cache/CacheManager.d.ts.map +1 -0
  116. package/dist/cache/CacheManager.js +277 -0
  117. package/dist/cache/CacheManager.js.map +1 -0
  118. package/dist/cache/index.d.ts +6 -0
  119. package/dist/cache/index.d.ts.map +1 -0
  120. package/dist/cache/index.js +5 -0
  121. package/dist/cache/index.js.map +1 -0
  122. package/dist/config/index.d.ts +6 -0
  123. package/dist/config/index.d.ts.map +1 -0
  124. package/dist/config/index.js +6 -0
  125. package/dist/config/index.js.map +1 -0
  126. package/dist/config/lionrapid.config.d.ts +12 -0
  127. package/dist/config/lionrapid.config.d.ts.map +1 -0
  128. package/dist/config/lionrapid.config.js +2 -0
  129. package/dist/config/lionrapid.config.js.map +1 -0
  130. package/dist/config/logger.config.d.ts +19 -0
  131. package/dist/config/logger.config.d.ts.map +1 -0
  132. package/dist/config/logger.config.js +9 -0
  133. package/dist/config/logger.config.js.map +1 -0
  134. package/dist/config/repository.config.d.ts +87 -0
  135. package/dist/config/repository.config.d.ts.map +1 -0
  136. package/dist/config/repository.config.js +5 -0
  137. package/dist/config/repository.config.js.map +1 -0
  138. package/dist/content/index.d.ts +20 -0
  139. package/dist/content/index.d.ts.map +1 -0
  140. package/dist/content/index.js +13 -0
  141. package/dist/content/index.js.map +1 -0
  142. package/dist/content/parser/IParser.d.ts +33 -0
  143. package/dist/content/parser/IParser.d.ts.map +1 -0
  144. package/dist/content/parser/IParser.js +6 -0
  145. package/dist/content/parser/IParser.js.map +1 -0
  146. package/dist/content/parser/MarkupParser.d.ts +67 -0
  147. package/dist/content/parser/MarkupParser.d.ts.map +1 -0
  148. package/dist/content/parser/MarkupParser.js +231 -0
  149. package/dist/content/parser/MarkupParser.js.map +1 -0
  150. package/dist/content/serializer/HTMLSerializer.d.ts +61 -0
  151. package/dist/content/serializer/HTMLSerializer.d.ts.map +1 -0
  152. package/dist/content/serializer/HTMLSerializer.js +100 -0
  153. package/dist/content/serializer/HTMLSerializer.js.map +1 -0
  154. package/dist/content/serializer/ISerializer.d.ts +33 -0
  155. package/dist/content/serializer/ISerializer.d.ts.map +1 -0
  156. package/dist/content/serializer/ISerializer.js +6 -0
  157. package/dist/content/serializer/ISerializer.js.map +1 -0
  158. package/dist/content/style-mapper/DefaultStyleMapper.d.ts +59 -0
  159. package/dist/content/style-mapper/DefaultStyleMapper.d.ts.map +1 -0
  160. package/dist/content/style-mapper/DefaultStyleMapper.js +128 -0
  161. package/dist/content/style-mapper/DefaultStyleMapper.js.map +1 -0
  162. package/dist/content/style-mapper/IStyleMapper.d.ts +50 -0
  163. package/dist/content/style-mapper/IStyleMapper.d.ts.map +1 -0
  164. package/dist/content/style-mapper/IStyleMapper.js +6 -0
  165. package/dist/content/style-mapper/IStyleMapper.js.map +1 -0
  166. package/dist/content/types.d.ts +39 -0
  167. package/dist/content/types.d.ts.map +1 -0
  168. package/dist/content/types.js +6 -0
  169. package/dist/content/types.js.map +1 -0
  170. package/dist/content/validator/ContentValidator.d.ts +57 -0
  171. package/dist/content/validator/ContentValidator.d.ts.map +1 -0
  172. package/dist/content/validator/ContentValidator.js +215 -0
  173. package/dist/content/validator/ContentValidator.js.map +1 -0
  174. package/dist/content/validator/IValidator.d.ts +82 -0
  175. package/dist/content/validator/IValidator.d.ts.map +1 -0
  176. package/dist/content/validator/IValidator.js +6 -0
  177. package/dist/content/validator/IValidator.js.map +1 -0
  178. package/dist/errors/translation-errors.d.ts +55 -0
  179. package/dist/errors/translation-errors.d.ts.map +1 -0
  180. package/dist/errors/translation-errors.js +85 -0
  181. package/dist/errors/translation-errors.js.map +1 -0
  182. package/dist/handlers/async-storage-handler.d.ts +77 -0
  183. package/dist/handlers/async-storage-handler.d.ts.map +1 -0
  184. package/dist/handlers/async-storage-handler.js +239 -0
  185. package/dist/handlers/async-storage-handler.js.map +1 -0
  186. package/dist/handlers/handler-chain.d.ts +79 -0
  187. package/dist/handlers/handler-chain.d.ts.map +1 -0
  188. package/dist/handlers/handler-chain.js +366 -0
  189. package/dist/handlers/handler-chain.js.map +1 -0
  190. package/dist/handlers/index.d.ts +7 -0
  191. package/dist/handlers/index.d.ts.map +1 -0
  192. package/dist/handlers/index.js +7 -0
  193. package/dist/handlers/index.js.map +1 -0
  194. package/dist/handlers/network-handler.d.ts +90 -0
  195. package/dist/handlers/network-handler.d.ts.map +1 -0
  196. package/dist/handlers/network-handler.js +283 -0
  197. package/dist/handlers/network-handler.js.map +1 -0
  198. package/dist/handlers/sync-storage-handler.d.ts +88 -0
  199. package/dist/handlers/sync-storage-handler.d.ts.map +1 -0
  200. package/dist/handlers/sync-storage-handler.js +270 -0
  201. package/dist/handlers/sync-storage-handler.js.map +1 -0
  202. package/dist/index.d.ts +37 -0
  203. package/dist/index.d.ts.map +1 -0
  204. package/dist/index.js +30 -0
  205. package/dist/index.js.map +1 -0
  206. package/dist/interfaces/emitter.interface.d.ts +33 -0
  207. package/dist/interfaces/emitter.interface.d.ts.map +1 -0
  208. package/dist/interfaces/emitter.interface.js +2 -0
  209. package/dist/interfaces/emitter.interface.js.map +1 -0
  210. package/dist/interfaces/handler.interface.d.ts +50 -0
  211. package/dist/interfaces/handler.interface.d.ts.map +1 -0
  212. package/dist/interfaces/handler.interface.js +5 -0
  213. package/dist/interfaces/handler.interface.js.map +1 -0
  214. package/dist/interfaces/index.d.ts +8 -0
  215. package/dist/interfaces/index.d.ts.map +1 -0
  216. package/dist/interfaces/index.js +8 -0
  217. package/dist/interfaces/index.js.map +1 -0
  218. package/dist/interfaces/plugin.interface.d.ts +49 -0
  219. package/dist/interfaces/plugin.interface.d.ts.map +1 -0
  220. package/dist/interfaces/plugin.interface.js +6 -0
  221. package/dist/interfaces/plugin.interface.js.map +1 -0
  222. package/dist/interfaces/repository.interface.d.ts +56 -0
  223. package/dist/interfaces/repository.interface.d.ts.map +1 -0
  224. package/dist/interfaces/repository.interface.js +2 -0
  225. package/dist/interfaces/repository.interface.js.map +1 -0
  226. package/dist/lionRapid.d.ts +225 -0
  227. package/dist/lionRapid.d.ts.map +1 -0
  228. package/dist/lionRapid.js +596 -0
  229. package/dist/lionRapid.js.map +1 -0
  230. package/dist/repositories/index.d.ts +3 -0
  231. package/dist/repositories/index.d.ts.map +1 -0
  232. package/dist/repositories/index.js +3 -0
  233. package/dist/repositories/index.js.map +1 -0
  234. package/dist/repositories/memory-repository.d.ts +30 -0
  235. package/dist/repositories/memory-repository.d.ts.map +1 -0
  236. package/dist/repositories/memory-repository.js +100 -0
  237. package/dist/repositories/memory-repository.js.map +1 -0
  238. package/dist/repositories/network-repository.d.ts +138 -0
  239. package/dist/repositories/network-repository.d.ts.map +1 -0
  240. package/dist/repositories/network-repository.js +516 -0
  241. package/dist/repositories/network-repository.js.map +1 -0
  242. package/dist/sync/missing-translation-queue.d.ts +64 -0
  243. package/dist/sync/missing-translation-queue.d.ts.map +1 -0
  244. package/dist/sync/missing-translation-queue.js +184 -0
  245. package/dist/sync/missing-translation-queue.js.map +1 -0
  246. package/dist/types/cache.types.d.ts +62 -0
  247. package/dist/types/cache.types.d.ts.map +1 -0
  248. package/dist/types/cache.types.js +6 -0
  249. package/dist/types/cache.types.js.map +1 -0
  250. package/dist/types/event-payloads.types.d.ts +46 -0
  251. package/dist/types/event-payloads.types.d.ts.map +1 -0
  252. package/dist/types/event-payloads.types.js +6 -0
  253. package/dist/types/event-payloads.types.js.map +1 -0
  254. package/dist/types/generated.types.d.ts +31 -0
  255. package/dist/types/generated.types.d.ts.map +1 -0
  256. package/dist/types/generated.types.js +6 -0
  257. package/dist/types/generated.types.js.map +1 -0
  258. package/dist/types/index.d.ts +7 -0
  259. package/dist/types/index.d.ts.map +1 -0
  260. package/dist/types/index.js +7 -0
  261. package/dist/types/index.js.map +1 -0
  262. package/dist/types/lionRapid.types.d.ts +169 -0
  263. package/dist/types/lionRapid.types.d.ts.map +1 -0
  264. package/dist/types/lionRapid.types.js +8 -0
  265. package/dist/types/lionRapid.types.js.map +1 -0
  266. package/dist/types/parameters.types.d.ts +36 -0
  267. package/dist/types/parameters.types.d.ts.map +1 -0
  268. package/dist/types/parameters.types.js +6 -0
  269. package/dist/types/parameters.types.js.map +1 -0
  270. package/dist/types/translation-data.types.d.ts +54 -0
  271. package/dist/types/translation-data.types.d.ts.map +1 -0
  272. package/dist/types/translation-data.types.js +6 -0
  273. package/dist/types/translation-data.types.js.map +1 -0
  274. package/dist/types/translation.types.d.ts +94 -0
  275. package/dist/types/translation.types.d.ts.map +1 -0
  276. package/dist/types/translation.types.js +5 -0
  277. package/dist/types/translation.types.js.map +1 -0
  278. package/dist/utils/context-utils.d.ts +23 -0
  279. package/dist/utils/context-utils.d.ts.map +1 -0
  280. package/dist/utils/context-utils.js +36 -0
  281. package/dist/utils/context-utils.js.map +1 -0
  282. package/dist/utils/index.d.ts +10 -0
  283. package/dist/utils/index.d.ts.map +1 -0
  284. package/dist/utils/index.js +19 -0
  285. package/dist/utils/index.js.map +1 -0
  286. package/dist/utils/logger.d.ts +229 -0
  287. package/dist/utils/logger.d.ts.map +1 -0
  288. package/dist/utils/logger.js +345 -0
  289. package/dist/utils/logger.js.map +1 -0
  290. package/dist/utils/namespace-utils.d.ts +30 -0
  291. package/dist/utils/namespace-utils.d.ts.map +1 -0
  292. package/dist/utils/namespace-utils.js +56 -0
  293. package/dist/utils/namespace-utils.js.map +1 -0
  294. package/dist/utils/translation-flow.d.ts +18 -0
  295. package/dist/utils/translation-flow.d.ts.map +1 -0
  296. package/dist/utils/translation-flow.js +74 -0
  297. package/dist/utils/translation-flow.js.map +1 -0
  298. package/dist/utils/translation-value-utils.d.ts +57 -0
  299. package/dist/utils/translation-value-utils.d.ts.map +1 -0
  300. package/dist/utils/translation-value-utils.js +194 -0
  301. package/dist/utils/translation-value-utils.js.map +1 -0
  302. package/package.json +76 -0
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Markup parser for converting HTML strings to ContentUnit
3
+ * @module content/parser/MarkupParser
4
+ */
5
+ import type { IParser } from './IParser.js';
6
+ import type { ContentUnit } from '../types.js';
7
+ import type { IStyleMapper } from '../style-mapper/IStyleMapper.js';
8
+ /**
9
+ * Configuration options for MarkupParser
10
+ */
11
+ export interface MarkupParserOptions {
12
+ /**
13
+ * Custom style mapper for element ↔ style conversion
14
+ * Defaults to DefaultStyleMapper
15
+ */
16
+ styleMapper?: IStyleMapper;
17
+ }
18
+ /**
19
+ * Parser that converts HTML markup strings to ContentUnit format
20
+ *
21
+ * Transforms inline HTML elements into indexed tags with style mapping:
22
+ * - `<b>bold</b>` → text: `<1>bold</1>`, styles: `{"1": ["bold"]}`
23
+ * - `<a href="/url">link</a>` → styles: `{"1": ["hyperlink:/url"]}`
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const parser = new MarkupParser();
28
+ *
29
+ * const unit = parser.parse('Read our <b>Terms</b>.');
30
+ * // → { text: "Read our <1>Terms</1>.", styles: {"1": ["bold"]} }
31
+ *
32
+ * const nested = parser.parse('<b>bold <i>and italic</i></b>');
33
+ * // → { text: "<1>bold <2>and italic</2></1>", styles: {"1": ["bold"], "2": ["italic"]} }
34
+ * ```
35
+ */
36
+ export declare class MarkupParser implements IParser<string> {
37
+ private readonly styleMapper;
38
+ constructor(options?: MarkupParserOptions);
39
+ /**
40
+ * Parse HTML string into ContentUnit
41
+ *
42
+ * Handles two input formats:
43
+ * 1. HTML tags: `<b>text</b>` → converts to indexed `<1>text</1>` with styles
44
+ * 2. Already indexed: `<1>text</1>` → passes through as-is (from server)
45
+ *
46
+ * @param input - HTML string with inline elements or already indexed
47
+ * @returns ContentUnit with indexed tags and styles
48
+ */
49
+ parse(input: string): ContentUnit;
50
+ /**
51
+ * Match an HTML tag at the given position
52
+ */
53
+ private matchTag;
54
+ /**
55
+ * Parse HTML attributes string into key-value pairs
56
+ */
57
+ private parseAttributes;
58
+ /**
59
+ * Find matching opening tag in the stack
60
+ */
61
+ private findMatchingOpenTag;
62
+ /**
63
+ * Find the closing tag index for a given opening tag
64
+ */
65
+ private findClosingTagIndex;
66
+ }
67
+ //# sourceMappingURL=MarkupParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MarkupParser.d.ts","sourceRoot":"","sources":["../../../src/content/parser/MarkupParser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAGpE;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,WAAW,CAAC,EAAE,YAAY,CAAC;CAC5B;AAaD;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,YAAa,YAAW,OAAO,CAAC,MAAM,CAAC;IAClD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAe;gBAE/B,OAAO,GAAE,mBAAwB;IAI7C;;;;;;;;;OASG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW;IAuIjC;;OAEG;IACH,OAAO,CAAC,QAAQ;IA0ChB;;OAEG;IACH,OAAO,CAAC,eAAe;IAiBvB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAa3B;;OAEG;IACH,OAAO,CAAC,mBAAmB;CA6B5B"}
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Markup parser for converting HTML strings to ContentUnit
3
+ * @module content/parser/MarkupParser
4
+ */
5
+ import { DefaultStyleMapper } from '../style-mapper/DefaultStyleMapper.js';
6
+ /**
7
+ * Parser that converts HTML markup strings to ContentUnit format
8
+ *
9
+ * Transforms inline HTML elements into indexed tags with style mapping:
10
+ * - `<b>bold</b>` → text: `<1>bold</1>`, styles: `{"1": ["bold"]}`
11
+ * - `<a href="/url">link</a>` → styles: `{"1": ["hyperlink:/url"]}`
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const parser = new MarkupParser();
16
+ *
17
+ * const unit = parser.parse('Read our <b>Terms</b>.');
18
+ * // → { text: "Read our <1>Terms</1>.", styles: {"1": ["bold"]} }
19
+ *
20
+ * const nested = parser.parse('<b>bold <i>and italic</i></b>');
21
+ * // → { text: "<1>bold <2>and italic</2></1>", styles: {"1": ["bold"], "2": ["italic"]} }
22
+ * ```
23
+ */
24
+ export class MarkupParser {
25
+ styleMapper;
26
+ constructor(options = {}) {
27
+ this.styleMapper = options.styleMapper ?? new DefaultStyleMapper();
28
+ }
29
+ /**
30
+ * Parse HTML string into ContentUnit
31
+ *
32
+ * Handles two input formats:
33
+ * 1. HTML tags: `<b>text</b>` → converts to indexed `<1>text</1>` with styles
34
+ * 2. Already indexed: `<1>text</1>` → passes through as-is (from server)
35
+ *
36
+ * @param input - HTML string with inline elements or already indexed
37
+ * @returns ContentUnit with indexed tags and styles
38
+ */
39
+ parse(input) {
40
+ // Check if input already uses indexed tags (from server)
41
+ // Pattern: <1>, </1>, <2>, </2>, etc.
42
+ const hasIndexedTags = /<\/?[0-9]+>/.test(input);
43
+ if (hasIndexedTags) {
44
+ // Input already has indexed tags - pass through without style mapping
45
+ // The styles will come from component children in Trans, not from server
46
+ return {
47
+ text: input,
48
+ styles: {}, // Empty - styles determined by children
49
+ };
50
+ }
51
+ const styles = {};
52
+ let tagIndex = 0;
53
+ // Stack to track open tags and their indices
54
+ const tagStack = [];
55
+ // Result text being built
56
+ let result = '';
57
+ let pos = 0;
58
+ while (pos < input.length) {
59
+ // Look for opening tag
60
+ const tagMatch = this.matchTag(input, pos);
61
+ if (!tagMatch) {
62
+ // No more tags, append rest of string
63
+ result += input.slice(pos);
64
+ break;
65
+ }
66
+ // Append text before tag
67
+ result += input.slice(pos, tagMatch.startIndex);
68
+ if (tagMatch.isClosing) {
69
+ // Closing tag - find matching opening tag
70
+ const matchingOpen = this.findMatchingOpenTag(tagStack, tagMatch.tag.toLowerCase());
71
+ if (matchingOpen !== null) {
72
+ result += `</${matchingOpen}>`;
73
+ // Remove from stack
74
+ const stackIndex = tagStack.findIndex((t) => t.index === matchingOpen);
75
+ if (stackIndex !== -1) {
76
+ tagStack.splice(stackIndex, 1);
77
+ }
78
+ }
79
+ else {
80
+ // Unmatched closing tag - keep as-is (will be validated later)
81
+ result += input.slice(tagMatch.startIndex, tagMatch.endIndex);
82
+ }
83
+ }
84
+ else if (tagMatch.isSelfClosing) {
85
+ // Self-closing tag - assign index but no content
86
+ tagIndex++;
87
+ const styleList = this.styleMapper.mapElementToStyles(tagMatch.tag, tagMatch.attrs);
88
+ if (styleList.length > 0) {
89
+ styles[String(tagIndex)] = styleList;
90
+ result += `<${tagIndex}/>`;
91
+ }
92
+ // If no styles mapped, skip the tag entirely
93
+ }
94
+ else {
95
+ // Opening tag
96
+ const styleList = this.styleMapper.mapElementToStyles(tagMatch.tag, tagMatch.attrs);
97
+ if (styleList.length > 0) {
98
+ tagIndex++;
99
+ styles[String(tagIndex)] = styleList;
100
+ result += `<${tagIndex}>`;
101
+ tagStack.push({ tag: tagMatch.tag.toLowerCase(), index: tagIndex });
102
+ }
103
+ else {
104
+ // Unknown element - don't index it, just process content
105
+ // We need to find the closing tag and process content
106
+ const closingIndex = this.findClosingTagIndex(input, tagMatch.endIndex, tagMatch.tag);
107
+ if (closingIndex !== -1) {
108
+ // Recursively parse content
109
+ const innerContent = input.slice(tagMatch.endIndex, closingIndex);
110
+ const innerUnit = this.parse(innerContent);
111
+ // Merge inner styles (adjusting indices based on current tagIndex)
112
+ const baseIndex = tagIndex;
113
+ for (const [key, value] of Object.entries(innerUnit.styles)) {
114
+ const adjustedKey = String(baseIndex + parseInt(key, 10));
115
+ styles[adjustedKey] = value;
116
+ }
117
+ // Adjust result and indices
118
+ result += innerUnit.text.replace(/<(\/?)\d+>/g, (match, slash) => {
119
+ const num = parseInt(match.replace(/[</>]/g, ''), 10);
120
+ return `<${slash}${baseIndex + num}>`;
121
+ });
122
+ // Move position past closing tag
123
+ const closingMatch = this.matchTag(input, closingIndex);
124
+ pos = closingMatch ? closingMatch.endIndex : closingIndex;
125
+ // Adjust tagIndex to account for inner tags
126
+ const maxInnerIndex = Object.keys(innerUnit.styles).length > 0
127
+ ? Math.max(...Object.keys(innerUnit.styles).map(Number))
128
+ : 0;
129
+ tagIndex = baseIndex + maxInnerIndex;
130
+ continue;
131
+ }
132
+ }
133
+ }
134
+ pos = tagMatch.endIndex;
135
+ }
136
+ // Handle unclosed tags (add closing tags in reverse order)
137
+ while (tagStack.length > 0) {
138
+ const unclosed = tagStack.pop();
139
+ result += `</${unclosed.index}>`;
140
+ }
141
+ return {
142
+ text: result,
143
+ styles,
144
+ };
145
+ }
146
+ /**
147
+ * Match an HTML tag at the given position
148
+ */
149
+ matchTag(input, startPos) {
150
+ // Find next '<' from startPos
151
+ const tagStart = input.indexOf('<', startPos);
152
+ if (tagStart === -1)
153
+ return null;
154
+ // Match tag pattern: <tagname attrs> or </tagname> or <tagname/>
155
+ const tagPattern = /^<(\/?)([a-zA-Z][a-zA-Z0-9]*)((?:\s+[a-zA-Z][a-zA-Z0-9-]*(?:=(?:"[^"]*"|'[^']*'|[^\s>]*))?)*)\s*(\/?)>/;
156
+ const remaining = input.slice(tagStart);
157
+ const match = remaining.match(tagPattern);
158
+ if (!match) {
159
+ // Not a valid tag, skip this '<' and try again
160
+ if (tagStart + 1 < input.length) {
161
+ return this.matchTag(input, tagStart + 1);
162
+ }
163
+ return null;
164
+ }
165
+ const [fullMatch, closingSlash, tagName, attrsString, selfClosingSlash] = match;
166
+ return {
167
+ tag: tagName,
168
+ attrs: this.parseAttributes(attrsString),
169
+ isClosing: closingSlash === '/',
170
+ isSelfClosing: selfClosingSlash === '/',
171
+ startIndex: tagStart,
172
+ endIndex: tagStart + fullMatch.length,
173
+ };
174
+ }
175
+ /**
176
+ * Parse HTML attributes string into key-value pairs
177
+ */
178
+ parseAttributes(attrsString) {
179
+ const attrs = {};
180
+ if (!attrsString.trim())
181
+ return attrs;
182
+ // Match attribute patterns: name="value" or name='value' or name=value or name
183
+ const attrPattern = /([a-zA-Z][a-zA-Z0-9-]*)(?:=(?:"([^"]*)"|'([^']*)'|([^\s>]*)))?/g;
184
+ let match;
185
+ while ((match = attrPattern.exec(attrsString)) !== null) {
186
+ const [, name, doubleQuoted, singleQuoted, unquoted] = match;
187
+ const value = doubleQuoted ?? singleQuoted ?? unquoted ?? '';
188
+ attrs[name.toLowerCase()] = value;
189
+ }
190
+ return attrs;
191
+ }
192
+ /**
193
+ * Find matching opening tag in the stack
194
+ */
195
+ findMatchingOpenTag(stack, tagName) {
196
+ // Search from end of stack (most recent)
197
+ for (let i = stack.length - 1; i >= 0; i--) {
198
+ if (stack[i].tag === tagName) {
199
+ return stack[i].index;
200
+ }
201
+ }
202
+ return null;
203
+ }
204
+ /**
205
+ * Find the closing tag index for a given opening tag
206
+ */
207
+ findClosingTagIndex(input, startPos, tagName) {
208
+ const lowerTag = tagName.toLowerCase();
209
+ let depth = 1;
210
+ let pos = startPos;
211
+ while (pos < input.length && depth > 0) {
212
+ const tagMatch = this.matchTag(input, pos);
213
+ if (!tagMatch)
214
+ break;
215
+ if (tagMatch.tag.toLowerCase() === lowerTag) {
216
+ if (tagMatch.isClosing) {
217
+ depth--;
218
+ if (depth === 0) {
219
+ return tagMatch.startIndex;
220
+ }
221
+ }
222
+ else if (!tagMatch.isSelfClosing) {
223
+ depth++;
224
+ }
225
+ }
226
+ pos = tagMatch.endIndex;
227
+ }
228
+ return -1;
229
+ }
230
+ }
231
+ //# sourceMappingURL=MarkupParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MarkupParser.js","sourceRoot":"","sources":["../../../src/content/parser/MarkupParser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAwB3E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,OAAO,YAAY;IAC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2
+ * HTML serializer for converting ContentUnit to HTML strings
3
+ * @module content/serializer/HTMLSerializer
4
+ */
5
+ import type { ISerializer } from './ISerializer.js';
6
+ import type { ContentUnit } from '../types.js';
7
+ import type { IStyleMapper } from '../style-mapper/IStyleMapper.js';
8
+ /**
9
+ * Configuration options for HTMLSerializer
10
+ */
11
+ export interface HTMLSerializerOptions {
12
+ /**
13
+ * Custom style mapper for styles → element conversion
14
+ * Defaults to DefaultStyleMapper
15
+ */
16
+ styleMapper?: IStyleMapper;
17
+ }
18
+ /**
19
+ * Serializer that converts ContentUnit format back to HTML strings
20
+ *
21
+ * Transforms indexed tags and style mappings into HTML:
22
+ * - `<1>bold</1>` with styles `{"1": ["bold"]}` → `<b>bold</b>`
23
+ * - `<1>link</1>` with styles `{"1": ["hyperlink:/url"]}` → `<a href="/url">link</a>`
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const serializer = new HTMLSerializer();
28
+ *
29
+ * const html = serializer.serialize({
30
+ * text: 'Read our <1>Terms</1>.',
31
+ * styles: { '1': ['bold'] }
32
+ * });
33
+ * // → "Read our <b>Terms</b>."
34
+ *
35
+ * const link = serializer.serialize({
36
+ * text: 'Click <1>here</1>.',
37
+ * styles: { '1': ['hyperlink:/url'] }
38
+ * });
39
+ * // → "Click <a href=\"/url\">here</a>."
40
+ * ```
41
+ */
42
+ export declare class HTMLSerializer implements ISerializer<string> {
43
+ private readonly styleMapper;
44
+ constructor(options?: HTMLSerializerOptions);
45
+ /**
46
+ * Serialize ContentUnit to HTML string
47
+ *
48
+ * @param unit - ContentUnit with indexed tags and styles
49
+ * @returns HTML string with semantic elements
50
+ */
51
+ serialize(unit: ContentUnit): string;
52
+ /**
53
+ * Format attributes object into HTML attribute string
54
+ */
55
+ private formatAttributes;
56
+ /**
57
+ * Escape special characters in attribute values
58
+ */
59
+ private escapeAttributeValue;
60
+ }
61
+ //# sourceMappingURL=HTMLSerializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HTMLSerializer.d.ts","sourceRoot":"","sources":["../../../src/content/serializer/HTMLSerializer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAGpE;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,WAAW,CAAC,EAAE,YAAY,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,cAAe,YAAW,WAAW,CAAC,MAAM,CAAC;IACxD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAe;gBAE/B,OAAO,GAAE,qBAA0B;IAI/C;;;;;OAKG;IACH,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM;IA2CpC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAYxB;;OAEG;IACH,OAAO,CAAC,oBAAoB;CAO7B"}
@@ -0,0 +1,100 @@
1
+ /**
2
+ * HTML serializer for converting ContentUnit to HTML strings
3
+ * @module content/serializer/HTMLSerializer
4
+ */
5
+ import { DefaultStyleMapper } from '../style-mapper/DefaultStyleMapper.js';
6
+ /**
7
+ * Serializer that converts ContentUnit format back to HTML strings
8
+ *
9
+ * Transforms indexed tags and style mappings into HTML:
10
+ * - `<1>bold</1>` with styles `{"1": ["bold"]}` → `<b>bold</b>`
11
+ * - `<1>link</1>` with styles `{"1": ["hyperlink:/url"]}` → `<a href="/url">link</a>`
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const serializer = new HTMLSerializer();
16
+ *
17
+ * const html = serializer.serialize({
18
+ * text: 'Read our <1>Terms</1>.',
19
+ * styles: { '1': ['bold'] }
20
+ * });
21
+ * // → "Read our <b>Terms</b>."
22
+ *
23
+ * const link = serializer.serialize({
24
+ * text: 'Click <1>here</1>.',
25
+ * styles: { '1': ['hyperlink:/url'] }
26
+ * });
27
+ * // → "Click <a href=\"/url\">here</a>."
28
+ * ```
29
+ */
30
+ export class HTMLSerializer {
31
+ styleMapper;
32
+ constructor(options = {}) {
33
+ this.styleMapper = options.styleMapper ?? new DefaultStyleMapper();
34
+ }
35
+ /**
36
+ * Serialize ContentUnit to HTML string
37
+ *
38
+ * @param unit - ContentUnit with indexed tags and styles
39
+ * @returns HTML string with semantic elements
40
+ */
41
+ serialize(unit) {
42
+ const { text, styles } = unit;
43
+ // Pattern to match indexed tags: <1>, </1>, <1/>
44
+ const tagPattern = /<(\/?)?(\d+)(\/)?>/g;
45
+ let result = '';
46
+ let lastIndex = 0;
47
+ let match;
48
+ while ((match = tagPattern.exec(text)) !== null) {
49
+ // Append text before the tag
50
+ result += text.slice(lastIndex, match.index);
51
+ const [fullMatch, closingSlash, indexStr, selfClosingSlash] = match;
52
+ const tagIndex = indexStr;
53
+ const styleList = styles[tagIndex] || [];
54
+ if (closingSlash) {
55
+ // Closing tag: </1>
56
+ const mapping = this.styleMapper.mapStylesToElement(styleList);
57
+ result += `</${mapping.element}>`;
58
+ }
59
+ else if (selfClosingSlash) {
60
+ // Self-closing tag: <1/>
61
+ const mapping = this.styleMapper.mapStylesToElement(styleList);
62
+ const attrs = this.formatAttributes(mapping.attrs);
63
+ result += `<${mapping.element}${attrs} />`;
64
+ }
65
+ else {
66
+ // Opening tag: <1>
67
+ const mapping = this.styleMapper.mapStylesToElement(styleList);
68
+ const attrs = this.formatAttributes(mapping.attrs);
69
+ result += `<${mapping.element}${attrs}>`;
70
+ }
71
+ lastIndex = match.index + fullMatch.length;
72
+ }
73
+ // Append remaining text
74
+ result += text.slice(lastIndex);
75
+ return result;
76
+ }
77
+ /**
78
+ * Format attributes object into HTML attribute string
79
+ */
80
+ formatAttributes(attrs) {
81
+ const entries = Object.entries(attrs);
82
+ if (entries.length === 0)
83
+ return '';
84
+ return (' ' +
85
+ entries
86
+ .map(([key, value]) => `${key}="${this.escapeAttributeValue(value)}"`)
87
+ .join(' '));
88
+ }
89
+ /**
90
+ * Escape special characters in attribute values
91
+ */
92
+ escapeAttributeValue(value) {
93
+ return value
94
+ .replace(/&/g, '&amp;')
95
+ .replace(/"/g, '&quot;')
96
+ .replace(/</g, '&lt;')
97
+ .replace(/>/g, '&gt;');
98
+ }
99
+ }
100
+ //# sourceMappingURL=HTMLSerializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HTMLSerializer.js","sourceRoot":"","sources":["../../../src/content/serializer/HTMLSerializer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAa3E;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,cAAc;IACR,WAAW,CAAe;IAE3C,YAAY,UAAiC,EAAE;QAC7C,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,IAAI,kBAAkB,EAAE,CAAC;IACrE,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,IAAiB;QACzB,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAE9B,iDAAiD;QACjD,MAAM,UAAU,GAAG,qBAAqB,CAAC;QAEzC,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,IAAI,KAAK,CAAC;QAEV,OAAO,CAAC,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;YAChD,6BAA6B;YAC7B,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;YAE7C,MAAM,CAAC,SAAS,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,CAAC,GAAG,KAAK,CAAC;YACpE,MAAM,QAAQ,GAAG,QAAQ,CAAC;YAC1B,MAAM,SAAS,GAAG,MAAM,CAAC,Q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E,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,KAAK,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,GAAG,CAAC;iBACrE,IAAI,CAAC,GAAG,CAAC,CACb,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,oBAAoB,CAAC,KAAa;QACxC,OAAO,KAAK;aACT,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;aACtB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;aACvB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;aACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Serializer interface for converting ContentUnit to platform-specific output
3
+ * @module content/serializer/ISerializer
4
+ */
5
+ import type { ContentUnit } from '../types.js';
6
+ /**
7
+ * Generic serializer interface
8
+ *
9
+ * Implementations:
10
+ * - HTMLSerializer: ContentUnit → HTML string (core)
11
+ * - JSXSerializer: ContentUnit → ReactNode (react package)
12
+ *
13
+ * @typeParam TOutput - Platform-specific output type
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * class HTMLSerializer implements ISerializer<string> {
18
+ * serialize(unit: ContentUnit): string {
19
+ * // Convert indexed tags back to <b>, <a>, etc.
20
+ * }
21
+ * }
22
+ * ```
23
+ */
24
+ export interface ISerializer<TOutput> {
25
+ /**
26
+ * Serialize ContentUnit to platform-specific output
27
+ *
28
+ * @param unit - The ContentUnit to serialize
29
+ * @returns Platform-specific output (HTML string, ReactNode, etc.)
30
+ */
31
+ serialize(unit: ContentUnit): TOutput;
32
+ }
33
+ //# sourceMappingURL=ISerializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ISerializer.d.ts","sourceRoot":"","sources":["../../../src/content/serializer/ISerializer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,WAAW,WAAW,CAAC,OAAO;IAClC;;;;;OAKG;IACH,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC;CACvC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Serializer interface for converting ContentUnit to platform-specific output
3
+ * @module content/serializer/ISerializer
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=ISerializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ISerializer.js","sourceRoot":"","sources":["../../../src/content/serializer/ISerializer.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Default style mapper for bidirectional HTML element ↔ style property mapping
3
+ * @module content/style-mapper/DefaultStyleMapper
4
+ */
5
+ import type { IStyleMapper, ElementMapping } from './IStyleMapper.js';
6
+ /**
7
+ * Default implementation of style mapping
8
+ *
9
+ * Provides bidirectional conversion between HTML elements and style properties:
10
+ * - mapElementToStyles: `<b>` → `["bold"]`
11
+ * - mapStylesToElement: `["bold"]` → `<b>`
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const mapper = new DefaultStyleMapper();
16
+ *
17
+ * // Parsing: HTML → styles
18
+ * mapper.mapElementToStyles('a', { href: '/terms' });
19
+ * // → ["hyperlink:/terms"]
20
+ *
21
+ * // Serializing: styles → HTML
22
+ * mapper.mapStylesToElement(['bold', 'hyperlink:/terms']);
23
+ * // → { element: 'a', attrs: { href: '/terms' } }
24
+ * ```
25
+ */
26
+ export declare class DefaultStyleMapper implements IStyleMapper {
27
+ /**
28
+ * Element → Style mapping
29
+ * Maps HTML element names to their style property
30
+ */
31
+ private readonly elementToStyles;
32
+ /**
33
+ * Style → Element mapping (reverse)
34
+ * Uses simple, familiar elements for developer convenience
35
+ * Also accepts element names directly (strong, em, etc.)
36
+ */
37
+ private readonly styleToElement;
38
+ /**
39
+ * Convert HTML element to style properties
40
+ *
41
+ * @param element - Tag name (e.g., "b", "a", "strong")
42
+ * @param attrs - Element attributes (e.g., { href: "/url" })
43
+ * @returns Array of style strings
44
+ */
45
+ mapElementToStyles(element: string, attrs: Record<string, string>): string[];
46
+ /**
47
+ * Convert style properties to HTML element
48
+ *
49
+ * Priority order:
50
+ * 1. Hyperlink (becomes wrapper element with href)
51
+ * 2. First recognized style property
52
+ * 3. Fallback to span
53
+ *
54
+ * @param styles - Array of style strings
55
+ * @returns Element info for serialization
56
+ */
57
+ mapStylesToElement(styles: string[]): ElementMapping;
58
+ }
59
+ //# sourceMappingURL=DefaultStyleMapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DefaultStyleMapper.d.ts","sourceRoot":"","sources":["../../../src/content/style-mapper/DefaultStyleMapper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACrD;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,eAAe,CAa9B;IAEF;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,cAAc,CAsB7B;IAEF;;;;;;OAMG;IACH,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,EAAE;IAkB5E;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc;CAyBrD"}