@jungvonmatt/cssg-plugin-hugo 1.0.0 → 1.0.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/.eslintrc.cjs ADDED
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ root: true,
3
+ plugins: ['prettier'],
4
+ extends: ["xo-space", 'plugin:prettier/recommended'],
5
+ ignorePatterns: ['**/*.cjs', 'src/**/*.test.js', 'src/__test__/*'],
6
+ env: {
7
+ node: true,
8
+ jest: true,
9
+ }
10
+ };
package/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.0.1](https://github.com/jungvonmatt/contentful-ssg/compare/v1.0.0...v1.0.1) (2021-11-25)
7
+
8
+ **Note:** Version bump only for package @jungvonmatt/cssg-plugin-hugo
9
+
10
+
11
+
12
+
13
+
6
14
  # [1.0.0](https://github.com/jungvonmatt/contentful-ssg/compare/v1.0.0-alpha.0...v1.0.0) (2021-11-25)
7
15
 
8
16
  **Note:** Version bump only for package @jungvonmatt/cssg-plugin-hugo
package/index.js CHANGED
@@ -1,10 +1,9 @@
1
-
2
1
  import mergeOptionsModule from 'merge-options';
3
2
 
4
- import {snakeCaseKeys} from '@jungvonmatt/contentful-ssg/lib/object';
3
+ import { snakeCaseKeys } from '@jungvonmatt/contentful-ssg/lib/object';
5
4
  import mm from 'micromatch';
6
- import {existsSync} from 'fs';
7
- import {writeFile, readFile} from 'fs/promises';
5
+ import { existsSync } from 'fs';
6
+ import { writeFile, readFile } from 'fs/promises';
8
7
  import path from 'path';
9
8
 
10
9
  const mergeOptions = mergeOptionsModule.bind({ ignoreUndefined: true });
@@ -20,47 +19,52 @@ const defaultOptions = {
20
19
  fieldIdHome: 'home',
21
20
  fieldIdSlug: 'slug',
22
21
  fieldIdParent: 'parent_page',
23
- typeConfig: {
22
+ typeConfig: {
24
23
  [TYPE_PAGE]: ['page'],
25
24
  [TYPE_DATA]: ['d-*'],
26
25
  },
27
- }
26
+ };
28
27
 
29
28
  export default (args) => {
30
- const options = {...defaultOptions, ...(args || {})}
31
-
32
-
29
+ const options = { ...defaultOptions, ...(args || {}) };
33
30
 
34
31
  const getSettingsHelper = (runtimeContext) => {
35
32
  let settings = {};
36
33
  if (options.typeIdSettings) {
37
- settings = Object.fromEntries(Array.from((runtimeContext?.localized ?? new Map()).entries()).map(([locale, contentfulData]) => {
38
- const entryMap = contentfulData?.entryMap ?? new Map();
39
- const settingsEntries = (Array.from(entryMap.values())).filter(entry => (entry?.sys?.contentType?.sys?.id ?? 'unknown') === options.typeIdSettings);
40
- const settingsFields = settingsEntries.map(entry => entry?.fields ?? {});
41
- return [locale, mergeOptions(...settingsFields)];
42
- }));
34
+ settings = Object.fromEntries(
35
+ Array.from((runtimeContext?.localized ?? new Map()).entries()).map(
36
+ ([locale, contentfulData]) => {
37
+ const entryMap = contentfulData?.entryMap ?? new Map();
38
+ const settingsEntries = Array.from(entryMap.values()).filter(
39
+ (entry) => (entry?.sys?.contentType?.sys?.id ?? 'unknown') === options.typeIdSettings
40
+ );
41
+ const settingsFields = settingsEntries.map((entry) => entry?.fields ?? {});
42
+ return [locale, mergeOptions(...settingsFields)];
43
+ }
44
+ )
45
+ );
43
46
  }
44
47
 
45
- return (key, locale, defaultValue) => settings?.[locale]?.[key] ?? defaultValue;
46
- }
48
+ return (key, locale, defaultValue) => settings?.[locale]?.[key] ?? defaultValue;
49
+ };
47
50
 
48
51
  const getEntryType = (transformContext) => {
49
- const {contentTypeId} = transformContext;
50
- const [type = TYPE_HEADLESS] = Object.entries(options?.typeConfig ?? {}).find(([,pattern]) => mm.isMatch(contentTypeId, pattern)) || [];
52
+ const { contentTypeId } = transformContext;
53
+ const [type = TYPE_HEADLESS] =
54
+ Object.entries(options?.typeConfig ?? {}).find(([, pattern]) =>
55
+ mm.isMatch(contentTypeId, pattern)
56
+ ) || [];
51
57
 
52
58
  return type;
53
- }
54
-
55
-
59
+ };
56
60
 
57
61
  return {
58
62
  // Before hook
59
63
  async before(runtimeContext) {
60
- const {helper,converter, data, localized} = runtimeContext;
64
+ const { helper, converter, data, localized } = runtimeContext;
61
65
  const locales = data?.locales ?? [];
62
66
 
63
- // initialize getSettings
67
+ // Initialize getSettings
64
68
  const getSettings = getSettingsHelper(runtimeContext);
65
69
  helper.getSettings = getSettings;
66
70
  // Write config toml according to locale settings in contentful
@@ -88,27 +92,28 @@ export default (args) => {
88
92
  ];
89
93
  })
90
94
  );
91
- await writeFile(
92
- dst,
93
- converter.toml.stringify(languageConfig)
94
- );
95
+ await writeFile(dst, converter.toml.stringify(languageConfig));
95
96
  }
96
97
 
97
98
  // Find section pages and add them to the runtimeconfig
98
- const enhancedLocalized = new Map(Array.from(localized.entries()).map(([localeCode, contentfulData]) => {
99
- const {entries} = contentfulData;
100
- const sectionIds = entries.reduce((nodes, entry) => {
101
- const id = entry?.fields?.[options.fieldIdParent]?.sys?.id;
102
- if (id) {
103
- nodes.add(id);
104
- }
105
-
106
- return nodes;
107
- }, new Set());
108
- return [localeCode, {...contentfulData, sectionIds}];
109
- }));
110
-
111
- return {...runtimeContext, helper, localized: enhancedLocalized};
99
+ const enhancedLocalized = new Map(
100
+ Array.from(localized.entries()).map(([localeCode, contentfulData]) => {
101
+ const { entries } = contentfulData;
102
+ const sectionIds = entries.reduce((nodes, entry) => {
103
+ const id = entry?.fields?.[options.fieldIdParent]?.sys?.id;
104
+ if (id) {
105
+ nodes.add(id);
106
+ }
107
+
108
+ return nodes;
109
+ }, new Set());
110
+ return [localeCode, { ...contentfulData, sectionIds }];
111
+ })
112
+ );
113
+
114
+ const i18n = Object.fromEntries(locales.map((locale) => [locale.code, {}]));
115
+
116
+ return { ...runtimeContext, helper, localized: enhancedLocalized, i18n };
112
117
  },
113
118
 
114
119
  /**
@@ -117,15 +122,11 @@ export default (args) => {
117
122
  * @param runtimeContext
118
123
  * @returns
119
124
  */
120
- async mapEntryLink(
121
- transformContext,
122
- runtimeContext,
123
- prev
124
- ) {
125
+ async mapEntryLink(transformContext, runtimeContext, prev) {
125
126
  const directory = await runtimeContext.hooks.mapDirectory(transformContext);
126
127
  const filename = await runtimeContext.hooks.mapFilename(transformContext);
127
128
 
128
- return {...prev, path: path.join(directory,filename)};
129
+ return { ...prev, path: path.join(directory, filename) };
129
130
  },
130
131
 
131
132
  /**
@@ -134,7 +135,7 @@ export default (args) => {
134
135
  * @returns
135
136
  */
136
137
  async mapDirectory(transformContext) {
137
- const {contentTypeId, locale} = transformContext;
138
+ const { contentTypeId, locale } = transformContext;
138
139
  const type = getEntryType(transformContext);
139
140
 
140
141
  if (type === TYPE_DATA) {
@@ -156,8 +157,8 @@ export default (args) => {
156
157
  * @returns
157
158
  */
158
159
  async mapFilename(transformContext, runtimeContext) {
159
- const {id, locale, entry, contentTypeId, utils} = transformContext;
160
- const {helper, localized} = runtimeContext;
160
+ const { id, locale, entry, contentTypeId, utils } = transformContext;
161
+ const { helper, localized } = runtimeContext;
161
162
  const sectionIds = localized?.get(locale.code)?.sectionIds ?? new Set();
162
163
 
163
164
  const type = getEntryType(transformContext);
@@ -165,9 +166,9 @@ export default (args) => {
165
166
  const home = helper.getSettings(options.fieldIdHome, locale.code);
166
167
  const homeId = home?.sys?.id;
167
168
 
168
- if (homeId && entry?.sys?.id === homeId) {
169
+ if (homeId && entry?.sys?.id === homeId) {
169
170
  return `/_index.md`;
170
- }
171
+ }
171
172
 
172
173
  if (contentTypeId === options.typeIdSettings) {
173
174
  return path.join(locale.code, `settings.json`);
@@ -182,25 +183,27 @@ export default (args) => {
182
183
  linkField: `fields.${options.fieldIdParent}`,
183
184
  entry,
184
185
  });
185
- return path.join(...(slugs || []), `_index.md`)
186
+ return path.join(...(slugs || []), `_index.md`);
186
187
  }
187
188
 
188
189
  if (type === TYPE_PAGE) {
189
190
  const slugs = utils.collectParentValues(`fields.${options.fieldIdSlug}`, {
190
191
  linkField: `fields.${options.fieldIdParent}`,
191
- entry
192
+ entry,
192
193
  });
193
194
 
194
- return path.join(...(slugs || []), `${entry?.fields?.[options.fieldIdSlug] ?? 'unknown'}.md`)
195
+ return path.join(
196
+ ...(slugs || []),
197
+ `${entry?.fields?.[options.fieldIdSlug] ?? 'unknown'}.md`
198
+ );
195
199
  }
196
200
 
197
201
  return path.join(id, `index.${locale.code}.md`);
198
202
  },
199
203
 
200
204
  async transform(transformContext, runtimeContext) {
201
- const {content, id, contentTypeId, locale} = transformContext;
202
- const contentDir = runtimeContext.config.directory;
203
- const toml = runtimeContext.converter.toml;
205
+ const { content, id, contentTypeId, locale } = transformContext;
206
+
204
207
  const type = getEntryType(transformContext);
205
208
 
206
209
  // Automatically store dictionary entries in i18n/[locale].json
@@ -209,22 +212,19 @@ export default (args) => {
209
212
  const { key, other, one } = content;
210
213
  const translations = one ? { one, other } : { other };
211
214
 
212
- const dictionaryPath = path.join(contentDir, `../i18n/${locale.code}.toml`);
213
- const oldContent = existsSync(dictionaryPath) ? toml.parse(await readFile(dictionaryPath, 'utf8')) : {};
214
- await writeFile(
215
- dictionaryPath,
216
- toml.stringify({ ...oldContent, [key]: translations }, undefined, ' ')
217
- );
215
+ runtimeContext.i18n[locale.code][key] = translations;
218
216
 
219
- // dont't write i-18n objects to the content folder
217
+ // Dont't write i-18n objects to the content folder
220
218
  return undefined;
221
219
  }
222
220
 
223
221
  if (type === TYPE_PAGE) {
224
- return {...snakeCaseKeys({
225
- ...content,
226
-
227
- }), translationKey: id};
222
+ return {
223
+ ...snakeCaseKeys({
224
+ ...content,
225
+ }),
226
+ translationKey: id,
227
+ };
228
228
  }
229
229
 
230
230
  if (type === TYPE_HEADLESS) {
@@ -235,9 +235,23 @@ export default (args) => {
235
235
  }
236
236
 
237
237
  return snakeCaseKeys(content);
238
- }
238
+ },
239
+
240
+ async after(runtimeContext) {
241
+ const contentDir = runtimeContext.config.directory;
242
+ const { toml } = runtimeContext.converter;
243
+ const i18n = runtimeContext?.i18n ?? {};
244
+
245
+ await Promise.all(
246
+ Object.entries(i18n).map(async ([localeCode, translations]) => {
247
+ const dictionaryPath = path.join(contentDir, `../i18n/${localeCode}.toml`);
248
+ const oldContent = existsSync(dictionaryPath)
249
+ ? toml.parse(await readFile(dictionaryPath, 'utf8'))
250
+ : {};
251
+
252
+ return writeFile(dictionaryPath, toml.stringify({ ...oldContent, ...translations }));
253
+ })
254
+ );
255
+ },
239
256
  };
240
257
  };
241
-
242
-
243
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jungvonmatt/cssg-plugin-hugo",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "type": "module",
10
10
  "scripts": {
11
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "lint": "eslint --color index.js --fix"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -29,5 +29,5 @@
29
29
  "merge-options": "^3.0.4",
30
30
  "micromatch": "^4.0.4"
31
31
  },
32
- "gitHead": "a5b7e99aef936cac4d1166d7a347ffe1a480799a"
32
+ "gitHead": "a66307559e8effa5b8961ba3ab9a784d497a9a59"
33
33
  }