@jungvonmatt/cssg-plugin-hugo 1.0.1 → 1.0.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/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.2](https://github.com/jungvonmatt/contentful-ssg/compare/v1.0.1...v1.0.2) (2021-11-29)
7
+
8
+ **Note:** Version bump only for package @jungvonmatt/cssg-plugin-hugo
9
+
10
+
11
+
12
+
13
+
6
14
  ## [1.0.1](https://github.com/jungvonmatt/contentful-ssg/compare/v1.0.0...v1.0.1) (2021-11-25)
7
15
 
8
16
  **Note:** Version bump only for package @jungvonmatt/cssg-plugin-hugo
package/README.md CHANGED
@@ -25,7 +25,6 @@ The configured translations can then be used using the [`i18n`](https://gohugo.i
25
25
  <p>
26
26
 
27
27
  ```js
28
-
29
28
  module.exports = function (migration) {
30
29
  const dI18n = migration
31
30
  .createContentType('d-i18n')
@@ -74,6 +73,7 @@ module.exports = function (migration) {
74
73
  });
75
74
  };
76
75
  ```
76
+
77
77
  </p>
78
78
  </details>
79
79
 
@@ -101,7 +101,6 @@ All reference fields are extended by the path to the associated markdown file so
101
101
  {{ end }}
102
102
  ```
103
103
 
104
-
105
104
  ## Install
106
105
 
107
106
  `npm install @jungvonmatt/cssg-plugin-hugo`
@@ -122,15 +121,16 @@ plugins: [
122
121
 
123
122
  ## Options
124
123
 
125
- | Name | Type | Default | Description |
126
- | -------------- | --------- | --------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
127
- | typeIdSettings | `string` | `'d-settings'` | The id of the settings content type. |
128
- | typeIdI18n | `string` | `'d-i18n'` | The id of the i18n content type for the translation of strings. |
129
- | fieldIdHome | `string` | `'home'` | The id of reference field to the home page in the settings content type. |
130
- | fieldIdSlug | `string` | `'slug'` | The id of the slug field in page content types. |
131
- | fieldIdParent | `string` | `'parent_page'` | The id of the parent page reference field in page content types. |
132
- | languageConfig | `boolean` | `true` | Auto-generate the hugo language config based on your locale configuration in contentful. |
133
- | typeConfig | `object` | `{ page: ['page'], data: ['d-*']}` | Pass a map with entry types (`data`, `map`) pointing to one or more glob patterns matching the content type ids.\ Data types will be stored inside the `/data/` directory. \ pages types will be stored inside `/content/<locale>/`.\ All content types that do not match are considered headless and will be stored inside `/content/headless` |
124
+ | Name | Type | Default | Description |
125
+ | ------------------- | ------------ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
126
+ | typeIdSettings | `string` | `'d-settings'` | The id of the settings content type. |
127
+ | typeIdI18n | `string` | `'d-i18n'` | The id of the i18n content type for the translation of strings. |
128
+ | fieldIdHome | `string` | `'home'` | The id of reference field to the home page in the settings content type. |
129
+ | fieldIdSlug | `string` | `'slug'` | The id of the slug field in page content types. |
130
+ | fieldIdParent | `string` | `'parent_page'` | The id of the parent page reference field in page content types. |
131
+ | languageConfig | `boolean` | `true` | Auto-generate the hugo language config based on your locale configuration in contentful. |
132
+ | translationStrategy | `'filename'` | `'directory'` \| `'filename'` | How to translate your content. See https://gohugo.io/content-management/multilingual/#translate-your-content |
133
+ | typeConfig | `object` | `{ page: ['page']}` | Pass a map with entry types (`page`) pointing to one or more glob patterns matching the content type ids.\ Data types will be stored inside the `/data/` directory. \ pages types will be stored inside `/content/${pagesFolder}/`.\ All content types that do not match are considered headless and will be stored inside `/content/${headlessFolder}/` |
134
134
 
135
135
  Example:
136
136
 
package/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+
2
+ export type TYPECONFIG_KEY = 'page' | 'data' | 'headless';
3
+ export type TRANSLATION_STRATEGY = 'directory' | 'filename';
4
+
5
+ export interface AssetPluginConfig {
6
+ typeIdSettings: string;
7
+ translationStrategy: TRANSLATION_STRATEGY;
8
+ typeIdI18n: string;
9
+ languageConfig: boolean;
10
+ fieldIdHome: string;
11
+ fieldIdSlug: string;
12
+ fieldIdParent: string;
13
+ typeConfig: {
14
+ [x: TYPECONFIG_KEY]: string | string[];
15
+ },
16
+ }
package/index.js CHANGED
@@ -8,20 +8,22 @@ import path from 'path';
8
8
 
9
9
  const mergeOptions = mergeOptionsModule.bind({ ignoreUndefined: true });
10
10
 
11
- export const TYPE_PAGE = 'page';
11
+ export const TYPE_CONTENT = 'content';
12
12
  export const TYPE_DATA = 'data';
13
- export const TYPE_HEADLESS = 'headless';
13
+
14
+ export const STRATEGY_DIRECTORY = 'directory';
15
+ export const STRATEGY_FILENAME = 'filename';
14
16
 
15
17
  const defaultOptions = {
16
18
  typeIdSettings: 'd-settings',
19
+ translationStrategy: STRATEGY_DIRECTORY,
17
20
  typeIdI18n: 'd-i18n',
18
21
  languageConfig: true,
19
22
  fieldIdHome: 'home',
20
23
  fieldIdSlug: 'slug',
21
24
  fieldIdParent: 'parent_page',
22
25
  typeConfig: {
23
- [TYPE_PAGE]: ['page'],
24
- [TYPE_DATA]: ['d-*'],
26
+ [TYPE_CONTENT]: ['page'],
25
27
  },
26
28
  };
27
29
 
@@ -50,7 +52,7 @@ export default (args) => {
50
52
 
51
53
  const getEntryType = (transformContext) => {
52
54
  const { contentTypeId } = transformContext;
53
- const [type = TYPE_HEADLESS] =
55
+ const [type = TYPE_DATA] =
54
56
  Object.entries(options?.typeConfig ?? {}).find(([, pattern]) =>
55
57
  mm.isMatch(contentTypeId, pattern)
56
58
  ) || [];
@@ -80,15 +82,19 @@ export default (args) => {
80
82
  // https://github.com/gohugoio/hugo/issues/7344
81
83
  const languageCode = code.toLowerCase();
82
84
  const [languageNameShort] = languageCode.split('-');
85
+
86
+ const localeConfig = {
87
+ languageCode,
88
+ languageName,
89
+ languageNameShort,
90
+ weight: locale.default ? 1 : 2,
91
+ };
92
+
83
93
  return [
84
94
  code,
85
- {
86
- contentDir: `content/${languageCode}`,
87
- languageCode,
88
- languageName,
89
- languageNameShort,
90
- weight: locale.default ? 1 : 2,
91
- },
95
+ options.translationStrategy === 'directory'
96
+ ? { contentDir: `content/${languageCode}`, ...localeConfig }
97
+ : localeConfig,
92
98
  ];
93
99
  })
94
100
  );
@@ -138,67 +144,83 @@ export default (args) => {
138
144
  const { contentTypeId, locale } = transformContext;
139
145
  const type = getEntryType(transformContext);
140
146
 
141
- if (type === TYPE_DATA) {
142
- return '../data';
147
+ if (type === TYPE_CONTENT) {
148
+ return options.translationStrategy === STRATEGY_FILENAME ? '' : locale.code;
143
149
  }
144
150
 
145
- if (contentTypeId === TYPE_PAGE) {
146
- return locale.code;
147
- }
148
-
149
- return path.join('headless', contentTypeId);
151
+ return options.translationStrategy === STRATEGY_FILENAME
152
+ ? path.join('../data', contentTypeId)
153
+ : path.join('../data', locale.code, contentTypeId);
150
154
  },
151
155
 
152
156
  /**
153
157
  * Map filenames data files to data, headless bundles to headless folder and pages in a
154
158
  * directory structure which matches the sitemap
155
159
  * @param transformContext
156
- * @param runtimeContext
160
+ * @param {RuntimeContext} runtimeContext
157
161
  * @returns
158
162
  */
159
163
  async mapFilename(transformContext, runtimeContext) {
160
164
  const { id, locale, entry, contentTypeId, utils } = transformContext;
161
- const { helper, localized } = runtimeContext;
165
+ const { helper, localized, defaultLocale } = runtimeContext;
162
166
  const sectionIds = localized?.get(locale.code)?.sectionIds ?? new Set();
163
167
 
168
+ const localeData =
169
+ options.translationStrategy === STRATEGY_FILENAME
170
+ ? localized.get(defaultLocale)
171
+ : localized.get(locale.code);
172
+ const collectEntryMap = localeData.entryMap;
173
+ const collectEntry = collectEntryMap.get(entry.sys.id);
174
+
164
175
  const type = getEntryType(transformContext);
165
176
 
166
177
  const home = helper.getSettings(options.fieldIdHome, locale.code);
167
178
  const homeId = home?.sys?.id;
168
179
 
169
180
  if (homeId && entry?.sys?.id === homeId) {
170
- return `/_index.md`;
181
+ return options.translationStrategy === STRATEGY_FILENAME
182
+ ? `/_index.${locale.code}.md`
183
+ : `/_index.md`;
171
184
  }
172
185
 
173
186
  if (contentTypeId === options.typeIdSettings) {
174
- return path.join(locale.code, `settings.json`);
187
+ return options.translationStrategy === STRATEGY_FILENAME
188
+ ? `../settings.${locale.code}.json`
189
+ : '../settings.json';
175
190
  }
176
191
 
177
- if (type === TYPE_DATA) {
178
- return path.join(locale.code, contentTypeId, `${id}.json`);
179
- }
180
-
181
- if (type === TYPE_PAGE && sectionIds.has(id)) {
192
+ if (type === TYPE_CONTENT && sectionIds.has(id)) {
182
193
  const slugs = utils.collectValues(`fields.${options.fieldIdSlug}`, {
183
194
  linkField: `fields.${options.fieldIdParent}`,
184
- entry,
195
+ entry: collectEntry,
196
+ entryMap: collectEntryMap,
185
197
  });
186
- return path.join(...(slugs || []), `_index.md`);
198
+ return options.translationStrategy === STRATEGY_FILENAME
199
+ ? path.join(...(slugs || []), `_index.${locale.code}.md`)
200
+ : path.join(...(slugs || []), `_index.md`);
187
201
  }
188
202
 
189
- if (type === TYPE_PAGE) {
203
+ if (type === TYPE_CONTENT) {
190
204
  const slugs = utils.collectParentValues(`fields.${options.fieldIdSlug}`, {
191
205
  linkField: `fields.${options.fieldIdParent}`,
192
- entry,
206
+ entry: collectEntry,
207
+ entryMap: collectEntryMap,
193
208
  });
194
209
 
195
- return path.join(
196
- ...(slugs || []),
197
- `${entry?.fields?.[options.fieldIdSlug] ?? 'unknown'}.md`
198
- );
210
+ return options.translationStrategy === STRATEGY_FILENAME
211
+ ? path.join(
212
+ ...(slugs || []),
213
+ `${collectEntry?.fields?.[options.fieldIdSlug] ?? 'unknown'}.${locale.code}.md`
214
+ )
215
+ : path.join(
216
+ ...(slugs || []),
217
+ `${collectEntry?.fields?.[options.fieldIdSlug] ?? 'unknown'}.md`
218
+ );
199
219
  }
200
220
 
201
- return path.join(id, `index.${locale.code}.md`);
221
+ return options.translationStrategy === STRATEGY_FILENAME
222
+ ? `${id}.${locale.code}.json`
223
+ : `${id}.json`;
202
224
  },
203
225
 
204
226
  async transform(transformContext, runtimeContext) {
@@ -218,7 +240,7 @@ export default (args) => {
218
240
  return undefined;
219
241
  }
220
242
 
221
- if (type === TYPE_PAGE) {
243
+ if (type === TYPE_CONTENT) {
222
244
  return {
223
245
  ...snakeCaseKeys({
224
246
  ...content,
@@ -227,13 +249,6 @@ export default (args) => {
227
249
  };
228
250
  }
229
251
 
230
- if (type === TYPE_HEADLESS) {
231
- return snakeCaseKeys({
232
- ...content,
233
- headless: true,
234
- });
235
- }
236
-
237
252
  return snakeCaseKeys(content);
238
253
  },
239
254
 
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@jungvonmatt/cssg-plugin-hugo",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
+ "typings": "./index.d.ts",
6
7
  "exports": {
7
8
  ".": "./index.js"
8
9
  },
@@ -25,9 +26,9 @@
25
26
  },
26
27
  "homepage": "https://github.com/jungvonmatt/contentful-ssg#readme",
27
28
  "dependencies": {
28
- "@jungvonmatt/contentful-ssg": "^1.0.0",
29
+ "@jungvonmatt/contentful-ssg": "^1.0.2",
29
30
  "merge-options": "^3.0.4",
30
31
  "micromatch": "^4.0.4"
31
32
  },
32
- "gitHead": "a66307559e8effa5b8961ba3ab9a784d497a9a59"
33
+ "gitHead": "c0d7b4e36ede445f92bcc059e379e8ac9584f527"
33
34
  }