@tryghost/mg-jekyll-export 0.6.0 → 0.8.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/README.md CHANGED
@@ -17,7 +17,7 @@ To use this package in your own project:
17
17
 
18
18
  or
19
19
 
20
- `yarn add @tryghost/mg-jekyll-export`
20
+ `pnpm add @tryghost/mg-jekyll-export`
21
21
 
22
22
 
23
23
  ## Usage
@@ -89,26 +89,26 @@ This will process all posts from the zip file, and all authors will have an emai
89
89
 
90
90
  ## Develop
91
91
 
92
- This is a mono repository, managed with [Nx](https://nx.dev/) and yarn workspaces.
92
+ This is a mono repository, managed with [Nx](https://nx.dev/) and pnpm workspaces.
93
93
 
94
94
  Follow the instructions for the top-level repo.
95
95
  1. `git clone` this repo & `cd` into it as usual
96
- 2. Run `yarn` to install top-level dependencies.
96
+ 2. Run `pnpm install` to install top-level dependencies.
97
97
 
98
98
 
99
99
  ## Run
100
100
 
101
- To run a local development copy, `cd` into this directory, and use `yarn dev` instead of `migrate` like so:
101
+ To run a local development copy, `cd` into this directory, and use `pnpm dev` instead of `migrate` like so:
102
102
 
103
103
  ```sh
104
- yarn dev jekyll --pathToZip /path/to/site.zip
104
+ pnpm dev jekyll --pathToZip /path/to/site.zip
105
105
  ```
106
106
 
107
107
 
108
108
  ## Test
109
109
 
110
- - `yarn lint` run just eslint
111
- - `yarn test` run lint and tests
110
+ - `pnpm lint` run just eslint
111
+ - `pnpm test` run lint and tests
112
112
 
113
113
 
114
114
  # Copyright & License
@@ -1,4 +1,6 @@
1
- import * as cheerio from 'cheerio';
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
- const $html = cheerio.load(rawHtml, {
11
- xml: {
12
- xmlMode: false,
13
- decodeEntities: false,
14
- scriptingEnabled: false
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
- $html('a').each((i, anchor) => {
27
- const thisURL = $html(anchor).attr('href');
42
+ // Unwrap <p> tags that are in <li> tags
43
+ parsed.$('li').forEach((li) => {
44
+ const p = li.querySelector('p');
28
45
 
29
- // If it starts with a slash, append the base URL
30
- if (thisURL && thisURL.indexOf('/') === 0) {
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
- $html('img').each((i, img) => {
37
- const thisSrc = $html(img).attr('src');
51
+ // convert HTML back to a string
52
+ let html = parsed.html();
38
53
 
39
- // If it starts with a slash, append the base URL
40
- if (thisSrc && thisSrc.indexOf('/') === 0) {
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
- // Unwrap <p> tags that are in <li> tags
48
- $html('li').each((i, li) => {
49
- const hasP = $html(li).find('p').length;
57
+ // Trim whitespace
58
+ html = html.trim();
50
59
 
51
- if (hasP) {
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.6.0",
3
+ "version": "0.8.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.19.0"
30
+ "@tryghost/mg-utils": "0.6.0",
31
+ "@tryghost/mg-fs-utils": "0.21.0"
31
32
  },
32
33
  "scripts": {
33
34
  "dev": "echo \"Implement me!\"",