@tryghost/mg-substack 0.1.1
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/LICENSE +21 -0
- package/README.md +115 -0
- package/index.js +17 -0
- package/lib/mapper.js +158 -0
- package/lib/process.js +327 -0
- package/lib/read-zip.js +30 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2022 Ghost Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Migrate Substack `zip` Export
|
|
2
|
+
|
|
3
|
+
Converts a Substack `zip` export and generates a `zip` file you can import into a Ghost installation.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
To install the CLI, which is required for the Usage commands below:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install --global @tryghost/migrate
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
To use this package in your own project:
|
|
15
|
+
|
|
16
|
+
`npm install @tryghost/mg-substack --save`
|
|
17
|
+
|
|
18
|
+
or
|
|
19
|
+
|
|
20
|
+
`yarn add @tryghost/mg-substack`
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
To run an absolute basic Substack migration, the required command is this:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
migrate substack <path to zip file>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
It's possible to pass more options, in order to achieve a better migration file for Ghost:
|
|
32
|
+
|
|
33
|
+
- **`-V` `--verbose`**
|
|
34
|
+
- Show verbose output
|
|
35
|
+
- bool - default: `false`
|
|
36
|
+
- **`--zip`**
|
|
37
|
+
- Create a zip file
|
|
38
|
+
- bool - default: `true`
|
|
39
|
+
- **`-s` `--scrape`**
|
|
40
|
+
- Configure scraping tasks
|
|
41
|
+
- string - default: `all`
|
|
42
|
+
- Choices: `all`, `img`, `web`, `media`, `none`
|
|
43
|
+
- **`--size_limit`**
|
|
44
|
+
- number - default: `false`
|
|
45
|
+
- Media files larger than this size (defined in MB [i.e. `5`]) will be flagged as oversize
|
|
46
|
+
- **`-e` `--email`**
|
|
47
|
+
- Provide an email domain for users e.g. `person@example.com` (Is ignored if `--useMetaAuthor` is provided)
|
|
48
|
+
- bool/string - default: `false`
|
|
49
|
+
- **`-u` `--url`**
|
|
50
|
+
- Provide a URL (without trailing slash) to the hosted source site, so we can scrape data e.g. `https://example.substack.com`
|
|
51
|
+
- string - default:`https://ghost.io`
|
|
52
|
+
- **`--drafts`**
|
|
53
|
+
- Import draft posts
|
|
54
|
+
- bool - default: `true`
|
|
55
|
+
- **`--threads`**
|
|
56
|
+
- Import thread posts
|
|
57
|
+
- bool - default: `false`
|
|
58
|
+
- **`--subscribeLink`**
|
|
59
|
+
- Provide a path that existing `/subscribe` anchors will link to e.g. `/join-us` or `#/portal/signup` (`#` characters need to be escaped with a `\`)
|
|
60
|
+
- string - default: `null`
|
|
61
|
+
- **`--commentLink`**
|
|
62
|
+
- Provide a path that existing `/comments` anchors will link to e.g. `#ghost-comments-root` (`#` characters need to be escaped with a `\`)
|
|
63
|
+
- string - default: `null`
|
|
64
|
+
- **`--useMetaImage`**
|
|
65
|
+
- Use `og:image` value as the feature image
|
|
66
|
+
- bool - default: `false`
|
|
67
|
+
- **`--useMetaAuthor`**
|
|
68
|
+
- Use the author field from `ld+json` (useful for posts with multiple authors)
|
|
69
|
+
- bool - default: `false`
|
|
70
|
+
- **`--postsBefore`**
|
|
71
|
+
- Only migrate posts before and including a given date e.g. 'March 20 2018'
|
|
72
|
+
- string - default: `null`
|
|
73
|
+
- **`--postsAfter`**
|
|
74
|
+
- Only migrate posts after and including a given date e.g. 'August 16 2022'
|
|
75
|
+
- string - default: `null`
|
|
76
|
+
- **`--fallBackHTMLCard`**
|
|
77
|
+
- Fall back to convert to HTMLCard, if standard Mobiledoc convert fails
|
|
78
|
+
- bool - default: `false`
|
|
79
|
+
|
|
80
|
+
**Note**: You can combine `--postsBefore` and `--postsAfter` to migrate posts between 2 dates.
|
|
81
|
+
|
|
82
|
+
A more realistic command for a Substack migration looks like this:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
migrate substack <path to zip file> --url <URL to substack instance> --subscribeLink \#/portal/signup --useMetaImage true --useMetaAuthor true
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
## Develop
|
|
90
|
+
|
|
91
|
+
This is a mono repository, managed with [lerna](https://lerna.js.org).
|
|
92
|
+
|
|
93
|
+
Follow the instructions for the top-level repo.
|
|
94
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
95
|
+
2. Run `yarn` to install top-level dependencies.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
### Run
|
|
99
|
+
|
|
100
|
+
To run a local development copy, `cd` into this directory, and use `yarn dev` instead of `migrate` like so:
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
yarn dev substack <path to csv file>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
## Test
|
|
108
|
+
|
|
109
|
+
- `yarn lint` run just eslint
|
|
110
|
+
- `yarn test` run lint and tests
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# Copyright & License
|
|
114
|
+
|
|
115
|
+
Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const readZip = require('./lib/read-zip');
|
|
2
|
+
const csv = require('@tryghost/mg-fs-utils').csv;
|
|
3
|
+
const map = require('./lib/mapper');
|
|
4
|
+
const process = require('./lib/process');
|
|
5
|
+
|
|
6
|
+
module.exports = async (ctx) => {
|
|
7
|
+
let zipContent = readZip(ctx.options.pathToZip);
|
|
8
|
+
zipContent.meta = csv.parseString(zipContent.csv);
|
|
9
|
+
delete zipContent.csv;
|
|
10
|
+
|
|
11
|
+
const mapped = await map(zipContent, ctx.options);
|
|
12
|
+
const processed = await process(mapped, ctx);
|
|
13
|
+
|
|
14
|
+
return processed;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
module.exports.process = process;
|
package/lib/mapper.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const {formatISO, parseISO, isBefore, isAfter, add} = require('date-fns');
|
|
2
|
+
const _ = require('lodash');
|
|
3
|
+
const errors = require('@tryghost/errors');
|
|
4
|
+
const {slugify} = require('@tryghost/string');
|
|
5
|
+
|
|
6
|
+
const mapConfig = (data, {url, email, useMetaAuthor}) => {
|
|
7
|
+
const slug = data.post_id.replace(/^(?:\d{1,10}\.)(\S*)/gm, '$1');
|
|
8
|
+
|
|
9
|
+
// Get an ISO 8601 date - https://date-fns.org/docs/formatISO
|
|
10
|
+
const dateNow = formatISO(new Date());
|
|
11
|
+
|
|
12
|
+
const typeSlug = slugify(data.type);
|
|
13
|
+
const visibilitySlug = slugify(data.audience);
|
|
14
|
+
|
|
15
|
+
const mappedData = {
|
|
16
|
+
url: `${url}/p/${slug}`,
|
|
17
|
+
substackId: data.post_id,
|
|
18
|
+
substackPodcastURL: data.podcast_url || false,
|
|
19
|
+
data: {
|
|
20
|
+
slug: slug,
|
|
21
|
+
published_at: data.post_date || dateNow,
|
|
22
|
+
updated_at: data.post_date || dateNow,
|
|
23
|
+
created_at: data.post_date || dateNow,
|
|
24
|
+
title: data.title || slug,
|
|
25
|
+
custom_excerpt: data.subtitle,
|
|
26
|
+
type: 'post',
|
|
27
|
+
html: data.html || null,
|
|
28
|
+
status: data.is_published.toLowerCase() === `true` ? 'published' : 'draft',
|
|
29
|
+
visibility: data.audience === 'only_paid' ? 'paid' : data.audience === 'only_free' ? 'members' : 'public',
|
|
30
|
+
tags: [
|
|
31
|
+
{
|
|
32
|
+
url: 'migrator-added-tag',
|
|
33
|
+
data: {
|
|
34
|
+
name: '#substack'
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
url: `${url}/tag/${typeSlug}`,
|
|
39
|
+
data: {
|
|
40
|
+
name: _.startCase(typeSlug)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Add an internal tag based on the type of post
|
|
48
|
+
mappedData.data.tags.push({
|
|
49
|
+
url: `migrator-added-tag-substack-type-${typeSlug}`,
|
|
50
|
+
data: {
|
|
51
|
+
slug: `hash-substack-type-${typeSlug}`,
|
|
52
|
+
name: `#substack-type-${typeSlug}`
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Add tags based on post visibility
|
|
57
|
+
mappedData.data.tags.push({
|
|
58
|
+
url: `migrator-added-tag-substack-access-${visibilitySlug}`,
|
|
59
|
+
data: {
|
|
60
|
+
slug: `hash-substack-access-${visibilitySlug}`,
|
|
61
|
+
name: `#substack-access-${visibilitySlug}`
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (email && !useMetaAuthor) {
|
|
66
|
+
const authorSlug = email.replace(/(^[\w_-]*)(@[\w_-]*\.\w*(?:\.\w{0,2})?)/, '$1');
|
|
67
|
+
|
|
68
|
+
mappedData.data.author = {
|
|
69
|
+
url: `${url}/author/${authorSlug}`,
|
|
70
|
+
data: {
|
|
71
|
+
email: email,
|
|
72
|
+
slug: authorSlug,
|
|
73
|
+
roles: [
|
|
74
|
+
'Contributor'
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return mappedData;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert the supplied post content & meta data and filter using the given options
|
|
85
|
+
*
|
|
86
|
+
* @param {Object[]} input - The input who are responsible for the project.
|
|
87
|
+
* @param {Array} input.meta - An array if post meta objects
|
|
88
|
+
* @param {Array} input.posts - An array of post data objects, inc content
|
|
89
|
+
*
|
|
90
|
+
* @returns {Array} A singular array of post objects, in a structure compatible with the other Migrate tools
|
|
91
|
+
*/
|
|
92
|
+
module.exports = async (input, options) => {
|
|
93
|
+
const output = {
|
|
94
|
+
posts: []
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if (input.posts.length < 1) {
|
|
98
|
+
return new errors.NoContentError({message: 'Input file is empty'});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Inline the HTML we have with the CSV data
|
|
102
|
+
input.meta.map((post) => {
|
|
103
|
+
const thisPostHTML = input.posts.find(item => item.name.includes(post.post_id));
|
|
104
|
+
post.html = thisPostHTML.html;
|
|
105
|
+
return post;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Delete the posts array
|
|
109
|
+
delete input.posts;
|
|
110
|
+
|
|
111
|
+
// Reassign the CSV array to input var
|
|
112
|
+
input = input.meta;
|
|
113
|
+
|
|
114
|
+
if (!options.drafts) {
|
|
115
|
+
input = input.filter(data => data.is_published.toLowerCase() === `true`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!options.threads) {
|
|
119
|
+
input = input.filter(data => data.type.toLowerCase() !== `thread`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (options.postsBefore && options.postsAfter) {
|
|
123
|
+
const startDate = parseISO(formatISO(new Date(options.postsAfter)));
|
|
124
|
+
const endDate = add(parseISO(formatISO(new Date(options.postsBefore))), {
|
|
125
|
+
days: 1
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await input.forEach((data) => {
|
|
129
|
+
if (isAfter(parseISO(data.post_date), startDate) && isBefore(parseISO(data.post_date), endDate)) {
|
|
130
|
+
output.posts.push(mapConfig(data, options));
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
} else if (options.postsAfter) {
|
|
134
|
+
const startDate = parseISO(formatISO(new Date(options.postsAfter)));
|
|
135
|
+
|
|
136
|
+
await input.forEach((data) => {
|
|
137
|
+
if (isAfter(parseISO(data.post_date), startDate)) {
|
|
138
|
+
output.posts.push(mapConfig(data, options));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
} else if (options.postsBefore) {
|
|
142
|
+
const endDate = add(parseISO(formatISO(new Date(options.postsBefore))), {
|
|
143
|
+
days: 1
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
await input.forEach((data) => {
|
|
147
|
+
if (isBefore(parseISO(data.post_date), endDate)) {
|
|
148
|
+
output.posts.push(mapConfig(data, options));
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
await input.forEach((data) => {
|
|
153
|
+
output.posts.push(mapConfig(data, options));
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return output;
|
|
158
|
+
};
|
package/lib/process.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
const fs = require('fs').promises;
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const $ = require('cheerio');
|
|
4
|
+
const url = require('url');
|
|
5
|
+
const errors = require('@tryghost/errors');
|
|
6
|
+
const SimpleDom = require('simple-dom');
|
|
7
|
+
const audioCard = require('@tryghost/kg-default-cards/lib/cards/audio');
|
|
8
|
+
|
|
9
|
+
const getFiles = async (filePath) => {
|
|
10
|
+
let filenames = await fs.readdir(filePath);
|
|
11
|
+
|
|
12
|
+
return filenames.filter(filename => filename.match(/\.html/));
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const readContent = async (filePath) => {
|
|
16
|
+
return fs.readFile(filePath, 'utf-8');
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const readFiles = async (files, postsDir) => {
|
|
20
|
+
const postContent = {};
|
|
21
|
+
for (const file of files) {
|
|
22
|
+
const substackId = file.replace(/\.html/, '');
|
|
23
|
+
|
|
24
|
+
postContent[substackId] = await readContent(path.join(postsDir, file));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return postContent;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const getUnsizedImageName = (str) => {
|
|
31
|
+
const noSizeRegex = /(.*)(_[0-9]{1,4}x[0-9]{1,4}.[a-z]{2,4})/gmi;
|
|
32
|
+
let srcParts = str.split(/\/|%2F/);
|
|
33
|
+
let last = srcParts.slice(-1)[0];
|
|
34
|
+
let matches = noSizeRegex.exec(last);
|
|
35
|
+
|
|
36
|
+
return matches[1];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const processContent = (post, siteUrl, options) => {
|
|
40
|
+
const {substackPodcastURL, metaData} = post;
|
|
41
|
+
const responseData = metaData?.responseData || {};
|
|
42
|
+
|
|
43
|
+
let html = post.data.html;
|
|
44
|
+
|
|
45
|
+
// If there's no HTML, exit & return an empty string to avoid errors
|
|
46
|
+
if (!html) {
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// As there is HTML, pass it to Cheerio inside a `<body>` tag so we have a global wrapper to target later on
|
|
51
|
+
const $html = $.load(`<body>${html}</body>`, {
|
|
52
|
+
decodeEntities: false
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// We use the `'og:image` as the feature image. If the first item in the content is an image and is the same as the `og:image`, remove it
|
|
56
|
+
if (responseData?.og_image) {
|
|
57
|
+
let firstElement = $html('body *').first();
|
|
58
|
+
|
|
59
|
+
if (firstElement.tagName === 'img' || $(firstElement).find('img').length) {
|
|
60
|
+
let theElementItself = (firstElement.tagName === 'img') ? firstElement : $(firstElement).find('img');
|
|
61
|
+
|
|
62
|
+
let firstImgSrc = $(theElementItself).attr('src');
|
|
63
|
+
let unsizedFirstSrc = getUnsizedImageName(firstImgSrc);
|
|
64
|
+
|
|
65
|
+
let ogImgSrc = responseData.og_image;
|
|
66
|
+
let unsizedOgSrc = getUnsizedImageName(ogImgSrc);
|
|
67
|
+
|
|
68
|
+
if (unsizedFirstSrc === unsizedOgSrc) {
|
|
69
|
+
if ($(firstElement).find('figcaption').length) {
|
|
70
|
+
post.data.feature_image_caption = $(firstElement).find('figcaption').html();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
$(firstElement).remove();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// If we have a podcast URL, embed an audio card at the start of the document
|
|
79
|
+
if (substackPodcastURL) {
|
|
80
|
+
let cardOpts = {
|
|
81
|
+
env: {dom: new SimpleDom.Document()},
|
|
82
|
+
payload: {
|
|
83
|
+
src: substackPodcastURL,
|
|
84
|
+
title: post.data.title
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const buildCard = audioCard.render(cardOpts);
|
|
89
|
+
const cardHTML = buildCard.nodeValue;
|
|
90
|
+
|
|
91
|
+
$html('body').prepend(cardHTML);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
$html('div.tweet').each((i, el) => {
|
|
95
|
+
let src = $(el).children('a').attr('href');
|
|
96
|
+
let parsed = url.parse(src);
|
|
97
|
+
|
|
98
|
+
if (parsed.search) {
|
|
99
|
+
// remove possible query params
|
|
100
|
+
parsed.search = null;
|
|
101
|
+
}
|
|
102
|
+
src = url.format(parsed, {search: false});
|
|
103
|
+
|
|
104
|
+
let $figure = $('<figure class="kg-card kg-embed-card"></figure>');
|
|
105
|
+
let $blockquote = $('<blockquote class="twitter-tweet"></blockquote>');
|
|
106
|
+
let $anchor = $(`<a href="${src}"></a>`);
|
|
107
|
+
let $script = $('<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>');
|
|
108
|
+
|
|
109
|
+
$blockquote.append($anchor);
|
|
110
|
+
|
|
111
|
+
$figure.append($blockquote);
|
|
112
|
+
$figure.append($script);
|
|
113
|
+
|
|
114
|
+
$(el).replaceWith($figure);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
$html('.captioned-image-container').each((i, div) => {
|
|
118
|
+
const hasCaption = $(div).find('figcaption').length;
|
|
119
|
+
|
|
120
|
+
$(div).find('a').removeAttr('class');
|
|
121
|
+
|
|
122
|
+
$(div).find('img').removeAttr('data-attrs');
|
|
123
|
+
$(div).find('img').removeAttr('srcset');
|
|
124
|
+
$(div).find('img').removeAttr('width');
|
|
125
|
+
$(div).find('img').removeAttr('height');
|
|
126
|
+
$(div).find('img').addClass('kg-image');
|
|
127
|
+
|
|
128
|
+
$(div).find('figure').addClass('kg-card kg-image-card');
|
|
129
|
+
|
|
130
|
+
if (hasCaption) {
|
|
131
|
+
$(div).find('figure').addClass('kg-card-hascaption');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
$(div).replaceWith($(div).find('figure'));
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
$html('a > style').each((i, style) => {
|
|
138
|
+
$(style).remove();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
$html('ul, ol').each((i, list) => {
|
|
142
|
+
if ($(list).find('img, div, figure, blockquote').length) {
|
|
143
|
+
$(list).before('<!--kg-card-begin: html-->');
|
|
144
|
+
$(list).after('<!--kg-card-end: html-->');
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Remove Substack share buttons
|
|
149
|
+
$html('p.button-wrapper').each((i, button) => {
|
|
150
|
+
let shareLinks = $(button).children('a.button');
|
|
151
|
+
if (shareLinks.length === 1 && siteUrl) {
|
|
152
|
+
let shareLink = $(shareLinks).get(0);
|
|
153
|
+
let src = $(shareLink).attr('href');
|
|
154
|
+
let parsed = url.parse(src);
|
|
155
|
+
|
|
156
|
+
// If it's a share button, there's no use for it and completely remove the button
|
|
157
|
+
if (parsed.search && parsed.search.indexOf('action=share') >= 0) {
|
|
158
|
+
$(button).remove();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// If it's a gift button, there's no use for it and completely remove the button
|
|
163
|
+
if (parsed.search && parsed.search.indexOf('gift=true') >= 0) {
|
|
164
|
+
$(button).remove();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Update button elements
|
|
171
|
+
$html('p.button-wrapper').each((i, button) => {
|
|
172
|
+
let buttons = $(button).children('a.button');
|
|
173
|
+
if (buttons.length === 1 && siteUrl) {
|
|
174
|
+
let siteRegex = new RegExp(`^(?:${siteUrl}(?:\\/?)(?:p\\/)?)([a-zA-Z-_\\d]*)(?:\\/?)`, 'gi');
|
|
175
|
+
let buttonLink = $(buttons).get(0);
|
|
176
|
+
let buttonHref = $(buttonLink).attr('href');
|
|
177
|
+
let buttonText = $(buttonLink).text();
|
|
178
|
+
let parsed = url.parse(buttonHref);
|
|
179
|
+
|
|
180
|
+
// remove possible query params
|
|
181
|
+
parsed.search = null;
|
|
182
|
+
|
|
183
|
+
buttonHref = url.format(parsed, {search: false});
|
|
184
|
+
|
|
185
|
+
if (buttonHref.match(siteRegex)) {
|
|
186
|
+
buttonHref = buttonHref.replace(siteRegex, '/$1/');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (buttonHref === '/subscribe/') {
|
|
190
|
+
buttonHref = options.subscribeLink || '#/portal/signup';
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (buttonHref.endsWith('/comments')) {
|
|
194
|
+
buttonHref = options.commentLink || '#ghost-comments-root';
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
$(button).replaceWith(`<div class="kg-card kg-button-card kg-align-center"><a href="${buttonHref}" class="kg-btn kg-btn-accent">${buttonText}</a></div>`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// TODO: this should be a parser plugin
|
|
202
|
+
// Wrap nested lists in HTML card
|
|
203
|
+
$html('ul li ul, ol li ol, ol li ul, ul li ol').each((i, nestedList) => {
|
|
204
|
+
let $parent = $(nestedList).parentsUntil('ul, ol').parent();
|
|
205
|
+
$parent.before('<!--kg-card-begin: html-->');
|
|
206
|
+
$parent.after('<!--kg-card-end: html-->');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// Handle footnotes
|
|
210
|
+
let footnotesMarkup = $(`<div class="footnotes"><hr><ol></ol></div>`);
|
|
211
|
+
let footnotesCount = 0;
|
|
212
|
+
$html('.footnote').each((i, el) => {
|
|
213
|
+
let footnoteBodyAnchor = $(el).find('a').attr('href');
|
|
214
|
+
let footnoteID = $(el).attr('id');
|
|
215
|
+
let footnoteNumber = parseInt(footnoteID);
|
|
216
|
+
let footnoteContent = $(el).find('.footnote-content');
|
|
217
|
+
|
|
218
|
+
footnoteContent.find('p').last().append(` <a href="${footnoteBodyAnchor}" title="Jump back to footnote ${footnoteNumber} in the text.">↩</a>`);
|
|
219
|
+
footnotesMarkup.find('ol').append(`<li id="${footnoteID}">${footnoteContent.html()}</li>`);
|
|
220
|
+
$(el).remove();
|
|
221
|
+
|
|
222
|
+
footnotesCount = footnotesCount + 1;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
if (footnotesCount > 0) {
|
|
226
|
+
// Only append notes markup is there are footnotes
|
|
227
|
+
$html('body').append(`<!--kg-card-begin: html-->${footnotesMarkup}<!--kg-card-end: html-->`);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Wrap content that has footnote anchors in HTML tags to retain the footnote jump anchor
|
|
231
|
+
$html('p, ul, ol').each((i, el) => {
|
|
232
|
+
if ($(el).find('a.footnote-anchor').length > 0) {
|
|
233
|
+
$(el).before('<!--kg-card-begin: html-->');
|
|
234
|
+
$(el).after('<!--kg-card-end: html-->');
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// Replace any subscribe link on the same domain with a specific link
|
|
239
|
+
if (options.subscribeLink) {
|
|
240
|
+
$html('a').each((i, anchor) => {
|
|
241
|
+
let href = $(anchor).attr('href');
|
|
242
|
+
let linkRegex = new RegExp(`^(${siteUrl})?(/subscribe)(.*)`, 'gi');
|
|
243
|
+
|
|
244
|
+
let matches = href.replace(linkRegex, '$2');
|
|
245
|
+
|
|
246
|
+
if (matches === '/subscribe') {
|
|
247
|
+
$(anchor).attr('href', options.subscribeLink);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Replace any signup forms with a Portal signup button
|
|
253
|
+
if (options.subscribeLink) {
|
|
254
|
+
$html('.subscription-widget-wrap').each((i, div) => {
|
|
255
|
+
const hasForm = $(div).find('form');
|
|
256
|
+
|
|
257
|
+
if (hasForm.length) {
|
|
258
|
+
const buttonText = $(div).find('form input[type="submit"]').attr('value');
|
|
259
|
+
$(div).replaceWith(`<div class="kg-card kg-button-card kg-align-center"><a href="__GHOST_URL__/${options.subscribeLink}" class="kg-btn kg-btn-accent">${buttonText}</a></div>`);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
$html('.embedded-post-wrap').each((i, div) => {
|
|
265
|
+
let bookmarkJSON = JSON.parse($(div).attr('data-attrs').replace(/"/gm, '"'));
|
|
266
|
+
|
|
267
|
+
let bookmarkLink = bookmarkJSON.url;
|
|
268
|
+
let bookmarkPubName = bookmarkJSON.publication_name;
|
|
269
|
+
let bookmarkPubIcon = bookmarkJSON.publication_logo_url;
|
|
270
|
+
let bookmarkTitle = bookmarkJSON.title;
|
|
271
|
+
let bookmarkContent = bookmarkJSON.truncated_body_text;
|
|
272
|
+
|
|
273
|
+
let $bookmark = $('<figure class="kg-card kg-bookmark-card"></figure>');
|
|
274
|
+
let $link = $(`<a class="kg-bookmark-container" href="${bookmarkLink}"></a>`);
|
|
275
|
+
let $content = $(`<div class="kg-bookmark-content"><div class="kg-bookmark-title">${bookmarkTitle}</div><div class="kg-bookmark-description">${bookmarkContent}</div><div class="kg-bookmark-metadata"><img class="kg-bookmark-icon" src="${bookmarkPubIcon}"><span class="kg-bookmark-author">${bookmarkPubName}</span></div></div>`);
|
|
276
|
+
|
|
277
|
+
$link.append($content);
|
|
278
|
+
$bookmark.append($link);
|
|
279
|
+
|
|
280
|
+
$(div).replaceWith($bookmark);
|
|
281
|
+
$bookmark.before('<!--kg-card-begin: html-->');
|
|
282
|
+
$bookmark.after('<!--kg-card-end: html-->');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// convert HTML back to a string
|
|
286
|
+
html = $html('body').html();
|
|
287
|
+
|
|
288
|
+
// Apply our new HTML back to the post object
|
|
289
|
+
post.data.html = html.trim();
|
|
290
|
+
|
|
291
|
+
return post;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const processPost = (post, siteUrl, options) => {
|
|
295
|
+
post = processContent(post, siteUrl, options);
|
|
296
|
+
|
|
297
|
+
return post;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
module.exports = async (input, ctx) => {
|
|
301
|
+
let {postsDir, options} = ctx;
|
|
302
|
+
let {url: siteUrl} = options;
|
|
303
|
+
const output = {};
|
|
304
|
+
|
|
305
|
+
if (postsDir) {
|
|
306
|
+
try {
|
|
307
|
+
let postFiles = await getFiles(postsDir);
|
|
308
|
+
let postContent = await readFiles(postFiles, postsDir);
|
|
309
|
+
|
|
310
|
+
input.posts.map((post) => { // eslint-disable-line array-callback-return
|
|
311
|
+
post.data.html = postContent[post.substackId];
|
|
312
|
+
});
|
|
313
|
+
} catch (error) {
|
|
314
|
+
return new errors.InternalServerError({message: 'Couldn\'t read post files'});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (input.posts && input.posts.length > 0) {
|
|
319
|
+
output.posts = input.posts.map((post) => {
|
|
320
|
+
return processPost(post, siteUrl, options);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return output;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
module.exports.processContent = processContent;
|
package/lib/read-zip.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const fsUtils = require('@tryghost/mg-fs-utils');
|
|
2
|
+
|
|
3
|
+
module.exports = (zipPath) => {
|
|
4
|
+
let content = {
|
|
5
|
+
csv: null,
|
|
6
|
+
posts: []
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// We only support the current Substack export file structure:
|
|
10
|
+
// - posts.csv
|
|
11
|
+
// - posts
|
|
12
|
+
// - 1234.post-slug.html
|
|
13
|
+
// - 5678.another-slug.html
|
|
14
|
+
|
|
15
|
+
fsUtils.zip.read(zipPath, (entryName, zipEntry) => {
|
|
16
|
+
// Catch all HTML files inside `profile/`
|
|
17
|
+
if (/^posts\/.*\.html$/.test(entryName)) {
|
|
18
|
+
content.posts.push({
|
|
19
|
+
name: entryName.replace('posts/', ''),
|
|
20
|
+
html: zipEntry.getData().toString('utf8')
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Skip if not matched above, and report skipped files if `--verbose`
|
|
24
|
+
} else if (entryName === 'posts.csv') {
|
|
25
|
+
content.csv = zipEntry.getData().toString('utf8');
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return content;
|
|
30
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tryghost/mg-substack",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-substack",
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "echo \"Implement me!\"",
|
|
10
|
+
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
|
|
11
|
+
"lint": "eslint . --ext .js --cache",
|
|
12
|
+
"posttest": "yarn lint"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"mocha": "10.0.0",
|
|
23
|
+
"should": "13.2.3",
|
|
24
|
+
"sinon": "14.0.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@tryghost/errors": "1.2.14",
|
|
28
|
+
"@tryghost/kg-default-cards": "5.16.5",
|
|
29
|
+
"@tryghost/mg-fs-utils": "^0.9.0",
|
|
30
|
+
"@tryghost/string": "0.1.27",
|
|
31
|
+
"bluebird": "^3.7.2",
|
|
32
|
+
"csv-parse": "5.3.0",
|
|
33
|
+
"date-fns": "2.29.1",
|
|
34
|
+
"ghost-ignition": "4.6.3",
|
|
35
|
+
"lodash": "4.17.21",
|
|
36
|
+
"simple-dom": "1.4.0"
|
|
37
|
+
},
|
|
38
|
+
"gitHead": "7f4422a89070611f2500548c14d286cccfd4c861"
|
|
39
|
+
}
|