-
Notifications
You must be signed in to change notification settings - Fork 4
/
TickTickMarkdown.user.js
163 lines (137 loc) · 5.41 KB
/
TickTickMarkdown.user.js
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
// ==UserScript==
// @name TickTickMarkdown
// @namespace https://develcraft.com/
// @version 0.1.0
// @description Add markdown support to TickTick
// @author [email protected]
// @match https://ticktick.com/*
// @match https://www.ticktick.com/*
// @grant GM_addStyle
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/showdown/1.8.6/showdown.min.js
// @run-at document-body
// ==/UserScript==
(function() {
'use strict';
// CSS
const css = `
.editor-toggle {
padding-left: 10px;
}
.editor-toggle a {
color: #333;
}
.rendered-markdown {
padding-bottom: 2em;
}
.rendered-markdown * {
line-height: 1.6;
-moz-user-select: text;
-webkit-user-select: text;
}
.rendered-markdown li,
.rendered-markdown ol,
.rendered-markdown ul {
list-style: inherit;
}
.rendered-markdown ol,
.rendered-markdown ul {
padding-left: 1.2em;
padding-right: 1.2em;
padding-bottom: 1.2em;
}
.rendered-markdown li p {
padding: 0
}
.rendered-markdown p {
padding-top: 1em;
padding-bottom: 1em;
}
.rendered-markdown h3,
.rendered-markdown h4,
.rendered-markdown h5,
.rendered-markdown h6 {
color: #000;
line-height: 1.8;
padding-bottom: 0.5em;
}
.rendered-markdown pre {
font-family: monospace;
border: 1px solid #ddd;
background-color: #f4f4f4;
padding: 0.5em;
overflow: auto;
}
`;
GM_addStyle(css);
// Make sure jquery isn't conflicting with anything
const jq = $.noConflict();
// console.log('Hello, I\'m TickTickMarkdown!');
// console.log('This is Showdown:', showdown);
// console.log('This is jQuery:', jq);
// Initialize showdown (markdown renderer)
// Docs: https://github.com/showdownjs/showdown/wiki/Showdown-options
const converter = new showdown.Converter({
simplifiedAutoLink: true,
excludeTrailingPunctuationFromURLs: true,
tables: true,
strikethrough: true,
ghCodeBlocks: true,
tasklists: true,
disableForced4SpacesIndentedSublists: true,
simpleLineBreaks: true,
noHeaderId: true,
headerLevelStart: 3,
});
// Select the node that will be observed for mutations
const targetNode = jq('body').get(0);
// Options for the observer (which mutations to observe)
const observerConfig = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = function(mutationsList) {
for(const mutation of mutationsList) {
const taskContentNode = jq('#taskcontent');
var renderedMarkdownNode = jq('.rendered-markdown', taskContentNode);
const editorNode = jq('.editor-with-link', taskContentNode);
const toolbarNode = jq('#td-caption');
var editorToggleNode = jq('.editor-toggle', toolbarNode);
// Add the rendered markdown
if (editorNode.get(0) !== undefined && renderedMarkdownNode.get(0) === undefined) {
// Hide the editor
editorNode.hide();
// Show the rendered markdown node
const text = editorNode.text();
const html = converter.makeHtml(text);
renderedMarkdownNode = jq(jq.parseHTML('<div class="rendered-markdown"></div>'));
renderedMarkdownNode.html(html);
taskContentNode.append(renderedMarkdownNode);
}
// Render the editor-toggle button and register the click event
if (editorNode.get(0) !== undefined && editorToggleNode.get(0) === undefined) {
editorToggleNode = jq(jq.parseHTML('<div class="editor-toggle line-right"><a>🖉</a></div>'));
toolbarNode.prepend(editorToggleNode);
jq('.editor-toggle a').click(function(e) {
const editorNode = jq('.editor-with-link', taskContentNode);
const renderedMarkdownNode = jq('.rendered-markdown', taskContentNode);
if (editorNode.is(':visible')) {
editorNode.hide();
renderedMarkdownNode.show();
jq('a', editorToggleNode).html('🖉'); // Unicode Character 'LOWER LEFT PENCIL' (U+1F589)
// Remove the rendered markdown node to trigger its update
renderedMarkdownNode.remove();
} else {
editorNode.show();
renderedMarkdownNode.hide();
jq('a', editorToggleNode).html('👁'); // Unicode Character 'EYE' (U+1F441)
}
});
}
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, observerConfig);
// Later, you can stop observing
//observer.disconnect();
})();