@tryghost/mg-jekyll-export 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/process-html.js +43 -47
- package/package.json +4 -3
package/lib/process-html.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {domUtils} from '@tryghost/mg-utils';
|
|
2
|
+
|
|
3
|
+
const {serializeChildren} = domUtils;
|
|
2
4
|
|
|
3
5
|
/*
|
|
4
6
|
Process the HTML of a single Jekyll Post.
|
|
@@ -7,60 +9,54 @@ Receives raw HTML, returns processed HTML
|
|
|
7
9
|
*/
|
|
8
10
|
|
|
9
11
|
export default (rawHtml, options = {}) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
return domUtils.processFragment(rawHtml, (parsed) => {
|
|
13
|
+
// Convert relative links and image paths to absolute
|
|
14
|
+
if (options.url) {
|
|
15
|
+
const urlData = new URL(options.url);
|
|
16
|
+
// The "origin" the URL truncated to the domain.
|
|
17
|
+
// The difference between the "href" matters for blogs that were hosted
|
|
18
|
+
// in sub-directories so that relative and relative-to-root URLs resolve correctly.
|
|
19
|
+
const urlOrigin = urlData.origin;
|
|
20
|
+
|
|
21
|
+
parsed.$('a').forEach((anchor) => {
|
|
22
|
+
const thisURL = anchor.getAttribute('href');
|
|
23
|
+
|
|
24
|
+
// If it starts with a slash, append the base URL
|
|
25
|
+
if (thisURL && thisURL.indexOf('/') === 0) {
|
|
26
|
+
const updatedURL = `${urlOrigin.replace(/^\/|\/$/g, '')}/${thisURL.replace(/^\/|\/$/g, '')}`;
|
|
27
|
+
anchor.setAttribute('href', updatedURL);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
parsed.$('img').forEach((img) => {
|
|
32
|
+
const thisSrc = img.getAttribute('src');
|
|
33
|
+
|
|
34
|
+
// If it starts with a slash, append the base URL
|
|
35
|
+
if (thisSrc && thisSrc.indexOf('/') === 0) {
|
|
36
|
+
const updatedURL = `${urlOrigin.replace(/^\/|\/$/g, '')}/${thisSrc.replace(/^\/|\/$/g, '')}`;
|
|
37
|
+
img.setAttribute('src', updatedURL);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
15
40
|
}
|
|
16
|
-
}, false); // This `false` is `isDocument`. If `true`, <html>, <head>, and <body> elements are introduced
|
|
17
|
-
|
|
18
|
-
// Convert relative links and image paths to absolute
|
|
19
|
-
if (options.url) {
|
|
20
|
-
const urlData = new URL(options.url);
|
|
21
|
-
// The "origin" the URL truncated to the domain.
|
|
22
|
-
// The difference between the "href" matters for blogs that were hosted
|
|
23
|
-
// in sub-directories so that relative and relative-to-root URLs resolve correctly.
|
|
24
|
-
const urlOrigin = urlData.origin;
|
|
25
41
|
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
// Unwrap <p> tags that are in <li> tags
|
|
43
|
+
parsed.$('li').forEach((li) => {
|
|
44
|
+
const p = li.querySelector('p');
|
|
28
45
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const updatedURL = `${urlOrigin.replace(/^\/|\/$/g, '')}/${thisURL.replace(/^\/|\/$/g, '')}`;
|
|
32
|
-
$html(anchor).attr('href', updatedURL);
|
|
46
|
+
if (p) {
|
|
47
|
+
li.innerHTML = serializeChildren(p);
|
|
33
48
|
}
|
|
34
49
|
});
|
|
35
50
|
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
// convert HTML back to a string
|
|
52
|
+
let html = parsed.html();
|
|
38
53
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const updatedURL = `${urlOrigin.replace(/^\/|\/$/g, '')}/${thisSrc.replace(/^\/|\/$/g, '')}`;
|
|
42
|
-
$html(img).attr('src', updatedURL);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
54
|
+
// Remove empty attributes
|
|
55
|
+
html = html.replace(/=""/g, '');
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const hasP = $html(li).find('p').length;
|
|
57
|
+
// Trim whitespace
|
|
58
|
+
html = html.trim();
|
|
50
59
|
|
|
51
|
-
|
|
52
|
-
$html(li).html($html(li).find('p').html());
|
|
53
|
-
}
|
|
60
|
+
return html;
|
|
54
61
|
});
|
|
55
|
-
|
|
56
|
-
// convert HTML back to a string
|
|
57
|
-
let html = $html.html();
|
|
58
|
-
|
|
59
|
-
// Remove empty attributes
|
|
60
|
-
html = html.replace(/=""/g, '');
|
|
61
|
-
|
|
62
|
-
// Trim whitespace
|
|
63
|
-
html = html.trim();
|
|
64
|
-
|
|
65
|
-
return html;
|
|
66
62
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-jekyll-export",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "tbc",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,11 +23,12 @@
|
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@tryghost/errors": "3.0.3",
|
|
26
|
+
"@tryghost/pretty-cli": "3.0.3",
|
|
26
27
|
"@tryghost/string": "0.3.1",
|
|
27
|
-
"cheerio": "1.2.0",
|
|
28
28
|
"front-matter": "4.0.2",
|
|
29
29
|
"markdown-it": "14.1.1",
|
|
30
|
-
"@tryghost/mg-fs-utils": "0.
|
|
30
|
+
"@tryghost/mg-fs-utils": "0.22.0",
|
|
31
|
+
"@tryghost/mg-utils": "0.7.0"
|
|
31
32
|
},
|
|
32
33
|
"scripts": {
|
|
33
34
|
"dev": "echo \"Implement me!\"",
|