1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
function quoteReply(path, selectorForContentElement, textFormatting) {
const contentElement = $(selectorForContentElement).get(0);
const selectedRange = QuoteExtractor.extract(contentElement);
let formatter;
if (textFormatting === 'common_mark') {
formatter = new QuoteCommonMarkFormatter();
} else {
formatter = new QuoteTextFormatter();
}
$.ajax({
url: path,
type: 'post',
data: { quote: formatter.format(selectedRange) }
});
}
class QuoteExtractor {
static extract(targetElement) {
return new QuoteExtractor(targetElement).extract();
}
constructor(targetElement) {
this.targetElement = targetElement;
this.selection = window.getSelection();
}
extract() {
const range = this.retriveSelectedRange();
if (!range) {
return null;
}
if (!this.targetElement.contains(range.startContainer)) {
range.setStartBefore(this.targetElement);
}
if (!this.targetElement.contains(range.endContainer)) {
range.setEndAfter(this.targetElement);
}
return range;
}
retriveSelectedRange() {
if (!this.isSelected) {
return null;
}
// Retrive the first range that intersects with the target element.
// NOTE: Firefox allows to select multiple ranges in the document.
for (let i = 0; i < this.selection.rangeCount; i++) {
let range = this.selection.getRangeAt(i);
if (range.intersectsNode(this.targetElement)) {
return range;
}
}
return null;
}
get isSelected() {
return this.selection.containsNode(this.targetElement, true);
}
}
class QuoteTextFormatter {
format(selectedRange) {
if (!selectedRange) {
return null;
}
const fragment = document.createElement('div');
fragment.appendChild(selectedRange.cloneContents());
// Remove all unnecessary anchor elements
fragment.querySelectorAll('a.wiki-anchor').forEach(e => e.remove());
const html = this.adjustLineBreaks(fragment.innerHTML);
const result = document.createElement('div');
result.innerHTML = html;
// Replace continuous line breaks with a single line break and remove tab characters
return result.textContent
.trim()
.replace(/\t/g, '')
.replace(/\n+/g, "\n");
}
adjustLineBreaks(html) {
return html
.replace(/<\/(h1|h2|h3|h4|div|p|li|tr)>/g, "\n</$1>")
.replace(/<br>/g, "\n")
}
}
class QuoteCommonMarkFormatter {
format(selectedRange) {
if (!selectedRange) {
return null;
}
const htmlFragment = this.extractHtmlFragmentFrom(selectedRange);
const preparedHtml = this.prepareHtml(htmlFragment);
return this.convertHtmlToCommonMark(preparedHtml);
}
extractHtmlFragmentFrom(range) {
const fragment = document.createElement('div');
const ancestorNodeName = range.commonAncestorContainer.nodeName;
if (ancestorNodeName == 'CODE' || ancestorNodeName == '#text') {
fragment.appendChild(this.wrapPreCode(range));
} else {
fragment.appendChild(range.cloneContents());
}
return fragment;
}
// When only the content within the `<code>` element is selected,
// the HTML within the selection range does not include the `<pre><code>` element itself.
// To create a complete code block, wrap the selected content with the `<pre><code>` tags.
//
// selected contentes => <pre><code class="ruby">selected contents</code></pre>
wrapPreCode(range) {
const rangeAncestor = range.commonAncestorContainer;
let codeElement = null;
if (rangeAncestor.nodeName == 'CODE') {
codeElement = rangeAncestor;
} else {
codeElement = rangeAncestor.parentElement.closest('code');
}
if (!codeElement) {
return range.cloneContents();
}
const pre = document.createElement('pre');
const code = codeElement.cloneNode(false);
code.appendChild(range.cloneContents());
pre.appendChild(code);
return pre;
}
convertHtmlToCommonMark(html) {
const turndownService = new TurndownService({
codeBlockStyle: 'fenced',
headingStyle: 'atx'
});
turndownService.addRule('del', {
filter: ['del'],
replacement: content => `~~${content}~~`
});
turndownService.addRule('checkList', {
filter: node => {
return node.type === 'checkbox' && node.parentNode.nodeName === 'LI';
},
replacement: (content, node) => {
return node.checked ? '[x]' : '[ ]';
}
});
// Table does not maintain its original format,
// and the text within the table is displayed as it is
//
// | A | B | C |
// |---|---|---|
// | 1 | 2 | 3 |
// =>
// A B C
// 1 2 3
turndownService.addRule('table', {
filter: ['td', 'th'],
replacement: (content, node) => {
const separator = node.parentElement.lastElementChild === node ? '' : ' ';
return content + separator;
}
});
turndownService.addRule('tableHeading', {
filter: ['thead', 'tbody', 'tfoot', 'tr'],
replacement: (content, _node) => content
});
turndownService.addRule('tableRow', {
filter: ['tr'],
replacement: (content, _node) => {
return content + '\n'
}
});
return turndownService.turndown(html);
}
prepareHtml(htmlFragment) {
// Remove all anchor elements.
// <h1>Title1<a href="#Title" class="wiki-anchor">¶</a></h1> => <h1>Title1</h1>
htmlFragment.querySelectorAll('a.wiki-anchor').forEach(e => e.remove());
// Convert code highlight blocks to CommonMark format code blocks.
// <code class="ruby" data-language="ruby"> => <code class="language-ruby" data-language="ruby">
htmlFragment.querySelectorAll('code[data-language]').forEach(e => {
e.classList.replace(e.dataset['language'], 'language-' + e.dataset['language'])
});
return htmlFragment.innerHTML;
}
}
|