Skip to content

Commit

Permalink
Merge branch 'main' into feature/hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Jun 17, 2021
2 parents 758b808 + e87d56c commit 59813cf
Show file tree
Hide file tree
Showing 14 changed files with 381 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ Initializes contentful-ssg and stores the config values in the `contentful-ssg.c
| previewAccessToken | `String` | `undefined` | Content Preview API - access token |
| spaceId | `String` | `undefined` | Contentful Space id |
| environmentId | `String` | `'master'` | Contentful Environment id |
| format | `String`\|`Function`\|`Object` | `'yaml'` | File format ( `yaml`, `md`, `json`) You can add a function returning the format or you can add a mapping object like `{yaml: [glob pattern]}` ([pattern](https://github.com/micromatch/micromatch) should match the directory) |
| format | `String`\|`Function`\|`Object` | `'yaml'` | File format ( `yaml`, `toml`, `md`, `json`) You can add a function returning the format or you can add a mapping object like `{yaml: [glob pattern]}` ([pattern](https://github.com/micromatch/micromatch) should match the directory) |
| directory | `String` | `'./content'` | Base directory for content files. |
| typeConfig | `Object` | `undefined` | Pass a map with e.g. grow's blueprint config ({<contenttypeid>: {$path: '...', $view: '...'}}) |
| preset | `String` | `undefined` | Pass `grow` to enable generator specific addons |
Expand Down
63 changes: 63 additions & 0 deletions lib/converter/index.js
@@ -0,0 +1,63 @@
const { stringifyJson, parseJson } = require('./json.js');
const { stringifyMarkdown, parseMarkdown } = require('./markdown.js');
const { stringifyToml, parseToml } = require('./toml.js');
const { stringifyYaml, parseYaml } = require('./yaml.js');

const JSON = 'json';
const YAML = 'yaml';
const MARKDOWN = 'markdown';
const TOML = 'toml';

/**
* stringify object to destination format
* @param {Object} obj Source object
* @param {String} format Destination format
* @returns {String}
*/
const stringify = (obj, format = 'yaml') => {
switch (format) {
case 'yml':
case YAML:
return stringifyYaml(obj);
case 'md':
case MARKDOWN:
return stringifyMarkdown(obj);
case JSON:
return stringifyJson(obj);
case TOML:
return stringifyToml(obj);
default:
throw new Error(`Format "${format}" is not supported`);
}
};

/**
* parse string to object
* @param {String} string Source object
* @param {String} format Destination format
* @returns {Object}
*/
const parse = (str, format = 'yaml') => {
switch (format) {
case 'yml':
case YAML:
return parseYaml(str);
case 'md':
case MARKDOWN:
const { data } = parseMarkdown(str);
return data;
case JSON:
return parseJson(str);
case TOML:
return parseToml(str);
default:
throw new Error(`Format "${format}" is not supported`);
}
};

module.exports.parse = parse;
module.exports.stringify = stringify;
module.exports.JSON = JSON;
module.exports.YAML = YAML;
module.exports.MARKDOWN = MARKDOWN;
module.exports.TOML = TOML;
20 changes: 20 additions & 0 deletions lib/converter/json.js
@@ -0,0 +1,20 @@
/**
* Convert object to json
* @param {Object} obj Source object
* @returns {String} JSON representation of source object
*/
const stringifyJson = (obj) => {
return JSON.stringify(obj, null, ' ');
};

/**
* parse json to object
* @param {String} string JSON string
* @returns {Object} parsed object
*/
const parseJson = (string) => {
return JSON.parse(string);
};

module.exports.stringifyJson = stringifyJson;
module.exports.parseJson = parseJson;
26 changes: 21 additions & 5 deletions lib/converter/markdown.js
@@ -1,16 +1,32 @@
const yaml = require('./yaml');
const { stringifyYaml, parseYaml } = require('./yaml');
const matter = require('gray-matter');

/**
* Convert object to markdown
* @param {Object} obj Source object
* @returns {String} Markdown representation of source object
*/
const convert = (obj) => {
const stringifyMarkdown = (obj, content = '') => {
let frontMatter = '';
frontMatter += `---\n`;
frontMatter += yaml.convert(obj);
frontMatter += `---\n`;
frontMatter += stringifyYaml(obj);
frontMatter += `---\n${content}`;
return frontMatter;
};

module.exports.convert = convert;
/**
* parse json to object
* @param {String} string JSON string
* @returns {Object} parsed object
*/
const parseMarkdown = (string) => {
const data = matter(string, {
engines: {
yaml: (string) => parseYaml(string),
},
});
return data;
};

module.exports.stringifyMarkdown = stringifyMarkdown;
module.exports.parseMarkdown = parseMarkdown;
22 changes: 22 additions & 0 deletions lib/converter/toml.js
@@ -0,0 +1,22 @@
const TOML = require('@iarna/toml');

/**
* Convert object to toml
* @param {Object} obj Source object
* @returns {String} toml representation of source object
*/
const stringifyToml = (obj) => {
return TOML.stringify(obj);
};

/**
* parse toml to object
* @param {String} string toml string
* @returns {Object} parsed object
*/
const parseToml = (string) => {
return TOML.parse(string);
};

module.exports.stringifyToml = stringifyToml;
module.exports.parseToml = parseToml;
17 changes: 15 additions & 2 deletions lib/converter/yaml.js
Expand Up @@ -2,6 +2,7 @@ const yaml = require('js-yaml');

const getPredicate = (type) => (data) => typeof data === 'string' && data.startsWith(`${type} `);
const getRepresent = (type) => (data) => data.replace(`${type} `, '');
const getConstruct = (type) => (data) => `${type} ${data}`;

const growYamlConstructors = ['!g.csv', '!g.doc', '!g.json', '!g.static', '!g.string', '!g.url', '!g.yaml'];

Expand All @@ -11,6 +12,7 @@ const growYamlTypes = growYamlConstructors.map(
kind: 'scalar',
predicate: getPredicate(type),
represent: getRepresent(type),
construct: getConstruct(type),
})
);

Expand All @@ -19,9 +21,20 @@ const growYamlTypes = growYamlConstructors.map(
* @param {Object} obj Source object
* @returns {String} Yaml representation of source object
*/
const convert = (obj) => {
const stringifyYaml = (obj) => {
const GROW_SCHEMA = yaml.DEFAULT_SCHEMA.extend(growYamlTypes);
return yaml.dump(obj, { schema: GROW_SCHEMA });
};

module.exports.convert = convert;
/**
* parse yaml to object
* @param {String} string yaml string
* @returns {Object} parsed object
*/
const parseYaml = (obj) => {
const GROW_SCHEMA = yaml.DEFAULT_SCHEMA.extend(growYamlTypes);
return yaml.load(obj, { schema: GROW_SCHEMA });
};

module.exports.stringifyYaml = stringifyYaml;
module.exports.parseYaml = parseYaml;
28 changes: 3 additions & 25 deletions lib/dump.js
Expand Up @@ -9,8 +9,7 @@ const fs = require('fs-extra');
const { getContent, getContentTypeId, getContentId, getFieldSettings, convertToMap } = require('./contentful');
const { mapAsync, forEachAsync } = require('./array');
const { groupBy, snakeCaseKeys, getContentTypeDirectory, collect } = require('./utils');
const yaml = require('./converter/yaml');
const markdown = require('./converter/markdown');
const { stringify, YAML } = require('./converter');
const { localizeEntry } = require('./transform/localize');
const { mapEntry } = require('./transform/mapper');
const { mapBuildInFields, addBlueprints, mapGrowLink } = require('./presets/grow');
Expand All @@ -19,28 +18,7 @@ const globby = require('globby');
const STATUS_SUCCES = 'success';
const STATUS_ERROR = 'error';
const STATUS_SKIPPED = 'skipped';
const DEFAULT_FORMAT = 'yaml';

/**
* Convert object to destination format
* @param {Object} obj Source object
* @param {String} format Destination format
* @returns {String}
*/
const convert = (obj, format = 'yaml') => {
switch (format) {
case 'yaml':
case 'yml':
return yaml.convert(obj);
case 'md':
case 'markdown':
return markdown.convert(obj);
case 'json':
return JSON.stringify(obj, null, ' ');
default:
throw new Error(`Format "${format}" is not supported`);
}
};
const DEFAULT_FORMAT = YAML;

/**
* Log dump stats
Expand Down Expand Up @@ -277,7 +255,7 @@ const dump = async (config) => {
])[0];
}

const content = await convert(data, format);
const content = await stringify(data, format);

const file =
typeof mapFilename === 'function'
Expand Down
70 changes: 66 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@contentful/rich-text-html-renderer": "^14.1.2",
"@contentful/rich-text-types": "^14.1.2",
"@iarna/toml": "^2.2.5",
"chalk": "^4.1.1",
"commander": "^7.2.0",
"contentful": "^8.3.5",
Expand All @@ -35,6 +36,7 @@
"find-up": "^5.0.0",
"fs-extra": "^10.0.0",
"globby": "^11.0.3",
"gray-matter": "^4.0.3",
"ignore": "^5.1.8",
"inquirer": "^8.0.0",
"js-yaml": "^4.1.0",
Expand Down

0 comments on commit 59813cf

Please sign in to comment.