-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshare.html
More file actions
410 lines (337 loc) · 16.1 KB
/
share.html
File metadata and controls
410 lines (337 loc) · 16.1 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document on Cohesion</title>
<link rel="shortcut icon" href="cohesion/favicon.svg" type="image/x-icon" />
<!-- Primary Meta Tags -->
<meta name="title" content="Document on Cohesion">
<meta name="description" content="Read Markdown Documents without distractions.">
<meta name="keywords" content="markdown table generator, markdown table editor, online markdown table, create markdown tables, markdown table tool">
<meta name="language" content="en">
<meta name="author" content="Cohesion Team">
<meta name="robots" content="index, follow">
<!-- Open Graph -->
<meta property="og:type" content="website">
<meta property="og:url" content="https://flarom.github.io/cohesion/share">
<meta property="og:title" content="Document on Cohesion">
<meta property="og:description" content="Read Markdown Documents without distractions.">
<meta property="og:image" content="https://flarom.github.io/cohesion/cohesion/cards/shared.png">
<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://flarom.github.io/cohesion/share">
<meta property="twitter:title" content="Document on Cohesion">
<meta property="twitter:description" content="Read Markdown Documents without distractions.">
<meta property="twitter:image" content="https://flarom.github.io/cohesion/cohesion/cards/shared.png">
<!-- Canonical URL -->
<link rel="canonical" href="https://flarom.github.io/cohesion/share">
<!-- PWA meta tags -->
<meta name="application-name" content="Cohesion">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta content="#303030" name="theme-color" />
<link rel="manifest" href="/manifest.json" />
<link rel="preconnect" href="https://fonts.gstatic.com">
<link rel="stylesheet" href="style/palette.css">
<link rel="stylesheet" href="style/share.css">
<link rel="stylesheet" href="style/prompts.css">
<link rel="stylesheet" href="style/landing.css">
<link rel="stylesheet" href="style/preview.css">
<link rel="stylesheet" href="script/highlight/lib/highlight.css">
</head>
<body class="theme-dark">
<div id="intro-screen">
<div id="intro-title" translate="no">Cohesion</div>
<noscript class="prompt-overlay" style="pointer-events: all !important;">
<div class="prompt-dialog" style="max-width: 500px; padding: 10px;">
<h2>Fatal</h2>
<p>Scripts failed to load.</p>
<p>Update your browser, check settings and extensions.</p>
<a target="_blank" href="https://github.com/flarom/cohesion/issues">Report an issue</a>
</div>
</noscript>
</div>
<header>
<div>
<a href="index.html" target="_blank" style="display: inline-flex; align-items: center; color: var(--text-color); text-decoration: none;">
<img src="cohesion/favicon.svg" alt="" width="48">
<h2 translate="no" class="header-text">Cohesion</h2>
</a>
</div>
<div style="display: flex; gap: 5px;">
<button class="secondary hide-on-mobile" data-locale="dialogs.landing.header.editor" onclick="editOnCohesion()" title="Edit this file using the Cohesion Markdown editor">Edit on Cohesion</button>
<button class="secondary hide-on-mobile" data-locale="dialogs.share.header.download" onclick="downloadDocument()" title="Download a copy of this document">Download</button>
<button class="secondary hide-on-mobile" data-locale="dialogs.landing.header.documentation" onclick="window.open('https://github.com/flarom/cohesion/wiki')" title="Cohesion Documentation">Documentation</button>
<button class="secondary" onclick="changeTheme();" title="Switch theme" style="font-size: large;" translate="no"><span class="icon">routine</span></button>
<button class="secondary hide-on-pc" onclick="handleMoreMenu();" style="font-size: large;" translate="no"><span class="icon">more_vert</span></button>
</div>
</header>
<div id="preview" class="preview" style="padding-top: 100px; display: block; height: 100vh; max-height: 100vh;">
</div>
<input type="file" id="fileInput" accept=".md,.markdown,.txt,text/markdown,text/plain" style="display:none" />
<script src="script/file.js"></script>
<script src="script/settings.js"></script>
<script src="script/prompts.js"></script>
<script src="script/highlight/lib/highlight.min.js"></script>
<script src="script/resources.js"></script>
<script src="script/showdown/lib/showdown.js"></script>
<script src="script/showdown/extension/bannerImage.js"></script>
<script src="script/showdown/extension/internalLink.js"></script>
<script src="script/showdown/extension/definitionList.js"></script>
<script src="script/showdown/extension/abbreviation.js"></script>
<script src="script/showdown/extension/rubyText.js"></script>
<script src="script/showdown/extension/footnotes.js"></script>
<script src="script/showdown/extension/embed.js"></script>
<script src="script/showdown/extension/figcaption.js"></script>
<script src="script/showdown/extension/style.js"></script>
<script src="script/showdown/extension/blocks.js"></script>
<script src="script/editorTools.js"></script>
<script>
// intro
window.addEventListener("load", () => {
const title = document.getElementById("intro-title");
title.style.visibility = "visible"
const text = title.innerText.trim();
title.innerHTML = "";
[...text].forEach((letter, i) => {
const span = document.createElement("span");
span.textContent = letter;
span.style.animationDelay = `${i * 0.1}s`;
title.appendChild(span);
});
const totalTime = text.length * 200;
setTimeout(() => {
const intro = document.getElementById("intro-screen");
intro.style.opacity = "0";
setTimeout(() => intro.remove(), 1800);
}, totalTime);
});
loadFilesFromStorage();
// load settings
function loadSettings(){
const theme = Settings.getSetting("theme");
if (theme) document.body.className = theme;
const root = document.documentElement;
const editorFont = Settings.getSetting("editorFont");
const paragraphFont = Settings.getSetting("paragraphFont");
const titleFont = Settings.getSetting("titleFont");
if (editorFont) root.style.setProperty("--font-editor", editorFont);
if (paragraphFont) root.style.setProperty("--font-text", paragraphFont);
if (titleFont) root.style.setProperty("--font-title", titleFont);
}
async function handleMoreMenu() {
const selection = await promptSelect("More options", ["Edit on Cohesion", "Download", "Documentation"]);
switch (selection) {
case 0: editOnCohesion(); break;
case 1: downloadDocument; break;
case 2: window.open('https://github.com/flarom/cohesion/wiki'); break;
}
}
function changeTheme() {
let current = Settings.getSetting('theme');
if (current === 'theme-dark') {
Settings.setSetting('theme', 'theme-light');
} else {
Settings.setSetting('theme', 'theme-dark');
}
loadSettings();
}
loadSettings();
const constMarkdown = ``
let markdown = constMarkdown;
function sanitizeMarkdown(text) {
return text.replace(
/\s+on[a-z]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi,
(match, offset) => {
console.warn(
"Removed inline event:",
{
removed: match,
position: offset
}
);
return '';
}
);
}
function detectInlineEvents(text) {
return /<[^>]*\bon[a-z]+\s*=/gi.test(text);
}
function extractMetadata(markdown) {
if (!markdown) return {};
const metaMatch = markdown.match(/«««([\s\S]*?)»»»/);
let metaText = null;
if (metaMatch) {
metaText = metaMatch[1];
} else {
const yamlMatch = markdown.match(/^---\s*\n([\s\S]*?)\n---/);
if (yamlMatch) metaText = yamlMatch[1];
}
if (!metaText) return {};
const metaLines = metaText.split('\n').map(l => l.trim()).filter(Boolean);
const metadata = {};
metaLines.forEach(line => {
const parts = line.split(':');
if (parts.length >= 2) {
const key = parts.shift().trim();
const value = parts.join(':').trim();
if (key) metadata[key] = value;
}
});
return metadata;
}
async function showSecurityWarning() {
await promptMessage(
`<div style="padding: 20px;">
<h2>Security Vulnerability Detected</h2>
<p>This document contains potentially dangerous event handlers.</p>
<p>This preview was sanitized for your protection. But <strong>saving or editing this document may expose you to security risks.</strong></p>
<p>You may encounter buttons and input fields not working as intended, or unexpected behaviors.</p>
</div>`,
true
);
}
const converter = new showdown.Converter({
metadata: true,
extensions: [
'bannerImage', // banner image controled by document meta
'internalLink', // internal link support
'definitionList', // definition list support
'abbreviation', // abbreviation support
'rubyText', // ruby text support
'footnotes', // footnotes support
'embed', // embed support
'figureCaption', // figure and figcaption support
'style',
'blocks'
],
});
const preview = document.getElementById("preview");
async function loadMarkdownFromURLParams() {
const params = new URLSearchParams(window.location.search);
if (params.has('f')) {
const id = params.get('f');
fetch(`https://tempfiles.flarowom.workers.dev/${id}`)
.then(async res => {
if (!res.ok) {
markdown = `
# File unavailable
The file with ID \`${id}\` is not currently publicly available to be displayed.
You may ask who sent you this document to share it again.
---
[Report an issue](https://github.com/flarom/cohesion/issues) • [Homepage](index)`;
preview.innerHTML = converter.makeHtml(markdown);
return;
}
const text = await res.text();
if (detectInlineEvents(text)) {
markdown = sanitizeMarkdown(text);
await showSecurityWarning();
} else {
markdown = text;
}
try {
const metadata = extractMetadata(markdown);
converter.getMetadata = function() { return metadata; };
} catch (e) { }
preview.innerHTML = converter.makeHtml(markdown);
document.title = getFileTitleFromText(text) + " - Cohesion";
preview.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
})
.catch(err => {
console.error(err);
markdown = `
# Unable to connect
Please try again later.
---
[Homepage](index)`;
preview.innerHTML = converter.makeHtml(markdown);
});
return;
}
if (markdown) {
if (detectInlineEvents(markdown)) {
try {
const metadata = extractMetadata(markdown);
converter.getMetadata = function() { return metadata; };
} catch (e) { }
preview.innerHTML = converter.makeHtml(sanitizeMarkdown(markdown));
showSecurityWarning();
} else {
try {
const metadata = extractMetadata(markdown);
converter.getMetadata = function() { return metadata; };
} catch (e) { }
preview.innerHTML = converter.makeHtml(markdown);
}
} else {
markdown = `
# No document provided
There is no document ID specified in the URL.
You may ask who sent you this document to share it again, properly.
---
[Report an issue](https://github.com/flarom/cohesion/issues) • [Homepage](index)`;
preview.innerHTML = converter.makeHtml(markdown);
}
}
function editOnCohesion() {
localStorage.setItem('lastPreviewState', detectInlineEvents(markdown) ? 'false' : 'true')
const params = new URLSearchParams(window.location.search);
if (params.has('id')) {
const id = params.get('id');
if (!isNaN(id) && files[Number(id)] !== undefined) {
index = id;
localStorage.setItem('lastIndex', index);
window.open('app.html', '_self')
return;
}else {
showToast(`'${id}' is an invalid/missing file ID`, 'error');
return;
}
}
// Check for vulnerabilities before saving
if (detectInlineEvents(markdown)) {
promptConfirm('This document contains malicious event handlers. Editing may expose you to security risks. Continue?', true).then((confirmed) => {
if (confirmed) {
saveAndEdit();
}
});
} else {
saveAndEdit();
}
function saveAndEdit() {
index = 0;
localStorage.setItem('lastIndex', index);
files.unshift(markdown); // Use unsanitized markdown
saveFilesToStorage();
window.open('app.html', '_self')
}
}
function downloadDocument() {
if (detectInlineEvents(markdown)) {
promptConfirm('This document contains malicious event handlers. Downloading may expose you to security risks. Continue?', true).then((confirmed) => {
if (confirmed) {
downloadFile();
}
});
} else {
downloadFile();
}
function downloadFile() {
const blob = new Blob([markdown], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = getFileTitleFromText(markdown) + '.md';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
}
loadMarkdownFromURLParams();
</script>
</body>
</html>