@tryghost/mg-jekyll-export 0.13.8-0 → 0.14.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.
@@ -1,281 +0,0 @@
1
- import MarkdownIt from 'markdown-it';
2
- import string from '@tryghost/string';
3
- import errors from '@tryghost/errors';
4
- import {load, YAML11_SCHEMA} from 'js-yaml';
5
- import processHtml from './process-html.js';
6
-
7
- const md = new MarkdownIt({
8
- html: true
9
- });
10
-
11
- const frontMatterPattern = /^\uFEFF?(---|= yaml =)\r?\n([\s\S]*?)\r?\n(?:\1|\.\.\.)[ \t]*(?:\r?\n|$)/;
12
-
13
- function parseFrontMatter(contents) {
14
- const match = contents.match(frontMatterPattern);
15
-
16
- if (!match) {
17
- return {
18
- attributes: {},
19
- body: contents
20
- };
21
- }
22
-
23
- const yaml = match[2].trim();
24
-
25
- return {
26
- attributes: yaml ? load(yaml, {schema: YAML11_SCHEMA}) : {},
27
- body: contents.slice(match[0].length)
28
- };
29
- }
30
-
31
- // The frontmatter date may be a Date object or a string
32
- function _parseFrontMatterDate(fmDate) {
33
- let postDate;
34
- // The date was unquoted in the frontmatter, it gets parsed into a data object
35
- if (typeof fmDate === 'object') {
36
- postDate = fmDate;
37
- // Otherwise the date gets parsed as a string
38
- } else {
39
- const frontMaterDateRegex = new RegExp('([0-9]{4})[-:/\\ ]([0-9]{2})[-:/\\ ]([0-9]{2})');
40
-
41
- const dateParts = fmDate.match(frontMaterDateRegex);
42
- postDate = new Date(Date.UTC(dateParts[1], dateParts[2] - 1, dateParts[3])); // Months are zero-index, so 11 equals December
43
- }
44
-
45
- return postDate;
46
- }
47
-
48
- /*
49
- Process both the frontmatter metadata and content of a single Jekyll Markdown post.
50
-
51
- The body may be in Markdown or HTML.
52
- */
53
-
54
- const processMeta = (fileName, fileContents, options) => {
55
- const inDraftsDir = fileName.startsWith('_drafts/');
56
-
57
- let frontmatter = parseFrontMatter(fileContents);
58
- let frontmatterAttributes = frontmatter.attributes;
59
-
60
- let postDate = false;
61
- let slugParts = false;
62
- let postType = 'post';
63
- let postSlug = false;
64
- let nonStandardPostType = false;
65
-
66
- // Get an ISO 8601 date
67
- const dateNow = new Date().toISOString();
68
-
69
- // If `basename` is supposed in the frontmatter, use that as the `slug`
70
- if (frontmatterAttributes.basename) {
71
- postSlug = frontmatterAttributes.basename;
72
- }
73
-
74
- if (inDraftsDir && !postSlug) {
75
- // Posts in _drafts don't have a date in the filename
76
- const slugRegex = new RegExp('(_drafts/)(.*).(md|markdown|html)');
77
- slugParts = fileName.match(slugRegex);
78
- postSlug = slugParts[2];
79
- } else if (!inDraftsDir && frontmatterAttributes.date) {
80
- postDate = _parseFrontMatterDate(frontmatterAttributes.date);
81
- if (!postSlug) {
82
- const slugRegex = new RegExp('([0-9a-zA-Z-_]+)/(.*).(md|markdown|html)');
83
- slugParts = fileName.match(slugRegex);
84
- postSlug = slugParts[2];
85
- }
86
- // If it's a post with no `date` frontmatter
87
- } else {
88
- const datedSlugRegex = new RegExp('([0-9a-zA-Z-_]+)/([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})-(.*).(md|markdown|html)');
89
- slugParts = fileName.match(datedSlugRegex);
90
-
91
- if (slugParts[1] !== '_posts') {
92
- postType = slugParts[1];
93
- nonStandardPostType = true;
94
- }
95
-
96
- // The file could have a date like `2018-8-9`, so split it up and reassemble in a format we can use
97
- const dateYear = slugParts[2].split(/[-/]/)[0];
98
- const dateMonth = ('0' + slugParts[2].split(/[-/]/)[1]).slice(-2);
99
- const dateDay = ('0' + slugParts[2].split(/[-/]/)[2]).slice(-2);
100
- postDate = new Date(Date.UTC(dateYear, dateMonth - 1, dateDay)); // Months are zero-index, so 11 equals December
101
- if (!postSlug) {
102
- postSlug = slugParts[3];
103
- }
104
- }
105
-
106
- const post = {
107
- url:
108
- options && options.url
109
- ? `${options.url.replace(/^\/|\/$/g, '')}/${postSlug.replace(/^\/|\/$/g, '')}`
110
- : postSlug, // Combine URL & slug, and replace extra slashes
111
- data: {
112
- slug: postSlug
113
- }
114
- };
115
-
116
- const isDraft = inDraftsDir || frontmatterAttributes.published === false;
117
-
118
- // This will be processed more later and deleted
119
- post._body = frontmatter.body;
120
-
121
- post.data.status = 'published';
122
- post.data.created_at = postDate || dateNow;
123
- post.data.published_at = postDate || dateNow;
124
- post.data.updated_at = postDate || dateNow;
125
-
126
- post.data.type = 'post';
127
- post.data.status = isDraft ? 'draft' : 'published';
128
-
129
- post.data.email_only = false;
130
-
131
- post.data.title = frontmatterAttributes.title || '(Untitled)';
132
-
133
- if (frontmatterAttributes.author) {
134
- post.data.author = {
135
- url: string.slugify(frontmatterAttributes.author),
136
- data: {
137
- email: `${string.slugify(frontmatterAttributes.author)}@${options.email ? options.email : 'example.com'}`,
138
- name: frontmatterAttributes.author,
139
- slug: string.slugify(frontmatterAttributes.author),
140
- roles: ['Contributor']
141
- }
142
- };
143
- }
144
-
145
- // Add tags ^ categories from front matter
146
- // `tag` & `category` are interpreted as a single item
147
- // `tags` & `categories` are interpreted as a list of items
148
- // `category` and `categories` are processed first so that they become the "primary tag"
149
- post.data.tags = [];
150
-
151
- if (frontmatterAttributes.category) {
152
- post.data.tags.push({
153
- url: `migrator-added-tag-category-${string.slugify(frontmatterAttributes.category)}`,
154
- data: {
155
- name: frontmatterAttributes.category,
156
- slug: `category-${string.slugify(frontmatterAttributes.category)}`
157
- }
158
- });
159
- }
160
-
161
- if (frontmatterAttributes.categories) {
162
- // Jekyll allows tags to be space separated or a YAML list. We support both cases.
163
- let normalizedCats;
164
- if (typeof frontmatterAttributes.categories === 'object') {
165
- normalizedCats = frontmatterAttributes.categories;
166
- } else {
167
- normalizedCats = frontmatterAttributes.categories.split(' ');
168
- }
169
-
170
- normalizedCats.forEach(tag => {
171
- post.data.tags.push({
172
- url: `migrator-added-tag-category-${string.slugify(tag)}`,
173
- data: {
174
- name: tag,
175
- slug: `category-${string.slugify(tag)}`
176
- }
177
- });
178
- });
179
- }
180
-
181
- if (frontmatterAttributes.tag) {
182
- post.data.tags.push({
183
- url: `migrator-added-tag-${string.slugify(frontmatterAttributes.tag)}`,
184
- data: {
185
- name: frontmatterAttributes.tag,
186
- slug: string.slugify(frontmatterAttributes.tag)
187
- }
188
- });
189
- }
190
-
191
- if (frontmatterAttributes.tags) {
192
- // Jekyll allows tags to be space separated or a YAML list. We support both cases.
193
- let normalizedTags;
194
- if (typeof frontmatterAttributes.tags === 'object') {
195
- normalizedTags = frontmatterAttributes.tags;
196
- } else {
197
- normalizedTags = frontmatterAttributes.tags.split(' ');
198
- }
199
-
200
- normalizedTags.forEach(tag => {
201
- post.data.tags.push({
202
- url: `migrator-added-tag-${string.slugify(tag)}`,
203
- data: {
204
- name: tag,
205
- slug: string.slugify(tag)
206
- }
207
- });
208
- });
209
- }
210
-
211
- // Add post-specific tags
212
- if (nonStandardPostType) {
213
- post.data.tags.push({
214
- url: `migrator-added-tag-hash-${string.slugify(postType)}`,
215
- data: {
216
- name: `#${postType}`,
217
- slug: `hash-${string.slugify(postType)}`
218
- }
219
- });
220
- }
221
-
222
- return post;
223
- };
224
-
225
- export default (fileName, fileContents, globalUser = false, options = {}) => {
226
- const post = processMeta(fileName, fileContents, options);
227
-
228
- // The post body may be in Markdown or HTML.
229
- // If it's in Markdown, convert the Markdown to HTML
230
- const htmlRegex = new RegExp('.*.html$');
231
- const markdownRegex = new RegExp('.*.(md|markdown)$');
232
- const isHtml = fileName.match(htmlRegex);
233
- const isMarkdown = fileName.match(markdownRegex);
234
- let rawHtml;
235
- if (isHtml) {
236
- rawHtml = post._body;
237
- } else if (isMarkdown) {
238
- rawHtml = md.render(post._body);
239
- } else {
240
- throw new errors.InternalServerError({
241
- message: 'Unrecognized file extension. Only .md, .markdown and .html are supported'
242
- });
243
- }
244
- // Now we are done with this temporary attribute
245
- delete post._body;
246
-
247
- // Clean up the HTML
248
- post.data.html = processHtml(rawHtml, options);
249
-
250
- // Process author
251
- if (!post.data.author) {
252
- post.data.author = globalUser;
253
- }
254
-
255
- // Add extra tags
256
- post.data.tags.push({
257
- url: 'migrator-added-tag',
258
- data: {
259
- name: '#jekyll',
260
- slug: 'hash-jekyll'
261
- }
262
- });
263
-
264
- if (options.addTags) {
265
- let theTags = options.addTags.split(',').map(item => item.trim());
266
-
267
- theTags.forEach(tag => {
268
- let tagSlug = string.slugify(tag.replace('#', 'hash-'));
269
-
270
- post.data.tags.push({
271
- url: `migrator-added-tag-${tagSlug}`,
272
- data: {
273
- name: tag,
274
- slug: tagSlug
275
- }
276
- });
277
- });
278
- }
279
-
280
- return post;
281
- };
package/lib/process.js DELETED
@@ -1,27 +0,0 @@
1
- import processPost from './process-post.js';
2
-
3
- export default (input, options = {}) => {
4
- let output = {};
5
-
6
- let globalUser = {
7
- url: `/author/exampleuser`,
8
- data: {
9
- email: `exampleuser@example.com`,
10
- name: 'Example User',
11
- slug: 'exampleuser',
12
- roles: ['Contributor']
13
- }
14
- };
15
-
16
- if (input.posts && input.posts.length > 0) {
17
- output.posts = [];
18
- for (let i = 0; i < input.posts.length; i++) {
19
- const post = input.posts[i];
20
- if (post) {
21
- output.posts.push(processPost(post.fileName, post.fileContents, globalUser, options));
22
- }
23
- }
24
- }
25
-
26
- return output;
27
- };
package/lib/read-zip.js DELETED
@@ -1,45 +0,0 @@
1
- import errors from '@tryghost/errors';
2
- import fsUtils from '@tryghost/mg-fs-utils';
3
- import {ui} from '@tryghost/pretty-cli';
4
-
5
- export default (zipPath, options) => {
6
- let content = {
7
- posts: []
8
- };
9
-
10
- let skippedFileCount = 0;
11
-
12
- // We only support the current Jekyll export file structure:
13
- // - _posts
14
- // - 2020-10-27-my-post.md
15
- // - 2021-05-19-2020-was-quite-a-year.md
16
- // The file extension may be ".md", ".markdown" or ".html"
17
- fsUtils.zip.read(zipPath, (entryName, zipEntry) => {
18
- // The `entryName` arg here lacks the directory name.
19
- // We need zipEntry.entryName instead, which contains it.
20
- entryName = zipEntry.entryName;
21
-
22
- if (/^_(posts|drafts)\/.*\.(md|markdown|html)$/.test(entryName)) {
23
- content.posts.push({
24
- fileName: entryName,
25
- fileContents: zipEntry.getData().toString('utf8')
26
- });
27
-
28
- // Skip if not matched above, and report skipped files if `--verbose`
29
- } else {
30
- if (options.verbose) {
31
- ui.log.info('Skipped: ' + zipEntry.entryName);
32
- }
33
- skippedFileCount += 1;
34
- }
35
- });
36
-
37
- ui.log.info('Skipped files: ' + skippedFileCount);
38
-
39
- if (!content.posts.length) {
40
- ui.log.error('No content found to import! Quitting.');
41
- throw new errors.InternalServerError();
42
- }
43
-
44
- return content;
45
- };