@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,192 @@
|
|
|
1
|
+
// src/platforms/slack.js
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
rules: [
|
|
5
|
+
{
|
|
6
|
+
name: 'slackImages',
|
|
7
|
+
filter: function (node) {
|
|
8
|
+
return node.nodeName === 'IMG' &&
|
|
9
|
+
node.getAttribute('src') &&
|
|
10
|
+
node.getAttribute('src').includes('a.slack-edge.com');
|
|
11
|
+
},
|
|
12
|
+
replacement: function () {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'slackMessageHeader',
|
|
18
|
+
filter: function (node) {
|
|
19
|
+
return node.nodeName === 'SPAN' && node.hasAttribute('data-slack-header');
|
|
20
|
+
},
|
|
21
|
+
replacement: function (content, node) {
|
|
22
|
+
return node.getAttribute('data-slack-header');
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'slackReplyHeader',
|
|
27
|
+
filter: function (node) {
|
|
28
|
+
return node.nodeName === 'SPAN' && node.hasAttribute('data-slack-reply-header');
|
|
29
|
+
},
|
|
30
|
+
replacement: function (content, node) {
|
|
31
|
+
return '*(↩ reply)* ' + node.getAttribute('data-slack-reply-header');
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: 'slackThreadSeparator',
|
|
36
|
+
filter: function (node) {
|
|
37
|
+
return node.nodeName === 'SPAN' && node.hasAttribute('data-slack-thread-sep');
|
|
38
|
+
},
|
|
39
|
+
replacement: function (content, node) {
|
|
40
|
+
return '*[thread: ' + node.getAttribute('data-slack-thread-sep') + ']*';
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: 'slackUnfurlHeader',
|
|
45
|
+
filter: function (node) {
|
|
46
|
+
return node.nodeName === 'P' && node.hasAttribute('data-slack-unfurl');
|
|
47
|
+
},
|
|
48
|
+
replacement: function (content, node) {
|
|
49
|
+
return '\n\n*[' + node.getAttribute('data-slack-unfurl') + ']*\n\n';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
|
|
54
|
+
sanitizer: function (doc) {
|
|
55
|
+
var body = doc.body || doc.documentElement;
|
|
56
|
+
|
|
57
|
+
var separator = doc.querySelector('[id$="-Thread_separator"]');
|
|
58
|
+
if (separator) {
|
|
59
|
+
var replySpan = separator.querySelector('span');
|
|
60
|
+
if (replySpan) {
|
|
61
|
+
replySpan.setAttribute('data-slack-thread-sep', replySpan.textContent.trim());
|
|
62
|
+
replySpan.textContent = '\u200B';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
var sibling = separator.nextElementSibling;
|
|
66
|
+
while (sibling) {
|
|
67
|
+
sibling.setAttribute('data-slack-is-reply', 'true');
|
|
68
|
+
sibling = sibling.nextElementSibling;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
var walker = doc.createTreeWalker(body, 4, null, false);
|
|
73
|
+
var headerNodes = [];
|
|
74
|
+
var textNode;
|
|
75
|
+
while ((textNode = walker.nextNode())) {
|
|
76
|
+
var text = textNode.textContent;
|
|
77
|
+
if (/^.+?\s+\[.+\]$/.test(text.trim())) {
|
|
78
|
+
var next = textNode.nextSibling;
|
|
79
|
+
if (next && next.nodeName === 'BR') {
|
|
80
|
+
headerNodes.push(textNode);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
headerNodes.forEach(function (node) {
|
|
86
|
+
var headerText = node.textContent.replace(/\u00a0/g, ' ').trim();
|
|
87
|
+
var span = doc.createElement('span');
|
|
88
|
+
|
|
89
|
+
var ancestor = node.parentNode;
|
|
90
|
+
var isReply = false;
|
|
91
|
+
while (ancestor && ancestor !== body) {
|
|
92
|
+
if (ancestor.hasAttribute && ancestor.hasAttribute('data-slack-is-reply')) {
|
|
93
|
+
isReply = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
ancestor = ancestor.parentNode;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (isReply) {
|
|
100
|
+
span.setAttribute('data-slack-reply-header', headerText);
|
|
101
|
+
} else {
|
|
102
|
+
span.setAttribute('data-slack-header', headerText);
|
|
103
|
+
}
|
|
104
|
+
span.textContent = '\u200B';
|
|
105
|
+
node.parentNode.replaceChild(span, node);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
var parsePosterName = function (headerText) {
|
|
109
|
+
if (!headerText) return '';
|
|
110
|
+
var match = headerText.match(/^(.*?)\s+\[.+\]$/);
|
|
111
|
+
return (match ? match[1] : headerText).trim();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
var annotateCard = function (card, posterName) {
|
|
115
|
+
if (!card || card.querySelector('p[data-slack-unfurl]')) return;
|
|
116
|
+
|
|
117
|
+
var label = 'link preview';
|
|
118
|
+
var serviceLink = card.querySelector('.c-message_attachment_v2__attribution a[title], a[aria-haspopup][title]');
|
|
119
|
+
if (serviceLink && serviceLink.getAttribute('title')) {
|
|
120
|
+
label = serviceLink.getAttribute('title').trim() + ' unfurl';
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (posterName) {
|
|
124
|
+
label = label + ' by ' + posterName;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
var annotation = doc.createElement('p');
|
|
128
|
+
annotation.setAttribute('data-slack-unfurl', label);
|
|
129
|
+
annotation.textContent = '\u200B';
|
|
130
|
+
card.insertBefore(annotation, card.firstChild);
|
|
131
|
+
|
|
132
|
+
var border = card.querySelector('.c-message_attachment__border, .c-message_attachment_v2__border');
|
|
133
|
+
if (!border) {
|
|
134
|
+
for (var bi = 0; bi < card.children.length; bi++) {
|
|
135
|
+
var child = card.children[bi];
|
|
136
|
+
if (child.style && child.style.backgroundColor === 'rgb(38, 132, 255)') {
|
|
137
|
+
border = child;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (border) border.parentNode.removeChild(border);
|
|
143
|
+
|
|
144
|
+
var attr = card.querySelector('.c-message_attachment_v2__attribution');
|
|
145
|
+
if (!attr) {
|
|
146
|
+
var divs = card.querySelectorAll('div');
|
|
147
|
+
for (var di = 0; di < divs.length; di++) {
|
|
148
|
+
var d = divs[di];
|
|
149
|
+
if (!d.firstChild || d.firstChild.nodeType !== 3) continue;
|
|
150
|
+
if (!d.firstChild.textContent.trim().startsWith('Added by')) continue;
|
|
151
|
+
if (!d.querySelector('a')) continue;
|
|
152
|
+
attr = d;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (attr) attr.parentNode.removeChild(attr);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
doc.querySelectorAll('span[data-slack-header], span[data-slack-reply-header]').forEach(function (headerSpan) {
|
|
160
|
+
var parent = headerSpan.parentNode;
|
|
161
|
+
var headerText = headerSpan.getAttribute('data-slack-header') || headerSpan.getAttribute('data-slack-reply-header') || '';
|
|
162
|
+
var posterName = parsePosterName(headerText);
|
|
163
|
+
|
|
164
|
+
var directDivs = [];
|
|
165
|
+
for (var i = 0; i < parent.childNodes.length; i++) {
|
|
166
|
+
if (parent.childNodes[i].nodeName === 'DIV') {
|
|
167
|
+
directDivs.push(parent.childNodes[i]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
for (var j = 1; j < directDivs.length; j++) {
|
|
172
|
+
annotateCard(directDivs[j], posterName);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
var findPosterFromContext = function (node) {
|
|
177
|
+
var current = node;
|
|
178
|
+
while (current && current !== body) {
|
|
179
|
+
if (current.querySelector) {
|
|
180
|
+
var sender = current.querySelector('[data-qa="message_sender_name"], [data-qa="message_sender"], .c-message__sender_button');
|
|
181
|
+
if (sender && sender.textContent) return sender.textContent.trim();
|
|
182
|
+
}
|
|
183
|
+
current = current.parentNode;
|
|
184
|
+
}
|
|
185
|
+
return '';
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
doc.querySelectorAll('.c-message_attachment_v2').forEach(function (card) {
|
|
189
|
+
annotateCard(card, findPosterFromContext(card));
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
};
|