@luutuankiet/clipboard2markdown 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.
@@ -0,0 +1,298 @@
1
+ // src/platforms/common.js
2
+ import pako from 'pako';
3
+
4
+ export default {
5
+ rules: [
6
+ // Superscript
7
+ {
8
+ name: 'superscript',
9
+ filter: 'sup',
10
+ replacement: function (content) {
11
+ return '^' + content + '^';
12
+ }
13
+ },
14
+
15
+ // Subscript — output plain text (no ~content~ since that's non-standard in GFM)
16
+ {
17
+ name: 'subscript',
18
+ filter: 'sub',
19
+ replacement: function (content) {
20
+ return content;
21
+ }
22
+ },
23
+
24
+ // Line breaks - simple newline
25
+ {
26
+ name: 'lineBreak',
27
+ filter: 'br',
28
+ replacement: function () {
29
+ return '\n';
30
+ }
31
+ },
32
+
33
+ // Horizontal rule
34
+ {
35
+ name: 'horizontalRule',
36
+ filter: 'hr',
37
+ replacement: function () {
38
+ return '\n\n---\n\n';
39
+ }
40
+ },
41
+
42
+ // Italic - includes CSS font-style detection (Google Docs support)
43
+ {
44
+ name: 'italicCSS',
45
+ filter: function (node) {
46
+ // Standard italic tags
47
+ var tags = ['EM', 'I', 'CITE', 'VAR'];
48
+ if (tags.indexOf(node.nodeName) !== -1) return true;
49
+
50
+ // CSS font-style="italic" (Google Docs/AI output)
51
+ if (node.style && node.style.fontStyle === 'italic') {
52
+ return true;
53
+ }
54
+
55
+ return false;
56
+ },
57
+ replacement: function (content) {
58
+ if (!content.trim()) return '';
59
+ return '*' + content + '*';
60
+ }
61
+ },
62
+
63
+ // Mermaid source blocks decoded from #pako payloads
64
+ {
65
+ name: 'mermaidSourceBlock',
66
+ filter: function (node) {
67
+ return node.nodeName === 'PRE' && node.hasAttribute('data-mermaid-source');
68
+ },
69
+ replacement: function (content, node) {
70
+ var source = node.textContent || '';
71
+ return '\n\n```mermaid\n' + source + '\n```\n\n';
72
+ }
73
+ },
74
+
75
+ // Mermaid base64 preview placeholders
76
+ {
77
+ name: 'mermaidImagePlaceholder',
78
+ filter: function (node) {
79
+ return node.nodeName === 'SPAN' && node.hasAttribute('data-mermaid-image-placeholder');
80
+ },
81
+ replacement: function (content, node) {
82
+ var alt = node.getAttribute('data-mermaid-image-placeholder') || 'mermaid diagram preview omitted';
83
+ return '![' + alt + '](mermaid-image-placeholder)';
84
+ }
85
+ },
86
+
87
+ // Fenced code blocks (PRE, with or without CODE child)
88
+ {
89
+ name: 'fencedCodeBlock',
90
+ filter: function (node) {
91
+ return node.nodeName === 'PRE';
92
+ },
93
+ replacement: function (content, node) {
94
+ var codeElement = node.querySelector('code');
95
+ var text = codeElement ? codeElement.textContent : node.textContent;
96
+ return '\n\n```\n' + text + '\n```\n\n';
97
+ }
98
+ },
99
+
100
+ // Inline code - includes span.inline-code (Google AI/Angular apps)
101
+ {
102
+ name: 'inlineCode',
103
+ filter: function (node) {
104
+ var hasSiblings = node.previousSibling || node.nextSibling;
105
+ var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
106
+
107
+ // Standard code tags
108
+ var isStandardCode = node.nodeName === 'CODE' ||
109
+ node.nodeName === 'KBD' ||
110
+ node.nodeName === 'SAMP' ||
111
+ node.nodeName === 'TT';
112
+
113
+ // Custom span.inline-code or span.code (Jira)
114
+ var isSpanCode = node.nodeName === 'SPAN' &&
115
+ (node.classList.contains('inline-code') || node.classList.contains('code'));
116
+
117
+ return (isStandardCode || isSpanCode) && !isCodeBlock;
118
+ },
119
+ replacement: function (content) {
120
+ if (!content) return '';
121
+ return '`' + content + '`';
122
+ }
123
+ },
124
+
125
+ // Links - with Slack team link handling
126
+ {
127
+ name: 'links',
128
+ filter: function (node) {
129
+ return node.nodeName === 'A' && node.getAttribute('href');
130
+ },
131
+ replacement: function (content, node) {
132
+ var url = node.getAttribute('href');
133
+ var titlePart = node.title ? ' "' + node.title + '"' : '';
134
+
135
+ if (content === url) {
136
+ return '<' + url + '>';
137
+ } else if (url === ('mailto:' + content)) {
138
+ return '<' + content + '>';
139
+ } else if (url && url.includes('slack.com/team')) {
140
+ return content;
141
+ } else {
142
+ return '[' + content + '](' + url + titlePart + ')';
143
+ }
144
+ }
145
+ },
146
+
147
+ // List items - custom prefix formatting
148
+ {
149
+ name: 'listItem',
150
+ filter: 'li',
151
+ replacement: function (content, node) {
152
+ content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ');
153
+ var prefix = '- ';
154
+ var parent = node.parentNode;
155
+
156
+ if (/ol/i.test(parent.nodeName)) {
157
+ var start = parseInt(parent.getAttribute('start') || '1', 10);
158
+ var index = Array.prototype.indexOf.call(parent.children, node) + 1;
159
+ var listNumber = start + index - 1;
160
+ prefix = listNumber + '. ';
161
+ while (prefix.length < 4) {
162
+ prefix += ' ';
163
+ }
164
+ }
165
+
166
+ return prefix + content + '\n';
167
+ }
168
+ },
169
+
170
+ // Remove buttons (e.g. Jira "Open image", "Copy to clipboard")
171
+ {
172
+ name: 'removeButtons',
173
+ filter: 'button',
174
+ replacement: function () {
175
+ return '';
176
+ }
177
+ }
178
+ ],
179
+ sanitizer: function (doc) {
180
+ var body = doc.body || doc.documentElement;
181
+ if (!body) return;
182
+ var seenMermaidPayloads = Object.create(null);
183
+
184
+ var cleanMermaidCode = function (source) {
185
+ if (!source) return null;
186
+ return source.replace(/\r\n/g, '\n').replace(/\u00a0/g, ' ').trim();
187
+ };
188
+
189
+ var normalizeBase64Url = function (payload) {
190
+ var normalized = (payload || '').replace(/-/g, '+').replace(/_/g, '/');
191
+ while (normalized.length % 4 !== 0) normalized += '=';
192
+ return normalized;
193
+ };
194
+
195
+ var decodePakoPayload = function (payload) {
196
+ try {
197
+ var binary = atob(normalizeBase64Url(payload));
198
+ var bytes = new Uint8Array(binary.length);
199
+ for (var i = 0; i < binary.length; i++) {
200
+ bytes[i] = binary.charCodeAt(i);
201
+ }
202
+
203
+ var inflated = pako.inflate(bytes, { to: 'string' });
204
+ var parsed = JSON.parse(inflated);
205
+
206
+ if (!parsed || typeof parsed.code !== 'string') {
207
+ return null;
208
+ }
209
+
210
+ return cleanMermaidCode(parsed.code);
211
+ } catch (error) {
212
+ return null;
213
+ }
214
+ };
215
+
216
+ var extractPakoPayload = function (href) {
217
+ if (!href) return null;
218
+ var match = href.match(/#pako:([^?&#]+)/);
219
+ return match ? match[1] : null;
220
+ };
221
+
222
+ var pruneEmptyContainers = function (node) {
223
+ var current = node;
224
+ while (current && current !== body) {
225
+ if (current.nodeType !== 1) return;
226
+
227
+ var text = (current.textContent || '').replace(/\u200B/g, '').trim();
228
+ if (text) return;
229
+
230
+ if (current.querySelector && current.querySelector('img,pre,code,a,table,ul,ol,li,blockquote,hr')) {
231
+ return;
232
+ }
233
+
234
+ var parent = current.parentNode;
235
+ if (!parent) return;
236
+
237
+ parent.removeChild(current);
238
+ current = parent;
239
+ }
240
+ };
241
+
242
+ // Decode Mermaid pako links into source code blocks.
243
+ doc.querySelectorAll('a[href*="#pako:"]').forEach(function (link) {
244
+ var payload = extractPakoPayload(link.getAttribute('href'));
245
+ if (!payload) return;
246
+
247
+ if (seenMermaidPayloads[payload]) {
248
+ var duplicateParent = link.parentNode;
249
+ var duplicateText = (link.textContent || '').trim();
250
+
251
+ if (duplicateParent) {
252
+ if (duplicateText) {
253
+ duplicateParent.replaceChild(doc.createTextNode(duplicateText), link);
254
+ } else {
255
+ duplicateParent.removeChild(link);
256
+ pruneEmptyContainers(duplicateParent);
257
+ }
258
+ }
259
+ return;
260
+ }
261
+
262
+ var source = decodePakoPayload(payload);
263
+ if (!source) return;
264
+
265
+ seenMermaidPayloads[payload] = true;
266
+
267
+ var pre = doc.createElement('pre');
268
+ pre.setAttribute('data-mermaid-source', 'true');
269
+ pre.textContent = source;
270
+
271
+ if (link.parentNode) {
272
+ link.parentNode.replaceChild(pre, link);
273
+ }
274
+ });
275
+
276
+ // Replace embedded base64 image payloads with stable placeholders.
277
+ doc.querySelectorAll('img[src^="data:image/"]').forEach(function (img) {
278
+ var src = img.getAttribute('src') || '';
279
+ if (!src) return;
280
+
281
+ var contextText = (img.getAttribute('alt') || '') + ' ' +
282
+ (img.getAttribute('title') || '') + ' ' +
283
+ (img.getAttribute('class') || '');
284
+
285
+ var label = /mermaid/i.test(contextText)
286
+ ? 'mermaid diagram preview omitted'
287
+ : 'embedded image omitted (base64 payload removed)';
288
+
289
+ var placeholder = doc.createElement('span');
290
+ placeholder.setAttribute('data-mermaid-image-placeholder', label);
291
+ placeholder.textContent = '\u200B';
292
+
293
+ if (img.parentNode) {
294
+ img.parentNode.replaceChild(placeholder, img);
295
+ }
296
+ });
297
+ }
298
+ };
@@ -0,0 +1,294 @@
1
+ // src/platforms/confluence.js
2
+
3
+ export default {
4
+ rules: [],
5
+ sanitizer: function (doc) {
6
+ // ===========================================
7
+ // CONFLUENCE: Normalize code blocks to <PRE><CODE> structure
8
+ // ===========================================
9
+ doc.querySelectorAll('.code-block, [data-ds--code--code-block]').forEach(function (codeBlock) {
10
+ // Find the <code> element inside the Confluence wrapper structure
11
+ var codeElement = codeBlock.querySelector('code');
12
+ if (!codeElement) return;
13
+
14
+ // Extract language hint if present (e.g., data-code-lang="xml")
15
+ var langSpan = codeBlock.querySelector('[data-code-lang]');
16
+ var lang = langSpan ? langSpan.getAttribute('data-code-lang') : '';
17
+
18
+ // Create standard <PRE><CODE> structure
19
+ var pre = doc.createElement('pre');
20
+ var code = doc.createElement('code');
21
+
22
+ // Preserve language as class if present (though we output generic ``` per PROJECT.md)
23
+ if (lang) {
24
+ code.className = 'language-' + lang;
25
+ }
26
+
27
+ // Transfer the actual code content
28
+ code.textContent = codeElement.textContent;
29
+ pre.appendChild(code);
30
+
31
+ // Replace the complex Confluence wrapper with clean structure
32
+ codeBlock.replaceWith(pre);
33
+ });
34
+
35
+ // ===========================================
36
+ // CONFLUENCE: Convert Smart Links (inline cards) to <a> tags
37
+ // ===========================================
38
+ doc.querySelectorAll('span[data-inline-card][data-card-url]').forEach(function (span) {
39
+ var url = span.getAttribute('data-card-url');
40
+ if (!url) return;
41
+
42
+ var title = url;
43
+ try {
44
+ if (url.includes('/wiki/')) {
45
+ var parts = url.split('/');
46
+ var pagesIdx = parts.indexOf('pages');
47
+ if (pagesIdx !== -1 && parts[pagesIdx + 2]) {
48
+ title = decodeURIComponent(parts[pagesIdx + 2].replace(/\+/g, ' '));
49
+ }
50
+ } else if (url.includes('docs.google.com/spreadsheets')) {
51
+ title = 'Google Sheet';
52
+ } else if (url.includes('docs.google.com/document')) {
53
+ title = 'Google Doc';
54
+ }
55
+ } catch (e) {}
56
+
57
+ var anchor = doc.createElement('a');
58
+ anchor.href = url;
59
+ anchor.textContent = title;
60
+ span.replaceWith(anchor);
61
+ });
62
+
63
+ // ===========================================
64
+ // CONFLUENCE: Convert emoji images to text
65
+ // ===========================================
66
+ doc.querySelectorAll('img[data-emoji-text]').forEach(function (img) {
67
+ var emojiText = img.getAttribute('data-emoji-text');
68
+ if (emojiText) {
69
+ img.replaceWith(emojiText);
70
+ }
71
+ });
72
+
73
+ // ===========================================
74
+ // CONFLUENCE: Unwrap expand containers FIRST
75
+ // ===========================================
76
+ doc.querySelectorAll('[data-node-type="nestedExpand"]').forEach(function (expand) {
77
+ var content = expand.querySelector('.css-pw7jst') || expand;
78
+ var fragment = doc.createDocumentFragment();
79
+ while (content.firstChild) {
80
+ var child = content.firstChild;
81
+ // Skip empty divs (Confluence adds positioning/styling divs)
82
+ if (child.nodeType === 1 && child.tagName === 'DIV' && !child.textContent.trim()) {
83
+ child.remove();
84
+ continue;
85
+ }
86
+ fragment.appendChild(child);
87
+ }
88
+ expand.replaceWith(fragment);
89
+ });
90
+
91
+ // ===========================================
92
+ // CONFLUENCE: Unwrap info panels (keep content)
93
+ // ===========================================
94
+ doc.querySelectorAll('.ak-editor-panel').forEach(function (panel) {
95
+ var content = panel.querySelector('.ak-editor-panel__content');
96
+ if (content) {
97
+ var fragment = doc.createDocumentFragment();
98
+ while (content.firstChild) {
99
+ fragment.appendChild(content.firstChild);
100
+ }
101
+ panel.replaceWith(fragment);
102
+ } else {
103
+ panel.remove();
104
+ }
105
+ });
106
+
107
+ // ===========================================
108
+ // CONFLUENCE: Handle colspan cells (section headers)
109
+ // ===========================================
110
+ doc.querySelectorAll('td[colspan], th[colspan]').forEach(function (cell) {
111
+ var colspan = parseInt(cell.getAttribute('colspan'), 10);
112
+ if (colspan > 1) {
113
+ var parent = cell.parentNode;
114
+ for (var i = 1; i < colspan; i++) {
115
+ var emptyCell = doc.createElement(cell.tagName.toLowerCase());
116
+ emptyCell.textContent = ' ';
117
+ cell.after(emptyCell);
118
+ }
119
+ cell.removeAttribute('colspan');
120
+ }
121
+ });
122
+
123
+ // ===========================================
124
+ // CONFLUENCE: Clean up table header cells
125
+ // ===========================================
126
+ doc.querySelectorAll('th').forEach(function (th) {
127
+ // Remove sorting icon figures
128
+ th.querySelectorAll('figure').forEach(function (figure) {
129
+ figure.remove();
130
+ });
131
+
132
+ // Collect all paragraph content and join with <br>
133
+ var paragraphs = th.querySelectorAll('p');
134
+ if (paragraphs.length > 0) {
135
+ var combined = doc.createDocumentFragment();
136
+ paragraphs.forEach(function (p, index) {
137
+ if (index > 0) {
138
+ var br = doc.createElement('span');
139
+ br.textContent = '{{TABLE_BR}}';
140
+ combined.appendChild(br);
141
+ }
142
+ while (p.firstChild) {
143
+ combined.appendChild(p.firstChild);
144
+ }
145
+ });
146
+ th.innerHTML = '';
147
+ th.appendChild(combined);
148
+ } else {
149
+ th.querySelectorAll('div').forEach(function (div) {
150
+ while (div.firstChild) {
151
+ div.parentNode.insertBefore(div.firstChild, div);
152
+ }
153
+ div.remove();
154
+ });
155
+ }
156
+ });
157
+
158
+ // ===========================================
159
+ // CONFLUENCE: Flatten lists inside table cells
160
+ // ===========================================
161
+ doc.querySelectorAll('td ul, td ol').forEach(function (list) {
162
+ var items = Array.prototype.slice.call(list.children).filter(function(el) {
163
+ return el.tagName === 'LI';
164
+ });
165
+ var fragment = doc.createDocumentFragment();
166
+
167
+ items.forEach(function (li, index) {
168
+ if (index > 0) {
169
+ var br = doc.createElement('span');
170
+ br.className = 'table-br-marker';
171
+ br.textContent = '{{TABLE_BR}}';
172
+ fragment.appendChild(br);
173
+ }
174
+
175
+ var prefix = list.tagName === 'OL' ? (index + 1) + '. ' : '• ';
176
+ fragment.appendChild(doc.createTextNode(prefix));
177
+
178
+ while (li.firstChild) {
179
+ if (li.firstChild.nodeName === 'P') {
180
+ var p = li.firstChild;
181
+ while (p.firstChild) {
182
+ fragment.appendChild(p.firstChild);
183
+ }
184
+ p.remove();
185
+ } else if (li.firstChild.nodeName === 'UL' || li.firstChild.nodeName === 'OL') {
186
+ var nestedList = li.firstChild;
187
+ var nestedItems = Array.prototype.slice.call(nestedList.children).filter(function(el) {
188
+ return el.tagName === 'LI';
189
+ });
190
+ nestedItems.forEach(function(nestedLi) {
191
+ var nestedBr = doc.createElement('span');
192
+ nestedBr.className = 'table-br-marker';
193
+ nestedBr.textContent = '{{TABLE_BR}}';
194
+ fragment.appendChild(nestedBr);
195
+ fragment.appendChild(doc.createTextNode(' ◦ '));
196
+ while (nestedLi.firstChild) {
197
+ if (nestedLi.firstChild.nodeName === 'P') {
198
+ var np = nestedLi.firstChild;
199
+ while (np.firstChild) {
200
+ fragment.appendChild(np.firstChild);
201
+ }
202
+ np.remove();
203
+ } else {
204
+ fragment.appendChild(nestedLi.firstChild);
205
+ }
206
+ }
207
+ });
208
+ nestedList.remove();
209
+ } else {
210
+ fragment.appendChild(li.firstChild);
211
+ }
212
+ }
213
+ });
214
+
215
+ // Mark this list as processed
216
+ var wrapper = doc.createElement('span');
217
+ wrapper.className = 'flattened-list';
218
+ wrapper.appendChild(fragment);
219
+ list.replaceWith(wrapper);
220
+ });
221
+
222
+ // ===========================================
223
+ // CONFLUENCE: Flatten block elements in table cells
224
+ // ===========================================
225
+ doc.querySelectorAll('td').forEach(function (td) {
226
+ var children = Array.prototype.slice.call(td.childNodes);
227
+ var hasBlockContent = children.some(function(n) {
228
+ return n.nodeName === 'P';
229
+ });
230
+
231
+ // Skip if no P tags (lists already handled)
232
+ if (!hasBlockContent) return;
233
+
234
+ var fragment = doc.createDocumentFragment();
235
+ var isFirst = true;
236
+
237
+ children.forEach(function (child) {
238
+ if (child.nodeName === 'P') {
239
+ // Skip empty paragraphs (nbsp only)
240
+ var text = child.textContent.trim();
241
+ if (text === '' || text === '\u00A0') {
242
+ return;
243
+ }
244
+ if (!isFirst) {
245
+ var br = doc.createElement('span');
246
+ br.textContent = '{{TABLE_BR}}';
247
+ fragment.appendChild(br);
248
+ }
249
+ while (child.firstChild) {
250
+ fragment.appendChild(child.firstChild);
251
+ }
252
+ isFirst = false;
253
+ } else if (child.nodeType === 1 && child.className === 'flattened-list') {
254
+ // Already flattened list
255
+ if (!isFirst) {
256
+ var br = doc.createElement('span');
257
+ br.textContent = '{{TABLE_BR}}';
258
+ fragment.appendChild(br);
259
+ }
260
+ while (child.firstChild) {
261
+ fragment.appendChild(child.firstChild);
262
+ }
263
+ isFirst = false;
264
+ } else if (child.nodeType === 1) {
265
+ // Other elements - just append
266
+ fragment.appendChild(child.cloneNode(true));
267
+ } else if (child.nodeType === 3 && child.textContent.trim()) {
268
+ // Text nodes
269
+ fragment.appendChild(child.cloneNode());
270
+ }
271
+ });
272
+
273
+ td.innerHTML = '';
274
+ td.appendChild(fragment);
275
+ });
276
+
277
+ // ===========================================
278
+ // CONFLUENCE: Clean up single <p> in table cells
279
+ // ===========================================
280
+ doc.querySelectorAll('td > p:only-child, th > p:only-child').forEach(function (p) {
281
+ p.replaceWith.apply(p, Array.prototype.slice.call(p.childNodes));
282
+ });
283
+
284
+ // ===========================================
285
+ // CONFLUENCE: Handle empty table cells
286
+ // ===========================================
287
+ doc.querySelectorAll('td, th').forEach(function (cell) {
288
+ var text = cell.textContent.trim();
289
+ if (!text || text === '\u00A0') {
290
+ cell.textContent = ' ';
291
+ }
292
+ });
293
+ }
294
+ };