1+ // @ts -nocheck
2+ // src/processors/markup/MarkdownToLinks.js
3+
4+ import logger from '../../utils/Logger.js'
5+ import ns from '../../utils/ns.js'
6+ import Processor from '../../model/Processor.js'
7+
8+ /**
9+ * @class MarkdownToLinks
10+ * @extends Processor
11+ * @classdesc
12+ * **a Transmissions Processor**
13+ *
14+ * MarkdownToLinks processor demonstrating basic processor structure and property handling.
15+ *
16+ * ### Processor Signature
17+ *
18+ * #### __*Settings*__
19+ * * **`ns.trn.me`** - Identifier for the processor instance
20+ * * **`ns.trn.common`** - Common value to be added to the message
21+ * * **`ns.trn.something1`** - First value to be processed
22+ * * **`ns.trn.something2`** - Second value to be processed
23+ * * **`ns.trn.added`** - Optional string to append to something1
24+ * * **`ns.trn.notavalue`** - Fallback value if not provided in config
25+ *
26+ * #### __*Input*__
27+ * * **`message`** - The message object to be processed
28+ *
29+ * #### __*Output*__
30+ * * **`message`** - The modified message object with added/updated fields
31+ *
32+ * #### __*Behavior*__
33+ * * Forwards message immediately if `message.done` is true
34+ * * Retrieves and processes configuration properties
35+ * * Appends optional values to message fields
36+ * * Handles fallback values for missing properties
37+ *
38+ * #### __*Side Effects*__
39+ * * Modifies the input message object
40+ * * Logs processing information
41+ *
42+ * #### __*Notes*__
43+ * This is an MarkdownToLinks implementation demonstrating:
44+ * - Basic processor structure
45+ * - Property retrieval with fallbacks
46+ * - Message modification
47+ * - Logging
48+ *
49+ * Use this as a template when creating new processors.
50+ */
51+ class MarkdownToLinks extends Processor {
52+ /**
53+ * Creates a new MarkdownToLinks processor instance.
54+ * @param {Object } config - Processor configuration object
55+ */
56+ constructor ( config ) {
57+ super ( config )
58+ }
59+
60+ /**
61+ * Processes the message by applying property transformations.
62+ * @param {Object } message - The message to process
63+ * @returns {Promise<boolean> } Resolves when processing is complete
64+ */
65+ async process ( message ) {
66+ logger . debug ( `\n\nMarkdownToLinks.process` )
67+
68+ // TODO figure this out better
69+ // may be needed if preceded by a spawning processor, eg. fs/DirWalker
70+ if ( message . done ) {
71+ return this . emit ( 'message' , message )
72+ // or simply return
73+ }
74+
75+ message . links = markdownToLinks ( message . content )
76+
77+ return this . emit ( 'message' , message )
78+ }
79+
80+
81+
82+ markdownToLinks ( markdown ) {
83+ // Regex to find markdown links: [text](url)
84+ const regex = / \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) ] + ) \) / g;
85+ const result = [ ] ;
86+ let match ;
87+ while ( ( match = regex . exec ( markdown ) ) !== null ) {
88+ result . push ( { url : match [ 2 ] , text : match [ 1 ] } ) ;
89+ }
90+ return result ;
91+ }
92+
93+ // Example usage
94+ const markdownText = "This is a [link to Google](https://google.com) and here is [another link](https://example.com)." ;
95+ console . log ( markdownToLinks ( markdownText ) ) ;
96+
97+ }
98+ export default MarkdownToLinks
0 commit comments