-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
541 lines (452 loc) · 19.4 KB
/
index.html
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hugo Editor Dashboard</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.0/css/bulma.min.css">
<link href="https://cdn.jsdelivr.net/npm/@toast-ui/editor@3.2.2/dist/toastui-editor.min.css" rel="stylesheet">
<style>
/* Increase font size for editor text */
.ProseMirror {
font-size: 16px; /* Adjust the font size as needed */
}
</style>
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title">Hugo Editor Dashboard</h1>
<p class="subtitle">Customizable JS panel to create markdown with YAML frontmatter for Hugo and other headless CMS. Allows you to convert images to webp.</p>
<div class="layout">
<div>
<p class="subtitle">Step 1: Upload Images</p>
<input type="file" multiple accept="image/*" id="file-input" class="is-hidden">
<label for="file-input" class="button is-info">Upload Images</label>
</div>
<div id="previews"></div>
<!--<button id="submit-button" class="button is-primary">Submit Images</button>-->
</div>
</div>
<br>
<div class="container">
<div class="layout">
<div>
<p class="subtitle">Step 2: Upload text in Docx format</p>
<input type="file" multiple accept=".docx" id="parseWordDocxFile" class="is-hidden">
<label for="parseWordDocxFile" class="button is-info">Upload Docx</label>
</div>
<!--<button id="submit-button" class="button is-primary">Submit Images</button>-->
</div>
</div>
<br>
<!-- input webform starts -->
<div class="container">
<p class="subtitle">Step 3: Fill-in metadata</p>
<form id="yamlForm" class="box">
<div class="field">
<label class="label">Date *</label>
<div class="control">
<input type="date" id="dateInput" name="dateInput" class="input" required style="width: 33%;">
</div>
</div>
<div class="field">
<label class="label">Author *</label>
<div class="control">
<input id="author" class="input" type="text" placeholder="Enter Authors (comma separated)" required maxlength="250" style="width: 33%;">
</div>
</div>
<div class="field">
<label class="label">Rubrika *</label>
<div class="control">
<input id="rubrika" class="input" type="text" placeholder="Enter Rubrika" required maxlength="100" style="width: 33%;">
</div>
</div>
<div class="field">
<label class="label">Article Title *</label>
<div class="control">
<input id="title" class="input" type="text" placeholder="Enter Title" required maxlength="250">
</div>
</div>
<div class="field">
<label class="label">Image Credit *</label>
<div class="control">
<input id="imageCredit" class="input" type="text" placeholder="Enter Image Credits (comma separated)" required maxlength="350">
</div>
</div>
<div class="field">
<label class="label">Tags *</label>
<div class="control">
<input id="tags" class="input" type="text" placeholder="Enter Tags (comma separated)" required maxlength="250">
</div>
</div>
<div class="field">
<label class="label">Sekcija *</label>
<div class="control">
<div class="select">
<select id="sekcija" required>
<option value="clanek" selected>clanek</option>
<option value="mix">mix</option>
</select>
</div>
</div>
</div>
<div class="field">
<label class="label">Draft *</label>
<div class="control">
<div class="select">
<select id="draft" required>
<option value="false">No</option>
<option value="true" selected>Yes</option>
</select>
</div>
</div>
</div>
<div class="field">
<label class="label">Text</label>
<div class="control">
<!-- Replace the textarea with the editor element -->
<div id="editor"></div>
</div>
</div>
<div class="control">
<button type="submit" id="downloadMarkdownWithYAMLAndImages" class="button is-primary">Download Package</button>
</div>
<br>
<div class="control">
<div id="uploadMessage" class="is-size-4 has-text-weight-bold pb-6"></div>
<button type="submit" id="uploadMarkdownWithYAMLAndImages" class="button is-primary">Upload Package</button>
</div>
</form>
</div>
</section>
<!-- Load dependencies -->
<!--Toast UI editor -->
<script src="https://uicdn.toast.com/editor/latest/toastui-editor-all.min.js"></script>
<!-- Mammoth to convert Docx -->
<script src="https://cdn.jsdelivr.net/npm/mammoth@1.4.8/mammoth.browser.min.js"></script>
<!-- JSZip -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<!-- FileSaver -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<!-- Load Dashboard Script --->
<script>
// Define constants for element references
const refs = {
imagePreviews: document.querySelector('#previews'),
fileSelector: document.getElementById('file-input'),
parseWordDocxFileInput: document.getElementById('parseWordDocxFile'),
modifiedFilenames: [] // Initialize modifiedFilenames array
};
// Function to add image preview
function addImagePreview(container, imageURL, filename) {
const imageBox = document.createElement("div");
imageBox.classList.add("column", "is-one-fifth");
const imageLink = document.createElement("a");
imageLink.href = imageURL;
imageLink.download = filename;
const imgElement = document.createElement("img");
imgElement.src = imageURL;
const removeButton = createButton("X", "is-danger", () => removeImage(imageBox, filename));
const moveDownButton = createButton("Move Down", "is-primary", () => moveImageDown(container, imageBox, filename));
imageLink.appendChild(imgElement);
imageBox.appendChild(imageLink);
imageBox.appendChild(removeButton);
imageBox.appendChild(moveDownButton);
container.appendChild(imageBox);
refs.modifiedFilenames.push(filename);
}
// Function to remove image
function removeImage(imageBox, filename) {
imageBox.remove();
refs.modifiedFilenames = refs.modifiedFilenames.filter(name => name !== filename);
}
// Function to move image down
function moveImageDown(container, imageBox, filename) {
const index = refs.modifiedFilenames.indexOf(filename);
if (index < refs.modifiedFilenames.length - 1) {
[refs.modifiedFilenames[index], refs.modifiedFilenames[index + 1]] = [refs.modifiedFilenames[index + 1], refs.modifiedFilenames[index]];
container.insertBefore(imageBox, imageBox.nextElementSibling.nextElementSibling);
}
}
// Helper function to create buttons
function createButton(text, className, onClick) {
const button = document.createElement("button");
button.textContent = text;
button.classList.add("button", className, "is-small");
button.addEventListener("click", onClick);
return button;
}
// Function to process user-uploaded image file
// We resize them to desired size and convert them to webp format
function processFile(file) {
if (!file) return;
const now = new Date();
const timestamp = `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}-IMG-${Math.floor(Math.random() * 10000).toString().padStart(5, '0')}`;
const reader = new FileReader();
reader.onload = function (event) {
const imageURL = event.target.result;
const filename = `${timestamp}.webp`;
const tempImg = new Image();
tempImg.src = imageURL;
tempImg.onload = function() {
const targetWidth = 820; // This is where you set the size to which user-uploaded images are resized
const targetHeight = (targetWidth / tempImg.width) * tempImg.height;
const canvas = document.createElement('canvas');
canvas.width = targetWidth;
canvas.height = targetHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(tempImg, 0, 0, targetWidth, targetHeight);
const webPURL = canvas.toDataURL("image/webp");
addImagePreview(refs.imagePreviews, webPURL, filename);
};
};
reader.readAsDataURL(file);
}
// Function to process files
function processFiles(files) {
for (const file of files) {
processFile(file);
}
}
// Event listener for file input change
refs.fileSelector.addEventListener("change", function () {
processFiles(refs.fileSelector.files);
refs.fileSelector.value = "";
});
// Function to print filenames in YAML format
function printFilenamesYAML() {
console.log("Figures:");
refs.modifiedFilenames.forEach(filename => {
console.log(`- '${filename}'`);
});
}
// Function to handle saving text
function saveText() {
const markdown = editor.getMarkdown();
// Implement saving logic here // but do not need to edit this
}
// Function to generate YAML frontmatter
function generateYAMLFrontmatter() {
const slugify = str => {
const normalizedStr = str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); // Normalize and remove diacritics // Here we create hard-coded slug for Hugo
const slug = normalizedStr
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
const words = slug.split('-').slice(0, 5);
return words.join('-');
};
const author = document.getElementById('author').value.split(',').map(author => author.trim().replace(/['"]/g, ''));
const title = document.getElementById('title').value.replace(/['"]/g, '');
const rubrika = document.getElementById('rubrika').value.replace(/['"]/g, '');
const imageCredit = document.getElementById('imageCredit').value.replace(/['"]/g, '');
const tags = document.getElementById('tags').value.split(',').map(tag => tag.trim().replace(/['"]/g, ''));
const sekcija = document.getElementById('sekcija').value;
const draft = document.getElementById('draft').value;
const md = editor.getMarkdown();
const slug = slugify(title);
const selectedDate = new Date(document.getElementById('dateInput').value);
const dateHugo = selectedDate.toISOString().slice(0, 19);
const dateFilename = `${selectedDate.getFullYear()}-${(selectedDate.getMonth() + 1).toString().padStart(2, '0')}-${selectedDate.getDate().toString().padStart(2, '0')}`;
const randomSuffix = Math.floor(Math.random() * 10000).toString().padStart(5, '0');
const filenameMd = `${dateFilename}-TXT-${randomSuffix}.md`;
// Set your YAML frontmatter here, edit as necessary
// Figures field is a YAML sequence of uploaded image files
// Foto field is not a YAML sequence; for example, you can use it as cover image paramerter in your Hugo layout
// Edit this frontmatter templates as needed and required by your Hugo config.toml
// ${md} variable is the actual text content
const yamlFrontmatter = `---
Author:${author && author.some(Boolean) ? `\n${author.map(author => `- '${author}'`).join('\n')}` : ''}
Date: '${dateHugo}'
Rubrika:${rubrika ? ` '${rubrika}'` : ''}
Tags:${tags && tags.some(Boolean) ? `\n${tags.map(tag => `- '${tag}'`).join('\n')}` : ''}
Title: '${title}'
Slug:${slug ? ` '${slug}'` : ''}
imageCredit:${imageCredit ? ` '${imageCredit}'` : ''}
sekcija:${sekcija ? `\n- '${sekcija}'` : ''}
Datoteka:${filenameMd ? ` '${filenameMd}'` : ''}
draft:${draft ? ` '${draft}'` : ''}
Figures:${refs.modifiedFilenames.length > 0 ? `\n${refs.modifiedFilenames.map(filename => `- '${filename}'`).join('\n')}` : ''}
Foto:${refs.modifiedFilenames.length > 0 ? ` '${refs.modifiedFilenames[0]}'` : ''}
---
${md}`;
return { yamlFrontmatter, filenameMd };
}
// Function to download Markdown file with YAML frontmatter and images
// It generates a zip package with the resulting md file and frontmatter and webp images for deployment
// to save zip to user's device
// Useful for a 'half-manual minimal setup' workflow without direct integration with your Hugo production environment
function downloadMarkdownWithYAMLAndImages() {
const { yamlFrontmatter, filenameMd } = generateYAMLFrontmatter();
const filenamePaket = `${filenameMd}`;
const markdownContent = `${yamlFrontmatter}\n`;
// Create a zip file
const zip = new JSZip();
// Add Markdown file with YAML frontmatter
zip.file(filenamePaket, markdownContent);
// Add images
const getImageData = async (imgElement) => {
return new Promise((resolve, reject) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
//img.crossOrigin = 'anonymous'; // Enable CORS if images are from a different origin
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.toBlob(resolve, 'image/webp');
};
img.onerror = reject;
img.src = imgElement.src;
});
};
const imagePromises = Array.from(document.querySelectorAll('#previews img')).map(img => getImageData(img));
Promise.all(imagePromises)
.then(imageBlobs => {
imageBlobs.forEach((blob, index) => {
const filename = refs.modifiedFilenames[index];
zip.file(filename, blob, { binary: true });
});
// Generate zip content and trigger download
zip.generateAsync({ type: "blob" }).then(content => {
saveAs(content, `${filenamePaket.split('.')[0]}.zip`);
}).catch(error => {
console.error('Error generating zip content:', error);
});
})
.catch(error => {
console.error('Error fetching images:', error);
});
}
// Function to Upload Markdown File with YAML and images
// This function is used to upload content package (md and converted images) to your server
// You need to set up your back-end to receive the zip file and process it how you desire
function uploadZipToFlask(zip) {
// Create FormData object
const formData = new FormData();
const datePaket = Date.now();
formData.append('zipFile', zip, `paket-${datePaket}.zip`);
// Make a POST request to your server-side app (you will need to write and deploy it yourself)
fetch('/youruploadsendpointhere/', {
method: 'POST',
body: formData // Assuming formData contains the file data
})
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Failed to upload file');
}
})
.then(data => {
// Update the content of the <div> with the message
document.getElementById('uploadMessage').innerHTML = data.message;
})
.catch(error => {
// Handle any errors
console.error('Error:', error);
});
}
function uploadMarkdownWithYAMLAndImages() {
const { yamlFrontmatter, filenameMd } = generateYAMLFrontmatter();
const filenamePaket = `${filenameMd}`;
const markdownContent = `${yamlFrontmatter}\n`;
// Create a zip file
const zip = new JSZip();
// Add Markdown file with YAML frontmatter
zip.file(filenamePaket, markdownContent);
// Add images
const getImageData = async (imgElement) => {
return new Promise((resolve, reject) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
//img.crossOrigin = 'anonymous'; // Enable CORS if images are from a different origin
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.toBlob(resolve, 'image/webp');
};
img.onerror = reject;
img.src = imgElement.src;
});
};
const imagePromises = Array.from(document.querySelectorAll('#previews img')).map(img => getImageData(img));
Promise.all(imagePromises)
.then(imageBlobs => {
imageBlobs.forEach((blob, index) => {
const filename = refs.modifiedFilenames[index];
zip.file(filename, blob, { binary: true });
});
// Generate zip content and trigger upload
zip.generateAsync({ type: "blob" }).then(content => {
uploadZipToFlask(content); // Upload the generated zip file
}).catch(error => {
console.error('Error generating zip content:', error);
});
})
.catch(error => {
console.error('Error fetching images:', error);
});
}
// TOAST UI editor settings, you can set up the toolbar how you require, refer to TUI docs
const editor = new toastui.Editor({
el: document.querySelector('#editor'),
height: '1000px',
initialEditType: 'markdown',
previewStyle: 'vertical',
toolbarItems: [
['heading', 'bold', 'italic'],
['hr', 'quote'],
['ul', 'ol', 'task', 'indent', 'outdent'],
['table', 'link'],
]
});
// Function to parse uploaded Word document to Markdown
function parseWordDocxFile(inputElement) {
var files = inputElement.files || [];
if (!files.length) return;
var file = files[0];
console.time();
var reader = new FileReader();
reader.onloadend = function(event) {
var arrayBuffer = reader.result;
mammoth.convertToMarkdown({arrayBuffer: arrayBuffer}).then(function (resultObject) {
let markdown = resultObject.value;
// Remove unwanted escaping of punctuation marks, this is post-production of text content that was generated by mammoth.js
// Mammoth escapes some characters in resulting md content, which is not required by Hugo
markdown = markdown.replace(/\\([^\w\s])/g, '$1');
// Set the modified Markdown to the editor
editor.setMarkdown(markdown);
}).catch(function(error) {
console.error('Error converting Word document to Markdown:', error);
});
};
reader.readAsArrayBuffer(file);
}
// Event listener for Word document file input change
refs.parseWordDocxFileInput.addEventListener("change", function () {
parseWordDocxFile(this);
this.value = ""; // Clear the input field after processing the file
});
// Event listener for "Download Package" button
// This allows downloading File Package to local machine
document.getElementById('downloadMarkdownWithYAMLAndImages').addEventListener('click', function(event) {
event.preventDefault();
downloadMarkdownWithYAMLAndImages();
});
// Event listener for "Upload Package" button
// Button to start server uploads
document.getElementById('yamlForm').addEventListener('submit', function(event) {
event.preventDefault();
uploadMarkdownWithYAMLAndImages();
});
</script>
</body>
</html>