@tryghost/mg-jekyll-export 0.1.2

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 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,98 @@
1
+ # Migrate Jekyll Export
2
+
3
+ Migrate content from Jekyll using the supplied zip file, and generate 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-jekyll-export --save`
17
+
18
+ or
19
+
20
+ `yarn add @tryghost/mg-jekyll-export`
21
+
22
+
23
+ ## Usage
24
+
25
+ To run a Jekyll migration, the required command is:
26
+
27
+ ```sh
28
+ migrate jekyll <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
+ - bool - default: `false`
35
+ - Show verbose output
36
+ - **`--zip`**
37
+ - bool - default: `true`
38
+ - Create a zip file
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
+ - **`-u` `--url`**
47
+ - string - default: `false`
48
+ - Provide a URL (without trailing slash) to the hosted source site
49
+ - **`-e` `--email`**
50
+ - string - default: `false`
51
+ - Provide an email domain for users e.g. example.com
52
+ - **`--addTags`**
53
+ - string - default: `null`
54
+ - Provide one or more tag names which should be added to every post in this migration
55
+ - **`--datedPermalinks`**
56
+ - Set the dated permalink structure
57
+ - string - default: `none`
58
+ - Choices: `none`, `'/yyyy/mm/'`, `'/yyyy/mm/dd/'`
59
+ - **`--fallBackHTMLCard`**
60
+ - bool - default: `false`
61
+ - Fall back to convert to HTMLCard, if standard Mobiledoc convert fails
62
+
63
+ A more complex migration command could look like this:
64
+
65
+ ```sh
66
+ migrate jekyll <path to zip file> --email example.com
67
+ ```
68
+
69
+ This will process all posts from the zip file, and all authors will have an email address ending in 'example.com'
70
+
71
+
72
+ ## Develop
73
+
74
+ This is a mono repository, managed with [lerna](https://lerna.js.org).
75
+
76
+ Follow the instructions for the top-level repo.
77
+ 1. `git clone` this repo & `cd` into it as usual
78
+ 2. Run `yarn` to install top-level dependencies.
79
+
80
+
81
+ ## Run
82
+
83
+ To run a local development copy, `cd` into this directory, and use `yarn dev` instead of `migrate` like so:
84
+
85
+ ```sh
86
+ yarn dev jekyll <path to zip file>
87
+ ```
88
+
89
+
90
+ ## Test
91
+
92
+ - `yarn lint` run just eslint
93
+ - `yarn test` run lint and tests
94
+
95
+
96
+ # Copyright & License
97
+
98
+ Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ const readZip = require('./lib/read-zip');
2
+ const process = require('./lib/process');
3
+
4
+ const jekyllExport = module.exports = (pathToZip, options) => {
5
+ let input = readZip(pathToZip, options);
6
+ let output = process(input, options);
7
+
8
+ return output;
9
+ };
10
+
11
+ if (require.main === module && process.argv[2]) {
12
+ console.log(JSON.stringify(jekyllExport(process.argv[2]), null, 2)); // eslint-disable-line
13
+ }
@@ -0,0 +1,53 @@
1
+ const MarkdownIt = require('markdown-it');
2
+ const md = new MarkdownIt({
3
+ html: true
4
+ });
5
+ const fm = require('front-matter');
6
+ const $ = require('cheerio');
7
+
8
+ module.exports = (markdown, options = {}) => {
9
+ const frontmatter = fm(markdown);
10
+ const processedMarkdown = md.render(frontmatter.body);
11
+ const $html = $.load(processedMarkdown, {
12
+ decodeEntities: false
13
+ });
14
+
15
+ // Convert relative links and image paths to absolute
16
+ if (options.url) {
17
+ const urlData = new URL(options.url);
18
+ const urlOrigin = urlData.href;
19
+
20
+ $html('a').each((i, anchor) => {
21
+ const thisURL = $(anchor).attr('href');
22
+
23
+ // If it starts with a slash, append the base URL
24
+ if (thisURL.indexOf('/') === 0) {
25
+ const updatedURL = `${urlOrigin.replace(/^\/|\/$/g, '')}/${thisURL.replace(/^\/|\/$/g, '')}`;
26
+ $(anchor).attr('href', updatedURL);
27
+ }
28
+ });
29
+
30
+ $html('img').each((i, img) => {
31
+ const thisSrc = $(img).attr('src');
32
+
33
+ // If it starts with a slash, append the base URL
34
+ if (thisSrc.indexOf('/') === 0) {
35
+ const updatedURL = `${urlOrigin.replace(/^\/|\/$/g, '')}/${thisSrc.replace(/^\/|\/$/g, '')}`;
36
+ $(img).attr('src', updatedURL);
37
+ }
38
+ });
39
+ }
40
+
41
+ // Unwrap <p> tags that are in <li> tags
42
+ $html('li').each((i, li) => {
43
+ const hasP = $(li).find('p').length;
44
+
45
+ if (hasP) {
46
+ $(li).html($(li).find('p').html());
47
+ }
48
+ });
49
+
50
+ let html = $html.html().trim();
51
+
52
+ return html;
53
+ };
@@ -0,0 +1,135 @@
1
+ const string = require('@tryghost/string');
2
+ const fm = require('front-matter');
3
+ const processContent = require('./process-content');
4
+
5
+ const processMeta = (fileName, markdown, options) => {
6
+ const isDraft = fileName.startsWith('drafts/');
7
+
8
+ let frontmatter = fm(markdown);
9
+ let frontmatterAttributes = frontmatter.attributes;
10
+
11
+ let postDate = false;
12
+ let slugParts = false;
13
+ let postType = 'post';
14
+ let postSlug = false;
15
+ let nonStandardPostType = false;
16
+
17
+ // Get an ISO 8601 date
18
+ const dateNow = new Date().toISOString();
19
+
20
+ if (isDraft) {
21
+ const slugRegex = new RegExp('(drafts/)(.*).md');
22
+ slugParts = fileName.match(slugRegex);
23
+ postSlug = slugParts[2];
24
+ } else if (frontmatterAttributes.date) {
25
+ const frontMaterDateRegex = new RegExp('([0-9]{4})[-:/\\ ]([0-9]{2})[-:/\\ ]([0-9]{2})');
26
+ const dateParts = frontmatterAttributes.date.match(frontMaterDateRegex);
27
+ postDate = new Date(Date.UTC(dateParts[1], (dateParts[2] - 1), dateParts[3], 12, 0, 0)); // Months are zero-index, so 12 equals December
28
+
29
+ const slugRegex = new RegExp('([0-9a-zA-Z-_]+)/(.*).md');
30
+ slugParts = fileName.match(slugRegex);
31
+ postSlug = slugParts[2];
32
+ } else {
33
+ const datedSlugRegex = new RegExp('([0-9a-zA-Z-_]+)/([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})-(.*).md');
34
+ slugParts = fileName.match(datedSlugRegex);
35
+
36
+ if (slugParts[1] !== 'posts') {
37
+ postType = slugParts[1];
38
+ nonStandardPostType = true;
39
+ }
40
+
41
+ // The file could have a date like `2018-8-9`, so split it up and reassemble in a format we can use
42
+ const dateYear = slugParts[2].split(/[-/]/)[0];
43
+ const dateMonth = ('0' + slugParts[2].split(/[-/]/)[1]).slice(-2);
44
+ const dateDay = ('0' + slugParts[2].split(/[-/]/)[2]).slice(-2);
45
+ postDate = new Date(Date.UTC(dateYear, (dateMonth - 1), dateDay, 12, 0, 0)); // Months are zero-index, so 12 equals December
46
+ postSlug = slugParts[3];
47
+ }
48
+
49
+ const post = {
50
+ url: (options && options.url) ? `${options.url.replace(/^\/|\/$/g, '')}/${postSlug.replace(/^\/|\/$/g, '')}` : postSlug, // Combine URL & slug, and replace extra slashes
51
+ data: {
52
+ slug: postSlug
53
+ }
54
+ };
55
+
56
+ post.data.status = 'published';
57
+ post.data.created_at = postDate || dateNow;
58
+ post.data.published_at = postDate || dateNow;
59
+ post.data.updated_at = postDate || dateNow;
60
+
61
+ post.data.type = 'post';
62
+ post.data.status = (isDraft) ? 'draft' : 'published';
63
+
64
+ post.data.email_only = false;
65
+
66
+ post.data.title = frontmatterAttributes.title || '(Untitled)';
67
+
68
+ if (frontmatterAttributes.author) {
69
+ post.data.author = {
70
+ url: string.slugify(frontmatterAttributes.author),
71
+ data: {
72
+ email: `${string.slugify(frontmatterAttributes.author)}@${(options.email) ? options.email : 'dummyemail.com'}`,
73
+ name: frontmatterAttributes.author,
74
+ slug: string.slugify(frontmatterAttributes.author),
75
+ roles: [
76
+ 'Contributor'
77
+ ]
78
+ }
79
+ };
80
+ }
81
+
82
+ // Add post-specific tags
83
+ post.data.tags = [];
84
+
85
+ if (nonStandardPostType) {
86
+ let typeTagSlug = `hash-${string.slugify(postType)}`;
87
+ post.data.tags.push({
88
+ url: `migrator-added-tag-${typeTagSlug}`,
89
+ data: {
90
+ name: `#${postType}`,
91
+ slug: typeTagSlug
92
+ }
93
+ });
94
+ }
95
+
96
+ return post;
97
+ };
98
+
99
+ module.exports = (fileName, markdown, globalUser = false, options = {}) => {
100
+ const post = processMeta(fileName, markdown, options);
101
+
102
+ // Process content
103
+ post.data.html = processContent(markdown, options);
104
+
105
+ // Process author
106
+ if (!post.data.author) {
107
+ post.data.author = globalUser;
108
+ }
109
+
110
+ // Add extra tags
111
+ post.data.tags.push({
112
+ url: 'migrator-added-tag', data: {
113
+ name: '#jekyll',
114
+ slug: 'hash-jekyll'
115
+ }
116
+ });
117
+
118
+ if (options.addTags) {
119
+ let theTags = options.addTags.split(',').map(item => item.trim());
120
+
121
+ theTags.forEach((tag) => {
122
+ let tagSlug = string.slugify(tag.replace('#', 'hash-'));
123
+
124
+ post.data.tags.push({
125
+ url: `migrator-added-tag-${tagSlug}`,
126
+ data: {
127
+ name: tag,
128
+ slug: tagSlug
129
+ }
130
+ });
131
+ });
132
+ }
133
+
134
+ return post;
135
+ };
package/lib/process.js ADDED
@@ -0,0 +1,23 @@
1
+ const processPost = require('./process-post');
2
+
3
+ module.exports = (input, options = {}) => {
4
+ let output = {};
5
+
6
+ let globalUser = {
7
+ url: `/author/dummyuser`,
8
+ data: {
9
+ email: `dummyuser@dummyemail.com`,
10
+ name: 'Dummy User',
11
+ slug: 'dummyuser',
12
+ roles: [
13
+ 'Contributor'
14
+ ]
15
+ }
16
+ };
17
+
18
+ if (input.posts && input.posts.length > 0) {
19
+ output.posts = input.posts.map(post => processPost(post.fileName, post.markdown, globalUser, options));
20
+ }
21
+
22
+ return output;
23
+ };
@@ -0,0 +1,36 @@
1
+ const fsUtils = require('@tryghost/mg-fs-utils');
2
+ const ui = require('@tryghost/pretty-cli').ui;
3
+
4
+ module.exports = (zipPath, options) => {
5
+ let content = {
6
+ posts: []
7
+ };
8
+
9
+ let skippedFileCount = 0;
10
+
11
+ // We only support the current Medium export file structure:
12
+ // - posts
13
+ // - 2020-10-27-my-post.md
14
+ // - 2021-05-19-2020-was-quite-a-year.md
15
+
16
+ fsUtils.zip.read(zipPath, (entryName, zipEntry) => {
17
+ // Catch all HTML files inside `profile/`
18
+ if (/^posts\/.*\.md$/.test(entryName)) {
19
+ content.posts.push({
20
+ fileName: entryName,
21
+ markdown: zipEntry.getData().toString('utf8')
22
+ });
23
+
24
+ // Skip if not matched above, and report skipped files if `--verbose`
25
+ } else {
26
+ if (options.verbose) {
27
+ ui.log.info('Skipped: ' + entryName);
28
+ }
29
+ skippedFileCount += 1;
30
+ }
31
+ });
32
+
33
+ ui.log.info('Skipped files: ' + skippedFileCount);
34
+
35
+ return content;
36
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@tryghost/mg-jekyll-export",
3
+ "version": "0.1.2",
4
+ "description": "tbc",
5
+ "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-jekyll-export",
6
+ "author": "Ghost Foundation",
7
+ "license": "MIT",
8
+ "main": "index.js",
9
+ "scripts": {
10
+ "dev": "echo \"Implement me!\"",
11
+ "test": "NODE_ENV=testing mocha ./test/**/*.test.js",
12
+ "lint": "eslint . --ext .js --cache",
13
+ "posttest": "yarn lint"
14
+ },
15
+ "files": [
16
+ "index.js",
17
+ "lib"
18
+ ],
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "devDependencies": {
23
+ "mocha": "9.2.2",
24
+ "should": "13.2.3",
25
+ "sinon": "11.1.2"
26
+ },
27
+ "dependencies": {
28
+ "@tryghost/mg-fs-utils": "^0.8.0",
29
+ "@tryghost/string": "^0.1.17",
30
+ "cheerio": "0.22.0",
31
+ "front-matter": "4.0.2",
32
+ "markdown-it": "13.0.0"
33
+ },
34
+ "gitHead": "a2fd432523e0e9f316686511d7494b855ef0007e"
35
+ }