|
1 |
| -import { replaceWrapper, newLine } from '../helpers'; |
| 1 | +// TODO explore other ways of doing replacing thing |
| 2 | +// https://www.npmjs.com/package/string-replace-to-array |
| 3 | +// https://www.npmjs.com/package/batch-replace |
| 4 | +// https://www.npmjs.com/package/pupa |
2 | 5 |
|
3 |
| -const italic = '$1<em>$3</em>$4'; |
4 |
| -const del = '<del>$1</del>'; |
5 |
| -const q = '<q>$1</q>'; |
6 |
| -const code = '<code>$1</code>'; |
7 |
| -const hr = `${newLine}<hr />`; |
8 |
| -const empty = ''; |
9 |
| -// const strong = "<strong>$2$3</strong>"; |
| 6 | +// import { sponsorship } from "./callbacksHtml/methods/callbacks"; |
| 7 | +// import { separator } from "./callbacksHtml/methods/custom"; |
| 8 | +import { previewText } from './callbacksHtml/methods/simple'; |
| 9 | +import { |
| 10 | + strong, |
| 11 | + link, |
| 12 | + blockquote, |
| 13 | + mem, |
| 14 | + header, |
| 15 | + italic, |
| 16 | + del, |
| 17 | + q, |
| 18 | + code, |
| 19 | + hr, |
| 20 | + // empty, |
| 21 | + ulList, |
| 22 | + olList, |
| 23 | + image, |
| 24 | + paragraphWrapper, |
| 25 | + sponsorship, |
| 26 | + br, |
| 27 | + separator, |
| 28 | +} from './callbacksHtml/index'; |
| 29 | +// function extractOptions(converter, key) { |
| 30 | +// if (!converter.key) throw new Error('no options for this converter'); |
| 31 | +import { |
| 32 | + REGEXP_HEADER, |
| 33 | + REGEXP_IMAGE, |
| 34 | + REGEXP_LINK, |
| 35 | + REGEXP_STRONG, |
| 36 | + REGEXP_DEL, |
| 37 | + REGEXP_Q, |
| 38 | + REGEXP_CODE, |
| 39 | + REGEXP_UL_LIST, |
| 40 | + REGEXP_OL_LIST, |
| 41 | + REGEXP_BLOCKQUOTE, |
| 42 | + REGEXP_HR, |
| 43 | + REGEXP_BR, |
| 44 | + REGEXP_EM, |
| 45 | + REGEXP_SPONSORSHIP, |
| 46 | + REGEXP_MEM, |
| 47 | + REGEXP_PREVIEW_TEXT, |
| 48 | + REGEXP_PARAGRAPH, |
| 49 | + REGEXP_SEPARATOR, |
| 50 | +} from '../constants'; |
| 51 | +// return converter.key; |
| 52 | +// } |
10 | 53 |
|
11 |
| -function strong(text, doubleAsterix, content, asterix) { |
12 |
| - const config = { |
13 |
| - content: `${content + asterix}`, |
14 |
| - }; |
| 54 | +// TODO what is it? |
| 55 | +/* function replaceMarkdownNew(key) { |
| 56 | + const { regexp, callback } = extractOptions(converter, key); |
15 | 57 |
|
16 |
| - const replaced = replaceWrapper('strong', config); |
| 58 | + // const {regexp, callback} = options; |
17 | 59 |
|
18 |
| - return replaced; |
19 |
| -} |
| 60 | + // try { |
20 | 61 |
|
21 |
| -function link(text, title, href) { |
22 |
| - const config = { |
23 |
| - content: title.trim(), |
24 |
| - href: href.trim(), |
25 |
| - }; |
| 62 | + // if (!regexp) throw new Error('regular expression is blank'); |
| 63 | + // if (!callback) throw new Error('no callback presented'); |
26 | 64 |
|
27 |
| - const replaced = replaceWrapper('link', config); |
| 65 | + // // ... add more here later |
28 | 66 |
|
29 |
| - return replaced; |
| 67 | + // } catch(err) { |
| 68 | + // // we need to test how it actually work |
| 69 | + // var caller_line = err.stack.split("\n")[4]; |
| 70 | + // var index = caller_line.indexOf("at "); |
| 71 | + // var clean = caller_line.slice(index+2, caller_line.length); |
| 72 | +
|
| 73 | + // throw err; |
| 74 | + // } |
| 75 | +} */ |
| 76 | + |
| 77 | +// TODO THIS WILL BE BROKEN SOOOOON |
| 78 | +// This structure was before |
| 79 | +// this.replaceMDBinded(this._constants.REGEXP_PREVIEW_TEXT, previewText); |
| 80 | +// Should be working like this |
| 81 | +// this.replaceMDBinded("previewText"); |
| 82 | + |
| 83 | + |
| 84 | +// @TODO map is a stupid name, that is also a name for a lodash method. |
| 85 | +const map = { |
| 86 | + strong: { |
| 87 | + constant: REGEXP_STRONG, |
| 88 | + replacer: strong, |
| 89 | + }, |
| 90 | + previewText: { |
| 91 | + constant: REGEXP_PREVIEW_TEXT, |
| 92 | + replacer: previewText, |
| 93 | + }, |
| 94 | + // 'empty': { |
| 95 | + // constant: REGEXP_HTML_COMMENTS, |
| 96 | + // replacer: comments |
| 97 | + // }, |
| 98 | + italic: { |
| 99 | + constant: REGEXP_EM, |
| 100 | + replacer: italic, |
| 101 | + }, |
| 102 | + header: { |
| 103 | + constant: REGEXP_HEADER, |
| 104 | + replacer: header, |
| 105 | + }, |
| 106 | + image: { |
| 107 | + constant: REGEXP_IMAGE, |
| 108 | + replacer: image, |
| 109 | + }, |
| 110 | + link: { |
| 111 | + constant: REGEXP_LINK, |
| 112 | + replacer: link, |
| 113 | + }, |
| 114 | + del: { |
| 115 | + constant: REGEXP_DEL, |
| 116 | + replacer: del, |
| 117 | + }, |
| 118 | + q: { |
| 119 | + constant: REGEXP_Q, |
| 120 | + replacer: q, |
| 121 | + }, |
| 122 | + code: { |
| 123 | + constant: REGEXP_CODE, |
| 124 | + replacer: code, |
| 125 | + }, |
| 126 | + ulList: { |
| 127 | + constant: REGEXP_UL_LIST, |
| 128 | + replacer: ulList, |
| 129 | + }, |
| 130 | + olList: { |
| 131 | + constant: REGEXP_OL_LIST, |
| 132 | + replacer: olList, |
| 133 | + }, |
| 134 | + blockquote: { |
| 135 | + constant: REGEXP_BLOCKQUOTE, |
| 136 | + replacer: blockquote, |
| 137 | + }, |
| 138 | + hr: { |
| 139 | + constant: REGEXP_HR, |
| 140 | + replacer: hr, |
| 141 | + }, |
| 142 | + paragraphWrapper: { |
| 143 | + constant: REGEXP_PARAGRAPH, |
| 144 | + replacer: paragraphWrapper, |
| 145 | + }, |
| 146 | + // 'REGEXP_EMPTY_UL': { |
| 147 | + // constant: REGEXP_EMPTY_UL, |
| 148 | + // replacer: emptyUl |
| 149 | + // }, |
| 150 | + // 'REGEXP_EMPTY_OL': { |
| 151 | + // constant: REGEXP_EMPTY_OL, |
| 152 | + // replacer: emptyOl |
| 153 | + // }, |
| 154 | + // 'REGEXP_EMPTY_BLOCKQUOTE': { |
| 155 | + // constant: REGEXP_EMPTY_BLOCKQUOTE, |
| 156 | + // replacer: emptyBlockQuote |
| 157 | + // }, |
| 158 | + br: { |
| 159 | + constant: REGEXP_BR, |
| 160 | + replacer: br, |
| 161 | + }, |
| 162 | + sponsorship: { |
| 163 | + constant: REGEXP_SPONSORSHIP, |
| 164 | + replacer: sponsorship, |
| 165 | + }, |
| 166 | + memes: { |
| 167 | + constant: REGEXP_MEM, |
| 168 | + replacer: mem, |
| 169 | + }, |
| 170 | + separator: { |
| 171 | + constant: REGEXP_SEPARATOR, |
| 172 | + replacer: separator, |
| 173 | + }, |
| 174 | +}; |
| 175 | + |
| 176 | +// @TODO replace the name of this method |
| 177 | +function prepOurCallback(callbackName) { |
| 178 | + if (!callbackName) { |
| 179 | + throw new Error('name of callback is undefined or empty'); |
| 180 | + } |
| 181 | + |
| 182 | + const fromMap = map[callbackName]; |
| 183 | + |
| 184 | + return fromMap; |
30 | 185 | }
|
31 | 186 |
|
32 |
| -function blockquote(text, tmp, item) { |
33 |
| - return `${newLine}<blockquote>${item.trim()}</blockquote>`; |
| 187 | +function debuggingReplacer(name){ |
| 188 | + let namesArr = ['sponsorship']; |
| 189 | + return namesArr.includes(name) |
34 | 190 | }
|
35 | 191 |
|
36 |
| -// TODO remove unused `text` argument |
37 |
| -function previewText(text, content) { |
38 |
| - const config = { |
39 |
| - content, |
40 |
| - folder: 'body', |
41 |
| - }; |
| 192 | +function replaceMarkdown(nameOfCallback) { |
42 | 193 |
|
43 |
| - this.errors.previewText = true; |
44 | 194 |
|
45 |
| - const replaced = replaceWrapper('previewText', config, 'body'); |
| 195 | + |
| 196 | + // if (!nameOfCallback) { |
| 197 | + // throw new Error('name of callback is undefined or empty'); |
| 198 | + // } |
46 | 199 |
|
47 |
| - return replaced; |
48 |
| -} |
| 200 | + // const fromMap = map[nameOfCallback]; |
49 | 201 |
|
50 |
| -function mem(text, src, href, altText) { |
51 |
| - const config = { |
52 |
| - src: src.trim(), |
53 |
| - altText: altText.trim(), |
54 |
| - href: href.trim(), |
55 |
| - }; |
| 202 | + const fromMap = prepOurCallback(nameOfCallback); |
56 | 203 |
|
57 |
| - const replaced = replaceWrapper('image', config); |
| 204 | + |
| 205 | + // console.log(fromMap.constant); |
| 206 | + // console.log(fromMap.replacer); |
58 | 207 |
|
59 |
| - return replaced; |
60 |
| -} |
61 | 208 |
|
62 |
| -function header(text, chars, content) { |
63 |
| - const config = { |
64 |
| - content: content.trim(), |
65 |
| - }; |
| 209 | + // @TODO I dont like names fromMap & nameOfCallback & forReplacer |
| 210 | + // fromMap.replacer is a single regex value |
| 211 | + // forReplacer is a new sting that will be applied |
66 | 212 |
|
67 |
| - const titleType = ['title', 'subtitle', 'heading']; |
| 213 | + // @TODO https://github.com/LLazyEmail/markdown-to-email/issues/931 |
| 214 | + var forReplacer = ''; |
| 215 | + if (typeof fromMap.replacer === 'string'){ |
| 216 | + forReplacer = fromMap.replacer; |
| 217 | + } else { |
| 218 | + forReplacer = fromMap.replacer.bind(this); |
| 219 | + } |
68 | 220 |
|
69 |
| - const replaced = replaceWrapper(titleType[chars.length - 1], config); |
| 221 | + // very cool, but generates an error, so not so cool at all. |
| 222 | + // const forReplacer = |
| 223 | + // typeof fromMap.replacer === 'string' |
| 224 | + // ? fromMap.replacer |
| 225 | + // : fromMap.replacer.bind(this); |
70 | 226 |
|
71 |
| - const result = newLine + replaced; |
| 227 | + |
| 228 | + |
| 229 | + // console.log(fromMap.replacer); |
| 230 | + // console.log(forReplacer); |
| 231 | + // console.log('------'); |
| 232 | + |
| 233 | + |
| 234 | + console.log(nameOfCallback); |
| 235 | + |
| 236 | + |
| 237 | + if(debuggingReplacer(nameOfCallback)){ |
| 238 | + this.content = this.content.replace(fromMap.constant, forReplacer); |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | + // this.content = this.content.replace(fromMap.replacer, forReplacer); |
72 | 243 |
|
73 |
| - return result; |
74 | 244 | }
|
75 | 245 |
|
76 |
| -export { |
77 |
| - strong, |
78 |
| - link, |
79 |
| - blockquote, |
80 |
| - mem, |
81 |
| - header, |
82 |
| - italic, |
83 |
| - del, |
84 |
| - q, |
85 |
| - code, |
86 |
| - hr, |
87 |
| - empty, |
88 |
| - previewText, |
89 |
| -}; |
| 246 | +// i think this method would be broken now, because we cant play with "this." |
| 247 | +// @TODO find out if we really using this method or not? |
| 248 | +function replaceMarkdownPreviewText(regexp) { |
| 249 | + // const config = { |
| 250 | + // content: this.content.trim(), |
| 251 | + // }; |
| 252 | + |
| 253 | + /* |
| 254 | + this.previewText = replaceHTMLWrapper( |
| 255 | + "previewText", |
| 256 | + config, |
| 257 | + "body" |
| 258 | + ); |
| 259 | + console.log(this.previewText) |
| 260 | + */ |
| 261 | + |
| 262 | + // this might now working anymore |
| 263 | + this.errors.previewText = true; |
| 264 | + |
| 265 | + this.content = this.content.replace(regexp, () => this.previewText); |
| 266 | +} |
| 267 | + |
| 268 | +export { replaceMarkdown, replaceMarkdownPreviewText }; |
0 commit comments