-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessHtml.js
More file actions
79 lines (63 loc) · 2.84 KB
/
processHtml.js
File metadata and controls
79 lines (63 loc) · 2.84 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
const { DOMParser, XMLSerializer } = require('xmldom');
const processHtml = async (confluence_content) => {
try {
// Parse the HTML code as a DOM tree
const parser = new DOMParser();
const doc = parser.parseFromString(confluence_content, 'text/html');
// Process the HTML content...
// Get code languages used in code blocks.
const parameters = doc.getElementsByTagName('ac:parameter');
let language = null;
const languages = [];
for (let i = 0; i < parameters.length; i++) {
const parameter = parameters[i];
if (parameter.getAttribute('ac:name') === 'language') {
language = parameter.textContent;
languages.push(language);
}
};
// Get all HTML elements that match the ac:plain-text-body tag
var plainTextBodies = doc.getElementsByTagNameNS('*', 'plain-text-body');
// Iterate over all matching elements and apply the procedure to each one
for (var i = 0; i < plainTextBodies.length; i++) {
var plainTextBody = plainTextBodies[i];
// Get the entire content of the ac:plain-text-body element
var content = plainTextBody.textContent.trim();
// Add the consecutive item to the beginning of the content
content = languages[i] + '\n' + content;
// Replace new line characters with a unique string
content = content.replace(/\n/g, 'NEWLINE');
// Extract the text content within the CDATA section
var cdataMatch = content.match(/<!\[CDATA\[(.*?)\]\]>/);
if (cdataMatch) {
var cdata = cdataMatch[1];
// Remove the CDATA section from the content
content = content.replace(cdataMatch[0], cdata);
}
// Create a new text node with the modified content
var newTextNode = doc.createTextNode(content);
// Replace the existing text node with the new one
plainTextBody.replaceChild(newTextNode, plainTextBody.firstChild);
}
// Serialize the modified DOM tree back into HTML code
const serializer = new XMLSerializer();
const modifiedHtml = serializer.serializeToString(doc);
let newHtml = modifiedHtml
.replace(/<table\b[^>]*>/gi, '<table>')
.replace(/<ac:plain-text-body\b[^>]*>/gi, '<pre><code>')
.replace(/<\/ac:plain-text-body\b[^>]*>/g, '</code></pre>')
.replace(/<colgroup([\s\S]*?)<\/colgroup>/gi, '')
.replace(/ri:filename="(.*?)"[^>]*"/g, '<temp></temp')
.replace(/<\/p>/g, '')
.replace(/<p>/g, '')
.replace(/<p xmlns\=\"http:\/\/www.w3.org\/1999\/xhtml\">/g, '')
.replace(/ <\/strong>/g, '</strong> ')
.replace(/!\[(.*?)\)/g, (_, content) => '<temp>![' + content.replace(/ /g, '_') + ')</temp>')
.replace(/<ac:caption>(.*?)<\/ac:caption>/g, (_, content) => '<ac:caption>' + content.replace(/<br\/>/g, '') + '</ac:caption>')
.replace(/<ac:inline-comment-marker ac:ref=".*?">(.*?)<\/ac:inline-comment-marker>/g, '$1');
return newHtml
} catch (error) {
return error
}
};
module.exports = processHtml;