@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.
- package/LICENSE +22 -0
- package/README.md +91 -0
- package/bin/cli.js +161 -0
- package/package.json +59 -0
- package/src/converter.js +91 -0
- package/src/md-to-html.js +182 -0
- package/src/platforms/common.js +298 -0
- package/src/platforms/confluence.js +294 -0
- package/src/platforms/google-chat.js +161 -0
- package/src/platforms/google-docs.js +304 -0
- package/src/platforms/google-sheets.js +239 -0
- package/src/platforms/index.js +40 -0
- package/src/platforms/jira.js +301 -0
- package/src/platforms/slack.js +192 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// src/platforms/google-sheets.js
|
|
2
|
+
// Handles tables copied from Google Sheets
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
rules: [
|
|
6
|
+
// Google Sheets-specific rules can be added here if needed
|
|
7
|
+
],
|
|
8
|
+
sanitizer: function (doc) {
|
|
9
|
+
// ===========================================
|
|
10
|
+
// DETECTION: Only process Google Sheets content
|
|
11
|
+
// ===========================================
|
|
12
|
+
var sheetsTables = doc.querySelectorAll('table[data-sheets-root]');
|
|
13
|
+
if (sheetsTables.length === 0) {
|
|
14
|
+
return; // Not Google Sheets content, skip
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Process all Google Sheets tables
|
|
18
|
+
sheetsTables.forEach(function (table) {
|
|
19
|
+
|
|
20
|
+
// ===========================================
|
|
21
|
+
// LINE BREAK FIX: Preserve <br> in table cells via placeholder
|
|
22
|
+
// MUST happen BEFORE we manipulate the grid structure.
|
|
23
|
+
//
|
|
24
|
+
// Special handling for <br> inside links:
|
|
25
|
+
// Google Sheets puts <br> INSIDE <a> tags: <a href="...">text<br></a>
|
|
26
|
+
// This causes the <br> to appear in the link text: [text<br>](url)
|
|
27
|
+
// We move such <br> outside the link first.
|
|
28
|
+
// ===========================================
|
|
29
|
+
|
|
30
|
+
// First: move <br> from inside links to after the link
|
|
31
|
+
table.querySelectorAll('a br').forEach(function (br) {
|
|
32
|
+
var link = br.closest('a');
|
|
33
|
+
if (link && link.parentNode) {
|
|
34
|
+
// Move <br> to after the link
|
|
35
|
+
link.parentNode.insertBefore(br, link.nextSibling);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Then: replace remaining <br> in cells with placeholder
|
|
40
|
+
// Also preserve leading whitespace after <br> (indentation)
|
|
41
|
+
table.querySelectorAll('td br, th br').forEach(function (br) {
|
|
42
|
+
var next = br.nextSibling;
|
|
43
|
+
// If next sibling is a text node with leading spaces, protect them
|
|
44
|
+
if (next && next.nodeType === 3) {
|
|
45
|
+
var text = next.textContent;
|
|
46
|
+
var leadingSpaces = text.match(/^[ \t]+/);
|
|
47
|
+
if (leadingSpaces) {
|
|
48
|
+
// Replace each space with placeholder to prevent HTML normalization
|
|
49
|
+
var preserved = leadingSpaces[0].replace(/ /g, '{{GS_SPACE}}').replace(/\t/g, '{{GS_TAB}}');
|
|
50
|
+
next.textContent = preserved + text.slice(leadingSpaces[0].length);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
br.replaceWith('{{TABLE_BR}}');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Clean up cells that contain ONLY the placeholder (empty cells with just <br>)
|
|
57
|
+
// These should become truly empty
|
|
58
|
+
table.querySelectorAll('td, th').forEach(function (cell) {
|
|
59
|
+
if (cell.textContent.trim() === '{{TABLE_BR}}') {
|
|
60
|
+
cell.textContent = '';
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// ===========================================
|
|
65
|
+
// SQL/LITERAL SAFETY: Protect [ ], and ` in Sheets table cells
|
|
66
|
+
// Turndown escapes these characters aggressively (\[, \], \`) which
|
|
67
|
+
// corrupts SQL snippets copied from Sheets.
|
|
68
|
+
// ===========================================
|
|
69
|
+
table.querySelectorAll('td, th').forEach(function (cell) {
|
|
70
|
+
var cellWalker = doc.createTreeWalker(cell, NodeFilter.SHOW_TEXT, null, false);
|
|
71
|
+
var cellTextNodes = [];
|
|
72
|
+
while (cellWalker.nextNode()) {
|
|
73
|
+
cellTextNodes.push(cellWalker.currentNode);
|
|
74
|
+
}
|
|
75
|
+
cellTextNodes.forEach(function (textNode) {
|
|
76
|
+
if (!textNode.textContent) return;
|
|
77
|
+
textNode.textContent = textNode.textContent
|
|
78
|
+
.replace(/\[/g, '{{GS_LBRACK}}')
|
|
79
|
+
.replace(/\]/g, '{{GS_RBRACK}}')
|
|
80
|
+
.replace(/`/g, '{{GS_BKTICK}}');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// ===========================================
|
|
85
|
+
// PIPE ESCAPE: Replace | in link text with placeholder
|
|
86
|
+
// Markdown tables use | as column separator. If link text contains |,
|
|
87
|
+
// it breaks the table structure: [text | more](url) splits the cell.
|
|
88
|
+
// We use a placeholder that converter.js will convert to \| in post-processing.
|
|
89
|
+
// ===========================================
|
|
90
|
+
table.querySelectorAll('a').forEach(function (link) {
|
|
91
|
+
// Get all text nodes inside the link and replace pipes with placeholder
|
|
92
|
+
var walker = doc.createTreeWalker(link, NodeFilter.SHOW_TEXT, null, false);
|
|
93
|
+
var textNodes = [];
|
|
94
|
+
while (walker.nextNode()) {
|
|
95
|
+
textNodes.push(walker.currentNode);
|
|
96
|
+
}
|
|
97
|
+
textNodes.forEach(function (textNode) {
|
|
98
|
+
if (textNode.textContent.indexOf('|') !== -1) {
|
|
99
|
+
textNode.textContent = textNode.textContent.replace(/\|/g, '{{PIPE}}');
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// ===========================================
|
|
105
|
+
// UNWRAP DIVS: Google Sheets wraps cell content in <span><div>...</div></span>
|
|
106
|
+
// This causes unwanted line breaks in Markdown output.
|
|
107
|
+
// ===========================================
|
|
108
|
+
table.querySelectorAll('td div, th div').forEach(function (div) {
|
|
109
|
+
// Replace div with its text content
|
|
110
|
+
div.replaceWith(div.textContent);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// ===========================================
|
|
114
|
+
// INLINE STYLE CONVERSION: Bold and Italic spans → semantic tags
|
|
115
|
+
// Google Sheets uses inline styles (font-weight:bold, font-style:italic)
|
|
116
|
+
// instead of <strong>/<em> tags. Turndown only recognizes semantic tags.
|
|
117
|
+
// ===========================================
|
|
118
|
+
table.querySelectorAll('span[style]').forEach(function (span) {
|
|
119
|
+
var style = span.getAttribute('style') || '';
|
|
120
|
+
var isBold = /font-weight\s*:\s*bold/i.test(style) || /font-weight\s*:\s*700/i.test(style);
|
|
121
|
+
var isItalic = /font-style\s*:\s*italic/i.test(style);
|
|
122
|
+
|
|
123
|
+
if (isBold && isItalic) {
|
|
124
|
+
// Bold + Italic: wrap in <strong><em>
|
|
125
|
+
var strong = doc.createElement('strong');
|
|
126
|
+
var em = doc.createElement('em');
|
|
127
|
+
while (span.firstChild) {
|
|
128
|
+
em.appendChild(span.firstChild);
|
|
129
|
+
}
|
|
130
|
+
strong.appendChild(em);
|
|
131
|
+
span.replaceWith(strong);
|
|
132
|
+
} else if (isBold) {
|
|
133
|
+
// Bold only: convert span to <strong>
|
|
134
|
+
var strongEl = doc.createElement('strong');
|
|
135
|
+
while (span.firstChild) {
|
|
136
|
+
strongEl.appendChild(span.firstChild);
|
|
137
|
+
}
|
|
138
|
+
span.replaceWith(strongEl);
|
|
139
|
+
} else if (isItalic) {
|
|
140
|
+
// Italic only: convert span to <em>
|
|
141
|
+
var emEl = doc.createElement('em');
|
|
142
|
+
while (span.firstChild) {
|
|
143
|
+
emEl.appendChild(span.firstChild);
|
|
144
|
+
}
|
|
145
|
+
span.replaceWith(emEl);
|
|
146
|
+
}
|
|
147
|
+
// Non-bold/italic spans are left as-is (will be handled by Turndown)
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// ===========================================
|
|
151
|
+
// ROWSPAN/COLSPAN FLATTENING
|
|
152
|
+
//
|
|
153
|
+
// Strategy: Treat each cell as ONE logical column regardless of colspan.
|
|
154
|
+
// For rowspan, inject empty placeholder cells in subsequent rows.
|
|
155
|
+
//
|
|
156
|
+
// This collapses a "7-physical-column" table with colspans into
|
|
157
|
+
// a "5-logical-column" table that matches the visual appearance.
|
|
158
|
+
// ===========================================
|
|
159
|
+
|
|
160
|
+
var rows = Array.prototype.slice.call(table.querySelectorAll('tr'));
|
|
161
|
+
|
|
162
|
+
// First, determine logical column count from header row
|
|
163
|
+
// (count cells, not physical columns)
|
|
164
|
+
var headerRow = rows[0];
|
|
165
|
+
var headerCells = headerRow ? Array.prototype.slice.call(headerRow.querySelectorAll('td, th')) : [];
|
|
166
|
+
var logicalColCount = headerCells.length;
|
|
167
|
+
|
|
168
|
+
// Build a rowspan tracker: for each logical column, track how many rows it spans
|
|
169
|
+
// rowspanTracker[colIndex] = number of remaining rows this cell spans
|
|
170
|
+
var rowspanTracker = [];
|
|
171
|
+
for (var i = 0; i < logicalColCount; i++) {
|
|
172
|
+
rowspanTracker.push(0);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Process each row
|
|
176
|
+
rows.forEach(function (row, rowIndex) {
|
|
177
|
+
var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
|
|
178
|
+
var newCells = [];
|
|
179
|
+
var cellIndex = 0;
|
|
180
|
+
|
|
181
|
+
for (var col = 0; col < logicalColCount; col++) {
|
|
182
|
+
if (rowspanTracker[col] > 0) {
|
|
183
|
+
// This column is still covered by a rowspan from above
|
|
184
|
+
// Insert empty placeholder
|
|
185
|
+
var placeholder = doc.createElement(rowIndex === 0 ? 'th' : 'td');
|
|
186
|
+
newCells.push(placeholder);
|
|
187
|
+
rowspanTracker[col]--;
|
|
188
|
+
} else if (cellIndex < cells.length) {
|
|
189
|
+
// Use the next available cell
|
|
190
|
+
var cell = cells[cellIndex];
|
|
191
|
+
var rowspan = parseInt(cell.getAttribute('rowspan')) || 1;
|
|
192
|
+
|
|
193
|
+
// Track rowspan for subsequent rows
|
|
194
|
+
if (rowspan > 1) {
|
|
195
|
+
rowspanTracker[col] = rowspan - 1;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Create clean cell with content
|
|
199
|
+
var newCell = doc.createElement(rowIndex === 0 ? 'th' : 'td');
|
|
200
|
+
newCell.innerHTML = cell.innerHTML;
|
|
201
|
+
newCells.push(newCell);
|
|
202
|
+
cellIndex++;
|
|
203
|
+
} else {
|
|
204
|
+
// No more cells, fill with empty
|
|
205
|
+
var emptyCell = doc.createElement(rowIndex === 0 ? 'th' : 'td');
|
|
206
|
+
newCells.push(emptyCell);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Replace row contents
|
|
211
|
+
while (row.firstChild) {
|
|
212
|
+
row.removeChild(row.firstChild);
|
|
213
|
+
}
|
|
214
|
+
newCells.forEach(function (cell) {
|
|
215
|
+
row.appendChild(cell);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// ===========================================
|
|
220
|
+
// CLEANUP: Remove Google-specific cruft
|
|
221
|
+
// ===========================================
|
|
222
|
+
table.querySelectorAll('colgroup').forEach(function (cg) { cg.remove(); });
|
|
223
|
+
table.querySelectorAll('table, tr, td, th').forEach(function (el) {
|
|
224
|
+
el.removeAttribute('style');
|
|
225
|
+
el.removeAttribute('rowspan');
|
|
226
|
+
el.removeAttribute('colspan');
|
|
227
|
+
});
|
|
228
|
+
table.removeAttribute('data-sheets-root');
|
|
229
|
+
table.removeAttribute('data-sheets-baot');
|
|
230
|
+
table.removeAttribute('cellspacing');
|
|
231
|
+
table.removeAttribute('cellpadding');
|
|
232
|
+
table.removeAttribute('dir');
|
|
233
|
+
table.removeAttribute('style');
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Remove Google's injected <style> tags
|
|
237
|
+
doc.querySelectorAll('style').forEach(function (s) { s.remove(); });
|
|
238
|
+
}
|
|
239
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/platforms/index.js
|
|
2
|
+
import jira from './jira.js';
|
|
3
|
+
import slack from './slack.js';
|
|
4
|
+
import common from './common.js';
|
|
5
|
+
import confluence from './confluence.js';
|
|
6
|
+
import googleSheets from './google-sheets.js';
|
|
7
|
+
import googleChat from './google-chat.js';
|
|
8
|
+
import googleDocs from './google-docs.js';
|
|
9
|
+
|
|
10
|
+
// Add other platforms here as they are created
|
|
11
|
+
// import notion from './notion.js';
|
|
12
|
+
|
|
13
|
+
const allPlatforms = [
|
|
14
|
+
googleSheets, // Must run early to flatten colspan before GFM table processing
|
|
15
|
+
googleDocs, // Must run before common to replace styled spans with semantic elements
|
|
16
|
+
confluence, // Must run before jira to clean up wrapper divs first
|
|
17
|
+
jira,
|
|
18
|
+
slack,
|
|
19
|
+
googleChat,
|
|
20
|
+
common,
|
|
21
|
+
// notion,
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export function addAllRules(turndownService) {
|
|
25
|
+
allPlatforms.forEach(platform => {
|
|
26
|
+
if (platform.rules) {
|
|
27
|
+
platform.rules.forEach(rule => {
|
|
28
|
+
turndownService.addRule(rule.name, rule);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function sanitize(doc) {
|
|
35
|
+
allPlatforms.forEach(platform => {
|
|
36
|
+
if (platform.sanitizer) {
|
|
37
|
+
platform.sanitizer(doc);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
rules: [
|
|
3
|
+
// ===========================================
|
|
4
|
+
// JIRA: Task list items (action items with checkboxes)
|
|
5
|
+
// Converts <div data-task-state="TODO/DONE"> to markdown task list
|
|
6
|
+
// ===========================================
|
|
7
|
+
{
|
|
8
|
+
name: 'jiraTaskItem',
|
|
9
|
+
filter: function (node) {
|
|
10
|
+
return node.nodeName === 'DIV' && node.hasAttribute('data-task-state');
|
|
11
|
+
},
|
|
12
|
+
replacement: function (content, node) {
|
|
13
|
+
var state = node.getAttribute('data-task-state');
|
|
14
|
+
var checkbox = state === 'DONE' ? '[x]' : '[ ]';
|
|
15
|
+
// Content already has inline elements converted by Turndown
|
|
16
|
+
return '- ' + checkbox + ' ' + content.trim() + '\n';
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
// ===========================================
|
|
20
|
+
// JIRA: Task list container — just pass through children
|
|
21
|
+
// ===========================================
|
|
22
|
+
{
|
|
23
|
+
name: 'jiraTaskList',
|
|
24
|
+
filter: function (node) {
|
|
25
|
+
return node.nodeName === 'DIV' && node.hasAttribute('data-task-list-local-id');
|
|
26
|
+
},
|
|
27
|
+
replacement: function (content) {
|
|
28
|
+
return '\n' + content + '\n';
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
// ===========================================
|
|
33
|
+
// JIRA: Comment thread separator — === Thread X ===
|
|
34
|
+
// Returns raw text to bypass Turndown's escape pass (which would produce \=== ...)
|
|
35
|
+
// ===========================================
|
|
36
|
+
name: 'jiraThreadSeparator',
|
|
37
|
+
filter: function (node) {
|
|
38
|
+
return node.nodeName === 'P' && node.hasAttribute('data-jira-thread');
|
|
39
|
+
},
|
|
40
|
+
replacement: function (content, node) {
|
|
41
|
+
return '\n\n' + node.getAttribute('data-jira-thread') + '\n\n';
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
// ===========================================
|
|
46
|
+
// JIRA: Comment annotation — *[ID ↩ parent] Author - Date (edited)*
|
|
47
|
+
// Returns raw text to bypass Turndown's escape pass (which would produce \[ID\])
|
|
48
|
+
// ===========================================
|
|
49
|
+
name: 'jiraCommentAnnotation',
|
|
50
|
+
filter: function (node) {
|
|
51
|
+
return node.nodeName === 'P' && node.hasAttribute('data-jira-annotation');
|
|
52
|
+
},
|
|
53
|
+
replacement: function (content, node) {
|
|
54
|
+
return '\n\n*' + node.getAttribute('data-jira-annotation') + '*\n\n';
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'confluenceCodeBlock',
|
|
59
|
+
filter: function (node) {
|
|
60
|
+
return node.nodeName === 'DIV' && node.classList.contains('code-block');
|
|
61
|
+
},
|
|
62
|
+
replacement: function (content, node) {
|
|
63
|
+
// Find the <code> element inside — don't use div.textContent (includes button text)
|
|
64
|
+
var codeEl = node.querySelector('code');
|
|
65
|
+
var text = '';
|
|
66
|
+
|
|
67
|
+
if (codeEl) {
|
|
68
|
+
// Clone so we can manipulate without affecting DOM
|
|
69
|
+
var clone = codeEl.cloneNode(true);
|
|
70
|
+
// Replace <br> with newlines (from our sanitizer)
|
|
71
|
+
clone.querySelectorAll('br').forEach(function(br) {
|
|
72
|
+
br.replaceWith('\n');
|
|
73
|
+
});
|
|
74
|
+
text = clone.textContent;
|
|
75
|
+
// Replace non-breaking spaces (from sanitizer) with regular spaces
|
|
76
|
+
text = text.replace(/\u00A0/g, ' ');
|
|
77
|
+
} else {
|
|
78
|
+
text = node.textContent;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Trim trailing whitespace from each line and overall
|
|
82
|
+
text = text.replace(/[ \t]+$/gm, '').trim();
|
|
83
|
+
return '\n\n```\n' + text + '\n```\n\n';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
],
|
|
87
|
+
sanitizer: function (doc) {
|
|
88
|
+
// ===========================================
|
|
89
|
+
// JIRA: Fix corrupted hrefs with markdown-style links
|
|
90
|
+
// Jira sometimes embeds [text](url) patterns inside href attributes
|
|
91
|
+
// e.g., href="[https://...](https://google.com/search?q=...)"
|
|
92
|
+
// ===========================================
|
|
93
|
+
doc.querySelectorAll('a[href]').forEach(function (a) {
|
|
94
|
+
var href = a.getAttribute('href');
|
|
95
|
+
// Match pattern: [real-url](google-redirect) — extract the REAL URL from brackets
|
|
96
|
+
// Jira corrupts hrefs like: [https://github.com/...](https://google.com/search?q=...)
|
|
97
|
+
var match = href.match(/^\[(.*?)\]\(.*?\)$/);
|
|
98
|
+
if (match) {
|
|
99
|
+
href = match[1];
|
|
100
|
+
a.setAttribute('href', href);
|
|
101
|
+
}
|
|
102
|
+
// Strip title when it duplicates href — Jira sets title=href as a tooltip,
|
|
103
|
+
// which Turndown renders as [text](url "url") with a redundant title.
|
|
104
|
+
if (a.getAttribute('title') === href) {
|
|
105
|
+
a.removeAttribute('title');
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// ===========================================
|
|
110
|
+
// JIRA: Fix inline code with embedded markdown links
|
|
111
|
+
// Same corruption pattern inside <span class="code">
|
|
112
|
+
// ===========================================
|
|
113
|
+
doc.querySelectorAll('span.code').forEach(function (span) {
|
|
114
|
+
var text = span.textContent;
|
|
115
|
+
// Match pattern: [display-text](url) — keep just the display text
|
|
116
|
+
var match = text.match(/^\[(.*?)\]\(.*?\)$/);
|
|
117
|
+
if (match) {
|
|
118
|
+
span.textContent = match[1];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// ===========================================
|
|
123
|
+
// JIRA: Fix code block line breaks
|
|
124
|
+
// ===========================================
|
|
125
|
+
doc.querySelectorAll('[data-ds--code--code-block] code').forEach(function (codeEl) {
|
|
126
|
+
// Find all line spans (Jira uses data-testid="renderer-code-block-line-N")
|
|
127
|
+
var lineSpans = codeEl.querySelectorAll('[data-testid^="renderer-code-block-line-"]');
|
|
128
|
+
|
|
129
|
+
if (lineSpans.length > 0) {
|
|
130
|
+
// Build clean text with explicit newlines
|
|
131
|
+
var lines = [];
|
|
132
|
+
lineSpans.forEach(function (span) {
|
|
133
|
+
// Get text content, excluding line numbers
|
|
134
|
+
var lineText = '';
|
|
135
|
+
span.childNodes.forEach(function (child) {
|
|
136
|
+
// Skip line number spans
|
|
137
|
+
if (child.nodeType === 1 && (child.classList.contains('linenumber') ||
|
|
138
|
+
child.classList.contains('ds-line-number') ||
|
|
139
|
+
child.hasAttribute('data-ds--line-number'))) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
lineText += child.textContent;
|
|
143
|
+
});
|
|
144
|
+
// Replace leading spaces with non-breaking spaces to survive HTML parsing
|
|
145
|
+
lineText = lineText.replace(/^ +/g, function(match) {
|
|
146
|
+
return match.split('').map(function() { return ' '; }).join('');
|
|
147
|
+
});
|
|
148
|
+
lines.push(lineText);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Use <br> to ensure newlines survive HTML serialization/parsing
|
|
152
|
+
var newHTML = lines.join('<br>');
|
|
153
|
+
codeEl.innerHTML = newHTML;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ===========================================
|
|
158
|
+
// JIRA: Remove <colgroup> elements from tables
|
|
159
|
+
// turndown-plugin-gfm's isFirstTbody() checks previousSibling — a <colgroup>
|
|
160
|
+
// before <tbody> causes it to return false, breaking GFM table conversion.
|
|
161
|
+
// <colgroup> only controls column widths which markdown can't represent anyway.
|
|
162
|
+
// ===========================================
|
|
163
|
+
doc.querySelectorAll('table colgroup').forEach(function (colgroup) {
|
|
164
|
+
colgroup.remove();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// ===========================================
|
|
168
|
+
// JIRA: Remove comment footer sections
|
|
169
|
+
// Footers contain action buttons and emoji reactions (e.g., <ul><li><br></li></ul>)
|
|
170
|
+
// which produce empty bullet artifacts ("-") in the markdown output.
|
|
171
|
+
// ===========================================
|
|
172
|
+
doc.querySelectorAll('[data-testid$="-footer"]').forEach(function (el) {
|
|
173
|
+
el.remove();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// ===========================================
|
|
177
|
+
// JIRA: Comment thread annotations
|
|
178
|
+
// Converts activity feed comment headers into structured italic annotations.
|
|
179
|
+
// Format: *[ID] Author · Date (edited)* and *[ID ↩ parentID] Author · Date*
|
|
180
|
+
//
|
|
181
|
+
// Handles three cases:
|
|
182
|
+
// 1. Full activity feed (comment-in-view-wrapper structure)
|
|
183
|
+
// 2. Partial copy (comment-base-item nesting only)
|
|
184
|
+
// 3. Stripped header (IDs only, no author/date)
|
|
185
|
+
//
|
|
186
|
+
// Guarded by activity-feed wrapper — ticket descriptions are untouched.
|
|
187
|
+
// ===========================================
|
|
188
|
+
if (doc.querySelector('[data-testid="issue-activity-feed.feed-display-with-intersection-observer"]')) {
|
|
189
|
+
var processedThreadIds = new Set();
|
|
190
|
+
|
|
191
|
+
// Pass 1a: === Thread X === separators — full view (comment-in-view-wrapper)
|
|
192
|
+
doc.querySelectorAll('[data-componentid^="issue-comment-base.ui.comment.comment-in-view-wrapper"]').forEach(function (wrapper) {
|
|
193
|
+
var isTopLevel = !wrapper.closest('[data-testid="issue-view-activity-comment.comment-reply-wrapper.reply-container"]');
|
|
194
|
+
if (!isTopLevel) return;
|
|
195
|
+
var match = wrapper.getAttribute('data-componentid').match(/comment-in-view-wrapper\.(\d+)/);
|
|
196
|
+
if (!match || processedThreadIds.has(match[1])) return;
|
|
197
|
+
|
|
198
|
+
if (processedThreadIds.size > 0) {
|
|
199
|
+
var hr = doc.createElement('hr');
|
|
200
|
+
wrapper.parentNode.insertBefore(hr, wrapper);
|
|
201
|
+
}
|
|
202
|
+
var sep = doc.createElement('p');
|
|
203
|
+
sep.setAttribute('data-jira-thread', '=== Thread ' + match[1] + ' ===');
|
|
204
|
+
sep.textContent = '\u200B'; // prevent Turndown blank-node pruning
|
|
205
|
+
wrapper.parentNode.insertBefore(sep, wrapper);
|
|
206
|
+
processedThreadIds.add(match[1]);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// Pass 1b: === Thread X === separators — partial copy (comment-base-item nesting)
|
|
210
|
+
if (processedThreadIds.size === 0) {
|
|
211
|
+
doc.querySelectorAll('[data-testid^="comment-base-item-"]').forEach(function (item) {
|
|
212
|
+
if (item.parentNode.closest('[data-testid^="comment-base-item-"]')) return; // nested, skip
|
|
213
|
+
var match = item.getAttribute('data-testid').match(/comment-base-item-(\d+)/);
|
|
214
|
+
if (!match || processedThreadIds.has(match[1])) return;
|
|
215
|
+
|
|
216
|
+
if (processedThreadIds.size > 0) {
|
|
217
|
+
var hr = doc.createElement('hr');
|
|
218
|
+
item.parentNode.insertBefore(hr, item);
|
|
219
|
+
}
|
|
220
|
+
var sep = doc.createElement('p');
|
|
221
|
+
sep.setAttribute('data-jira-thread', '=== Thread ' + match[1] + ' ===');
|
|
222
|
+
sep.textContent = '\u200B'; // prevent Turndown blank-node pruning
|
|
223
|
+
item.parentNode.insertBefore(sep, item);
|
|
224
|
+
processedThreadIds.add(match[1]);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Pass 2: Replace each comment header with italic annotation
|
|
229
|
+
doc.querySelectorAll('[data-testid*="ak-comment."][data-testid$="-header"]').forEach(function (headerEl) {
|
|
230
|
+
var idMatch = headerEl.getAttribute('data-testid').match(/ak-comment\.(\d+)-header/);
|
|
231
|
+
if (!idMatch) return;
|
|
232
|
+
var commentId = idMatch[1];
|
|
233
|
+
|
|
234
|
+
// Author — use aria-label for robustness across Jira versions
|
|
235
|
+
var authorEl = headerEl.querySelector('[aria-label^="More information about"]');
|
|
236
|
+
var author = authorEl
|
|
237
|
+
? authorEl.getAttribute('aria-label').replace('More information about ', '')
|
|
238
|
+
: '';
|
|
239
|
+
|
|
240
|
+
// Date — the span with aria-hidden="false" and role="presentation"
|
|
241
|
+
var dateEl = headerEl.querySelector('[role="presentation"][aria-hidden="false"]');
|
|
242
|
+
var date = dateEl ? dateEl.textContent.trim() : '';
|
|
243
|
+
|
|
244
|
+
// Edited — tooltip container is present and non-empty only when edited
|
|
245
|
+
var tooltipEl = headerEl.querySelector('[data-testid="issue-comment-base.ui.comment.ak-tool-tip--container"]');
|
|
246
|
+
var isEdited = !!(tooltipEl && tooltipEl.textContent.trim().length > 0);
|
|
247
|
+
|
|
248
|
+
// Parent ID — method 1: reply-container → enclosing wrapper (full view)
|
|
249
|
+
var parentId = null;
|
|
250
|
+
var replyContainer = headerEl.closest('[data-testid="issue-view-activity-comment.comment-reply-wrapper.reply-container"]');
|
|
251
|
+
if (replyContainer) {
|
|
252
|
+
var parentWrapper = replyContainer.parentNode.closest('[data-componentid^="issue-comment-base.ui.comment.comment-in-view-wrapper"]');
|
|
253
|
+
if (parentWrapper) {
|
|
254
|
+
var pMatch = parentWrapper.getAttribute('data-componentid').match(/comment-in-view-wrapper\.(\d+)/);
|
|
255
|
+
parentId = pMatch ? pMatch[1] : null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Parent ID — method 2: comment-base-item nesting (partial copies)
|
|
260
|
+
if (!parentId) {
|
|
261
|
+
var myItem = doc.querySelector('[data-testid="comment-base-item-' + commentId + '"]');
|
|
262
|
+
if (myItem) {
|
|
263
|
+
var parentItem = myItem.parentNode.closest('[data-testid^="comment-base-item-"]');
|
|
264
|
+
if (parentItem) {
|
|
265
|
+
var piMatch = parentItem.getAttribute('data-testid').match(/comment-base-item-(\d+)/);
|
|
266
|
+
parentId = piMatch ? piMatch[1] : null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Build annotation — use data-attr to bypass Turndown's escape pass
|
|
272
|
+
// (textContent would produce \[ID\] and \=== ... from Turndown's escape)
|
|
273
|
+
// Use '-' directly: post-processing escape() converts \u00B7 to '-' anyway
|
|
274
|
+
var idPart = '[' + commentId + (parentId ? ' ↩ ' + parentId : '') + ']';
|
|
275
|
+
var parts = [idPart];
|
|
276
|
+
if (author) parts.push(author);
|
|
277
|
+
if (date) { parts.push('-'); parts.push(date); }
|
|
278
|
+
if (isEdited) parts.push('(edited)');
|
|
279
|
+
|
|
280
|
+
var annotationEl = doc.createElement('p');
|
|
281
|
+
annotationEl.setAttribute('data-jira-annotation', parts.join(' '));
|
|
282
|
+
annotationEl.textContent = '\u200B'; // prevent Turndown blank-node pruning
|
|
283
|
+
|
|
284
|
+
headerEl.parentNode.insertBefore(annotationEl, headerEl);
|
|
285
|
+
headerEl.remove();
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ===========================================
|
|
290
|
+
// JIRA/CONFLUENCE: Fix table cell <p> tags
|
|
291
|
+
// ===========================================
|
|
292
|
+
doc.querySelectorAll('td p, th p').forEach(function (p) {
|
|
293
|
+
p.replaceWith.apply(p, Array.prototype.slice.call(p.childNodes));
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// Remove empty spans that can interfere with table parsing
|
|
297
|
+
doc.querySelectorAll('td span:empty, th span:empty').forEach(function (span) {
|
|
298
|
+
span.remove();
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
};
|