├── .babelrc ├── .circleci └── config.yml ├── .eslintrc ├── .gitignore ├── .npmrc ├── .nvmrc ├── .prettierignore ├── .prettierrc ├── LICENSE.txt ├── README.md ├── bin └── hes.js ├── content ├── test-toml.md └── test-yaml.md ├── hero.png ├── jest.config.js ├── lib └── hes.js ├── package.json ├── public ├── elasticsearch-api-constructor-all.json ├── elasticsearch-api-setters-toml.json ├── elasticsearch-api-setters-yaml.json ├── elasticsearch-toml.json └── elasticsearch-yaml.json ├── publish.sh ├── src ├── bin │ └── hes.js └── lib │ └── hes.js ├── test ├── hes.api.spec.js └── hes.bin.spec.js ├── usage.gif └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "env", 4 | "stage-0" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | jobs: 4 | build: 5 | docker: 6 | - image: circleci/node:10.8 7 | 8 | working_directory: ~/hugo-elasticsearch 9 | 10 | environment: 11 | - NODE_ENV: test 12 | 13 | steps: 14 | - checkout 15 | 16 | - restore_cache: 17 | keys: 18 | - dependencies-{{ checksum "yarn.lock" }} 19 | 20 | - run: yarn --frozen-lockfile 21 | 22 | - save_cache: 23 | paths: 24 | - node_modules 25 | key: dependencies-{{ checksum "yarn.lock" }} 26 | 27 | - run: yarn test 28 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true, 4 | "es6": true, 5 | "jest": true 6 | }, 7 | "parser": "babel-eslint", 8 | "parserOptions": { 9 | "sourceType": "module" 10 | }, 11 | "extends": [ 12 | "eslint:recommended" 13 | ], 14 | "rules": { 15 | "no-cond-assign": "off", 16 | "no-case-declarations": "off", 17 | "default-case": "off", 18 | "no-unused-vars": "warn", 19 | "no-console": "warn", 20 | "indent": "off", 21 | "linebreak-style": [ 22 | "error", 23 | "unix" 24 | ], 25 | "quotes": [ 26 | "warn", 27 | "double" 28 | ] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### IDE 2 | .idea 3 | *.iml 4 | 5 | ### Node template 6 | # Logs 7 | logs 8 | *.log 9 | npm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # Typescript v1 declaration files 45 | typings/ 46 | 47 | # Optional npm cache directory 48 | .npm 49 | 50 | # Optional eslint cache 51 | .eslintcache 52 | 53 | # Optional REPL history 54 | .node_repl_history 55 | 56 | # Output of 'npm pack' 57 | *.tgz 58 | 59 | # Yarn Integrity file 60 | .yarn-integrity 61 | 62 | # dotenv environment variables file 63 | #.env 64 | 65 | # next.js build output 66 | .next 67 | 68 | dist -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v10.8.0 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | *.log 4 | /bin 5 | /lib 6 | build 7 | node_modules 8 | package.json 9 | 10 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 140 3 | } 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright (c) 2018 - Travis Clarke - https://www.travismclarke.com 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Hugo-Elasticsearch (HES)](https://blog.travismclarke.com/project/hugo-elasticsearch/) 2 | 3 | [![npm-badge](https://img.shields.io/npm/v/hugo-elasticsearch.svg)](https://www.npmjs.com/package/hugo-elasticsearch) 4 | [![codacy-badge](https://api.codacy.com/project/badge/Grade/1ce2505fd16c4e5ab284c7b36b666a08)](https://www.codacy.com/app/clarketm/hugo-elasticsearch?utm_source=github.com&utm_medium=referral&utm_content=clarketm/hugo-elasticsearch&utm_campaign=Badge_Grade) 5 | [![circleci-badge](https://circleci.com/gh/clarketm/hugo-elasticsearch.svg?style=shield)](https://circleci.com/gh/clarketm/hugo-elasticsearch) 6 | 7 | Generate [Elasticsearch](https://www.elastic.co/products/elasticsearch) indexes for [Hugo](https://gohugo.io/) static sites by parsing front matter. 8 | 9 |
10 |
11 | 12 |

13 |
14 | 15 | ## Installation 16 | 17 | ### Install with npm 18 | ```bash 19 | $ npm install hugo-elasticsearch 20 | ``` 21 | 22 | ## Demo 23 | 24 | ![usage demo](https://github.com/clarketm/hugo-elasticsearch/blob/master/usage.gif) 25 | 26 | ## Usage 27 | 28 | ### CLI 29 | ```bash 30 | 31 | NAME: 32 | hugo-elasticsearch 33 | hes (alias) 34 | 35 | SYNOPSIS: 36 | hes [ opts... ] 37 | 38 | DESCRIPTION: 39 | Generate Elasticsearch indexes from Hugo front matter. 40 | 41 | OPTIONS: 42 | -i, --input path Input path. (default: "content/**") 43 | -o, --output path Output path. (default: "public/elasticsearch.json") 44 | -l, --language lang Language [toml | yaml]. (default: "toml") 45 | -d, --delimiter delim Delimiter [toml: +++ | yaml: ---]. (optional) 46 | -n, --name name Index name. (optional) 47 | 48 | ``` 49 | 50 | ##### Long form 51 | ```bash 52 | $ hugo-elasticsearch \ 53 | --input "content/**" \ 54 | --output "public/elasticsearch.json" \ 55 | --language "toml" \ 56 | --delimiter "+++" \ 57 | --index-name "posts" 58 | ``` 59 | 60 | ##### Short form 61 | ```bash 62 | $ hes \ 63 | -i "content/**" \ 64 | -o "public/elasticsearch.json" \ 65 | -l "toml" \ 66 | -d "+++" \ 67 | -n "posts" 68 | ``` 69 | 70 | ### NPM Scripts 71 | ```javascript 72 | ... 73 | "scripts": { 74 | "index": "hes -i 'content/**' -o 'public/elasticsearch.json'" 75 | "index:toml": "hes -i 'content/toml/**' -o 'public/toml/elasticsearch.json' -l 'toml' -d '+++'" 76 | "index:yaml": "hes -i 'content/yaml/**' -o 'public/yaml/elasticsearch.json' -l 'yaml' -d '---'" 77 | }, 78 | ... 79 | ``` 80 | 81 | 82 | ### API 83 | ```javascript 84 | const hes = require('hugo-elasticsearch'); 85 | 86 | const Indexer = new hes({ 87 | input: 'content/blog/**', 88 | output: 'public/static/elasticsearch.json', 89 | language: 'yaml', 90 | delimiter: '---', 91 | indexName: 'posts' 92 | }); 93 | 94 | // Create index 95 | Indexer.index() 96 | 97 | // Setters 98 | Indexer.setInput('content/blog/**'); 99 | Indexer.setOutput('public/static/elasticsearch.json'); 100 | Indexer.setLanguage('yaml'); 101 | Indexer.setDelimiter('---'); 102 | Indexer.setIndexName('posts'); 103 | ``` 104 | 105 | ## Example 106 | 107 | #### 1. Create a directory named `content`. 108 | ```bash 109 | $ mkdir 'content' 110 | ``` 111 | 112 | #### 2. Create a markdown file with `toml` front matter in a file named `content/test-toml.md`. 113 | ```markdown 114 | $ cat > 'content/test-toml.md' < { 166 | return new Promise((resolve, reject) => { 167 | let lines = []; 168 | 169 | fs.createReadStream("./public/elasticsearch.json") 170 | .pipe(ndjson.parse()) 171 | .on("data", line => lines.push(line)) 172 | .on("end", () => resolve(lines)) 173 | .on("error", err => reject(err)); 174 | }); 175 | }; 176 | 177 | // Perform the bulk index operations in a single API call. 178 | const bulkUpload = async () => { 179 | const json = await this.fetchBulkJson(); 180 | return await client.bulk({ body: json }); 181 | }; 182 | ``` 183 | 184 | **[Java](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html), [Python](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/index.html), [Ruby](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/index.html), ...** 185 | > Although the bulk upload examples above are only for cUrl and JavaScript, this format will work seamlessly with **any** one of the *numerous* Elasticsearch [clients](https://www.elastic.co/guide/en/elasticsearch/client/index.html). 186 | 187 | #### 5. You content is now successfully indexed in Elasticsearch 👍. Happy elastic searching! 188 | 189 | > Refer to the [`content`](content) directory in the root of *this* project for examples of both *yaml* and *toml* content (i.e. `.md` files). 190 | 191 | > Refer to the [`public`](public) directory in the root of *this* project for examples of ndjson files (i.e. Elasticsearch index files) generated from both *yaml* and *toml* content. 192 | 193 | ## Sites using hugo-elasticsearch 194 | * https://blog.travismclarke.com/ 195 | 196 | ## License 197 | Apache-2.0 © [Travis Clarke](https://www.travismclarke.com/) 198 | -------------------------------------------------------------------------------- /bin/hes.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | "use strict"; 3 | 4 | var _ = require("../"); 5 | 6 | var _2 = _interopRequireDefault(_); 7 | 8 | var _commander = require("commander"); 9 | 10 | var _commander2 = _interopRequireDefault(_commander); 11 | 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 13 | 14 | var _require = require("../package.json"), 15 | version = _require.version; 16 | 17 | _commander2.default.version(version).option("-i, --input ", "Input path.", "content/**").option("-o, --output ", "Output path.", "public/elasticsearch.json").option("-l, --language ", "Language [toml | yaml].", "toml").option("-d, --delimiter ", "Delimiter [toml: +++ | yaml: ---]. (optional)").option("-n, --index-name ", "Index name. (optional)").parse(process.argv); 18 | 19 | var input = _commander2.default.input, 20 | output = _commander2.default.output, 21 | language = _commander2.default.language, 22 | delimiter = _commander2.default.delimiter, 23 | indexName = _commander2.default.indexName; 24 | 25 | 26 | new _2.default({ input: input, output: output, language: language, delimiter: delimiter, indexName: indexName }).index(); -------------------------------------------------------------------------------- /content/test-toml.md: -------------------------------------------------------------------------------- 1 | +++ 2 | title = "Sample title" 3 | description = "Sample description" 4 | tags = [ "tag1" ] 5 | +++ 6 | 7 | # Sample content header 8 | Sample content body -------------------------------------------------------------------------------- /content/test-yaml.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Sample title" 3 | description: "Sample description" 4 | tags: 5 | - tag1 6 | --- 7 | 8 | # Sample content header 9 | Sample content body -------------------------------------------------------------------------------- /hero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clarketm/hugo-elasticsearch/2091c0d90ffbe3eef4634e4b4a4cfbf89b2eef7f/hero.png -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverageFrom: ["lib/**/*.{js,jsx,mjs}"], 3 | testMatch: ["/**/?(*)(spec|test).{js,jsx,mjs}"], 4 | testEnvironment: "node", 5 | moduleFileExtensions: ["web.js", "mjs", "js", "json", "web.jsx", "jsx", "node"] 6 | }; 7 | -------------------------------------------------------------------------------- /lib/hes.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 4 | 5 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 6 | 7 | var _fs = require("fs"); 8 | 9 | var _fs2 = _interopRequireDefault(_fs); 10 | 11 | var _glob = require("glob"); 12 | 13 | var _glob2 = _interopRequireDefault(_glob); 14 | 15 | var _grayMatter = require("gray-matter"); 16 | 17 | var _grayMatter2 = _interopRequireDefault(_grayMatter); 18 | 19 | var _toml = require("toml"); 20 | 21 | var _toml2 = _interopRequireDefault(_toml); 22 | 23 | var _removeMarkdown = require("remove-markdown"); 24 | 25 | var _removeMarkdown2 = _interopRequireDefault(_removeMarkdown); 26 | 27 | var _striptags = require("striptags"); 28 | 29 | var _striptags2 = _interopRequireDefault(_striptags); 30 | 31 | var _path = require("path"); 32 | 33 | var _path2 = _interopRequireDefault(_path); 34 | 35 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 36 | 37 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 38 | 39 | var HugoElasticsearch = function () { 40 | //////////////////////////////////////////// 41 | // Constructor 42 | //////////////////////////////////////////// 43 | 44 | function HugoElasticsearch() { 45 | var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; 46 | 47 | _classCallCheck(this, HugoElasticsearch); 48 | 49 | this.indexMeta = { index: {} }; 50 | 51 | // DEFAULTS 52 | this.input = config.input || "content/**"; 53 | this.output = config.output || "public/elasticsearch.json"; 54 | this.language = config.language || "toml"; 55 | this.delimiter = config.delimiter; 56 | this.indexName = config.indexName; 57 | } 58 | 59 | //////////////////////////////////////////// 60 | // Setters 61 | //////////////////////////////////////////// 62 | 63 | _createClass(HugoElasticsearch, [{ 64 | key: "setInput", 65 | value: function setInput(input) { 66 | this.input = input; 67 | } 68 | }, { 69 | key: "setOutput", 70 | value: function setOutput(output) { 71 | this.output = output; 72 | } 73 | }, { 74 | key: "setLanguage", 75 | value: function setLanguage(language) { 76 | this.language = language; 77 | } 78 | }, { 79 | key: "setDelimiter", 80 | value: function setDelimiter(delimiter) { 81 | this.delimiter = delimiter; 82 | } 83 | }, { 84 | key: "setIndexName", 85 | value: function setIndexName(indexName) { 86 | this.indexName = indexName; 87 | } 88 | }, { 89 | key: "setLanguageConfig", 90 | value: function setLanguageConfig(language, delimiter) { 91 | switch (true) { 92 | case language.toLowerCase() === "yaml": 93 | this.delimiter = delimiter || "---"; 94 | this.languageConfig = { 95 | delims: this.delimiter, 96 | lang: "yaml" 97 | }; 98 | break; 99 | default: 100 | case language.toLowerCase() === "toml": 101 | this.delimiter = delimiter || "+++"; 102 | this.languageConfig = { 103 | delims: this.delimiter, 104 | lang: "toml", 105 | engines: { 106 | toml: _toml2.default.parse.bind(_toml2.default) 107 | } 108 | }; 109 | break; 110 | } 111 | } 112 | }, { 113 | key: "setIndexMetaId", 114 | value: function setIndexMetaId(id) { 115 | this.indexMeta.index._id = id || this.id++; 116 | } 117 | }, { 118 | key: "setIndexMetaIndex", 119 | value: function setIndexMetaIndex(indexName) { 120 | this.indexMeta.index._index = indexName; 121 | } 122 | }, { 123 | key: "writeIndexStream", 124 | value: function writeIndexStream(input, output, indexMeta) { 125 | this.id = 1; 126 | this.list = []; 127 | this.baseDir = _path2.default.dirname(this.input); 128 | 129 | if (this.indexName) this.setIndexMetaIndex(this.indexName); 130 | this.setLanguageConfig(this.language, this.delimiter); 131 | this.readInputDirectory(input); 132 | 133 | if (this.list.length <= 0) { 134 | global.console.error("No content found for specified input path: \"" + this.input + "\""); 135 | global.process.exit(1); 136 | } 137 | 138 | this.createOutputDirectory(output); 139 | this.stream = _fs2.default.createWriteStream(output); 140 | for (var i = 0; i < this.list.length; i++) { 141 | this.setIndexMetaId(); 142 | this.stream.write(JSON.stringify(indexMeta)); 143 | this.stream.write("\n"); 144 | this.stream.write(JSON.stringify(this.list[i])); 145 | this.stream.write("\n"); 146 | } 147 | this.stream.write("\n"); 148 | this.stream.end(); 149 | } 150 | 151 | //////////////////////////////////////////// 152 | // Methods 153 | //////////////////////////////////////////// 154 | 155 | }, { 156 | key: "createOutputDirectory", 157 | value: function createOutputDirectory(output) { 158 | var dir = _path2.default.dirname(output); 159 | if (!_fs2.default.existsSync(dir)) _fs2.default.mkdirSync(dir); 160 | } 161 | }, { 162 | key: "readInputDirectory", 163 | value: function readInputDirectory(path) { 164 | var files = _glob2.default.sync(path); 165 | 166 | var _iteratorNormalCompletion = true; 167 | var _didIteratorError = false; 168 | var _iteratorError = undefined; 169 | 170 | try { 171 | for (var _iterator = files[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { 172 | var file = _step.value; 173 | 174 | var stats = _fs2.default.lstatSync(file); 175 | if (!stats.isDirectory()) this.readInputFile(file); 176 | } 177 | } catch (err) { 178 | _didIteratorError = true; 179 | _iteratorError = err; 180 | } finally { 181 | try { 182 | if (!_iteratorNormalCompletion && _iterator.return) { 183 | _iterator.return(); 184 | } 185 | } finally { 186 | if (_didIteratorError) { 187 | throw _iteratorError; 188 | } 189 | } 190 | } 191 | } 192 | }, { 193 | key: "readInputFile", 194 | value: function readInputFile(filePath) { 195 | var ext = _path2.default.extname(filePath); 196 | var meta = _grayMatter2.default.read(filePath, this.languageConfig); 197 | 198 | // Check if is a draft 199 | if (meta.data.draft) return; 200 | 201 | var tags = meta.data.tags || []; 202 | var content = void 0; 203 | 204 | // Content 205 | if (ext === ".md") content = (0, _removeMarkdown2.default)(meta.content);else content = (0, _striptags2.default)(meta.content); 206 | 207 | // Uri 208 | var uri = ("/" + filePath.substring(0, filePath.lastIndexOf("."))).replace(this.baseDir + "/", ""); 209 | 210 | if (meta.data.slug) uri = _path2.default.dirname(uri) + meta.data.slug; 211 | 212 | if (meta.data.url) uri = meta.data.url; 213 | 214 | this.list.push(_extends({}, meta.data, { uri: uri, content: content, tags: tags })); 215 | } 216 | }, { 217 | key: "index", 218 | value: function index() { 219 | this.writeIndexStream(this.input, this.output, this.indexMeta); 220 | } 221 | }]); 222 | 223 | return HugoElasticsearch; 224 | }(); 225 | 226 | module.exports = HugoElasticsearch; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hugo-elasticsearch", 3 | "version": "1.1.4", 4 | "description": "Generate elasticsearch indices for Hugo static sites by parsing front matter.", 5 | "main": "./lib/hes.js", 6 | "bin": { 7 | "hes": "./bin/hes.js", 8 | "hugo-elasticsearch": "./bin/hes.js" 9 | }, 10 | "files": [ 11 | "bin", 12 | "lib", 13 | "README.md", 14 | "LICENSE.txt" 15 | ], 16 | "directories": { 17 | "bin": "./bin", 18 | "lib": "./lib" 19 | }, 20 | "scripts": { 21 | "index": "node ./bin/hes -i './content/**' -o './public/elasticsearch.json' -l 'toml' -d '+++'", 22 | "build": "shx rm -rf ./bin ./lib ; babel ./src --out-dir ./", 23 | "lint": "eslint --fix ./src ./test", 24 | "prettier": "prettier --write '**/*.js'", 25 | "pretest": "yarn build", 26 | "test": "jest", 27 | "test:watch": "jest --watch", 28 | "test:cov": "jest --coverage", 29 | "prep": "yarn lint && yarn prettier && yarn test", 30 | "prepublishOnly": "yarn prep && bash ./publish.sh" 31 | }, 32 | "husky": { 33 | "hooks": { 34 | "pre-commit": "lint-staged" 35 | } 36 | }, 37 | "lint-staged": { 38 | "./src/*.js": [ 39 | "eslint --fix", 40 | "git add" 41 | ] 42 | }, 43 | "keywords": [ 44 | "elasticsearch", 45 | "hugo", 46 | "index", 47 | "indexer", 48 | "toml", 49 | "yaml" 50 | ], 51 | "author": "Travis Clarke (https://www.travismclarke.com/)", 52 | "repository": "github:clarketm/hugo-elasticsearch", 53 | "homepage": "https://github.com/clarketm/hugo-elasticsearch#readme", 54 | "license": "Apache-2.0", 55 | "dependencies": { 56 | "commander": "^2.14.1", 57 | "glob": "^7.1.2", 58 | "gray-matter": "^4.0.1", 59 | "remove-markdown": "^0.3.0", 60 | "striptags": "^3.1.1", 61 | "toml": "^2.3.3" 62 | }, 63 | "devDependencies": { 64 | "@types/jest": "^23.3.1", 65 | "@types/shelljs": "^0.8.0", 66 | "babel-cli": "^6.26.0", 67 | "babel-eslint": "^8.2.2", 68 | "babel-polyfill": "^6.26.0", 69 | "babel-preset-env": "^1.6.1", 70 | "babel-preset-stage-0": "^6.24.1", 71 | "eslint": "^4.18.2", 72 | "husky": "^0.15.0-rc.9", 73 | "jest": "^23.5.0", 74 | "lint-staged": "^7.0.0", 75 | "mock-fs": "^4.7.0", 76 | "prettier": "^1.14.2", 77 | "shelljs": "^0.8.2", 78 | "shx": "^0.2.2", 79 | "yeoman-assert": "^3.1.1" 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /public/elasticsearch-api-constructor-all.json: -------------------------------------------------------------------------------- 1 | {"index":{"_index":"index","_id":1}} 2 | {"title":"Sample title","description":"Sample description","tags":["tag1"],"uri":"/test-yaml","content":"\nSample content header\nSample content body"} 3 | 4 | -------------------------------------------------------------------------------- /public/elasticsearch-api-setters-toml.json: -------------------------------------------------------------------------------- 1 | {"index":{"_index":"index","_id":1}} 2 | {"title":"Sample title","description":"Sample description","tags":["tag1"],"uri":"/test-toml","content":"\nSample content header\nSample content body"} 3 | 4 | -------------------------------------------------------------------------------- /public/elasticsearch-api-setters-yaml.json: -------------------------------------------------------------------------------- 1 | {"index":{"_index":"index","_id":1}} 2 | {"title":"Sample title","description":"Sample description","tags":["tag1"],"uri":"/test-yaml","content":"\nSample content header\nSample content body"} 3 | 4 | -------------------------------------------------------------------------------- /public/elasticsearch-toml.json: -------------------------------------------------------------------------------- 1 | {"index":{"_index":"indexx","_id":1}} 2 | {"title":"Sample title","description":"Sample description","tags":["tag1"],"uri":"/test-toml","content":"\nSample content header\nSample content body"} 3 | 4 | -------------------------------------------------------------------------------- /public/elasticsearch-yaml.json: -------------------------------------------------------------------------------- 1 | {"index":{"_index":"indexx","_id":1}} 2 | {"title":"Sample title","description":"Sample description","tags":["tag1"],"uri":"/test-yaml","content":"\nSample content header\nSample content body"} 3 | 4 | -------------------------------------------------------------------------------- /publish.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if git diff --exit-code --quiet -- . ':(exclude)package.json'; then 4 | echo 'Clean working tree' 5 | else 6 | echo 'Dirty working tree' 7 | exit 1 8 | fi 9 | -------------------------------------------------------------------------------- /src/bin/hes.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import hes from "../"; 4 | const { version } = require("../package.json"); 5 | import commander from "commander"; 6 | 7 | commander 8 | .version(version) 9 | .option("-i, --input ", "Input path.", "content/**") 10 | .option("-o, --output ", "Output path.", "public/elasticsearch.json") 11 | .option("-l, --language ", "Language [toml | yaml].", "toml") 12 | .option("-d, --delimiter ", "Delimiter [toml: +++ | yaml: ---]. (optional)") 13 | .option("-n, --index-name ", "Index name. (optional)") 14 | .parse(process.argv); 15 | 16 | const { input, output, language, delimiter, indexName } = commander; 17 | 18 | new hes({ input, output, language, delimiter, indexName }).index(); 19 | -------------------------------------------------------------------------------- /src/lib/hes.js: -------------------------------------------------------------------------------- 1 | import fs from "fs"; 2 | import glob from "glob"; 3 | import matter from "gray-matter"; 4 | import toml from "toml"; 5 | import removeMd from "remove-markdown"; 6 | import striptags from "striptags"; 7 | import path from "path"; 8 | 9 | class HugoElasticsearch { 10 | //////////////////////////////////////////// 11 | // Constructor 12 | //////////////////////////////////////////// 13 | 14 | constructor(config = {}) { 15 | this.indexMeta = { index: {} }; 16 | 17 | // DEFAULTS 18 | this.input = config.input || "content/**"; 19 | this.output = config.output || "public/elasticsearch.json"; 20 | this.language = config.language || "toml"; 21 | this.delimiter = config.delimiter; 22 | this.indexName = config.indexName; 23 | } 24 | 25 | //////////////////////////////////////////// 26 | // Setters 27 | //////////////////////////////////////////// 28 | 29 | setInput(input) { 30 | this.input = input; 31 | } 32 | 33 | setOutput(output) { 34 | this.output = output; 35 | } 36 | 37 | setLanguage(language) { 38 | this.language = language; 39 | } 40 | 41 | setDelimiter(delimiter) { 42 | this.delimiter = delimiter; 43 | } 44 | 45 | setIndexName(indexName) { 46 | this.indexName = indexName; 47 | } 48 | 49 | setLanguageConfig(language, delimiter) { 50 | switch (true) { 51 | case language.toLowerCase() === "yaml": 52 | this.delimiter = delimiter || "---"; 53 | this.languageConfig = { 54 | delims: this.delimiter, 55 | lang: "yaml" 56 | }; 57 | break; 58 | default: 59 | case language.toLowerCase() === "toml": 60 | this.delimiter = delimiter || "+++"; 61 | this.languageConfig = { 62 | delims: this.delimiter, 63 | lang: "toml", 64 | engines: { 65 | toml: toml.parse.bind(toml) 66 | } 67 | }; 68 | break; 69 | } 70 | } 71 | 72 | setIndexMetaId(id) { 73 | this.indexMeta.index._id = id || this.id++; 74 | } 75 | 76 | setIndexMetaIndex(indexName) { 77 | this.indexMeta.index._index = indexName; 78 | } 79 | 80 | writeIndexStream(input, output, indexMeta) { 81 | this.id = 1; 82 | this.list = []; 83 | this.baseDir = path.dirname(this.input); 84 | 85 | if (this.indexName) this.setIndexMetaIndex(this.indexName); 86 | this.setLanguageConfig(this.language, this.delimiter); 87 | this.readInputDirectory(input); 88 | 89 | if (this.list.length <= 0) { 90 | global.console.error(`No content found for specified input path: "${this.input}"`); 91 | global.process.exit(1); 92 | } 93 | 94 | this.createOutputDirectory(output); 95 | this.stream = fs.createWriteStream(output); 96 | for (let i = 0; i < this.list.length; i++) { 97 | this.setIndexMetaId(); 98 | this.stream.write(JSON.stringify(indexMeta)); 99 | this.stream.write("\n"); 100 | this.stream.write(JSON.stringify(this.list[i])); 101 | this.stream.write("\n"); 102 | } 103 | this.stream.write("\n"); 104 | this.stream.end(); 105 | } 106 | 107 | //////////////////////////////////////////// 108 | // Methods 109 | //////////////////////////////////////////// 110 | 111 | createOutputDirectory(output) { 112 | const dir = path.dirname(output); 113 | if (!fs.existsSync(dir)) fs.mkdirSync(dir); 114 | } 115 | 116 | readInputDirectory(path) { 117 | const files = glob.sync(path); 118 | 119 | for (let file of files) { 120 | const stats = fs.lstatSync(file); 121 | if (!stats.isDirectory()) this.readInputFile(file); 122 | } 123 | } 124 | 125 | readInputFile(filePath) { 126 | const ext = path.extname(filePath); 127 | const meta = matter.read(filePath, this.languageConfig); 128 | 129 | // Check if is a draft 130 | if (meta.data.draft) return; 131 | 132 | const tags = meta.data.tags || []; 133 | let content; 134 | 135 | // Content 136 | if (ext === ".md") content = removeMd(meta.content); 137 | else content = striptags(meta.content); 138 | 139 | // Uri 140 | let uri = `/${filePath.substring(0, filePath.lastIndexOf("."))}`.replace(`${this.baseDir}/`, ""); 141 | 142 | if (meta.data.slug) uri = path.dirname(uri) + meta.data.slug; 143 | 144 | if (meta.data.url) uri = meta.data.url; 145 | 146 | this.list.push({ ...meta.data, uri, content, tags }); 147 | } 148 | 149 | index() { 150 | this.writeIndexStream(this.input, this.output, this.indexMeta); 151 | } 152 | } 153 | 154 | module.exports = HugoElasticsearch; 155 | -------------------------------------------------------------------------------- /test/hes.api.spec.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | const mockfs = require("mock-fs"); 3 | 4 | global.console.error = jest.fn(); 5 | global.process.exit = jest.fn(); 6 | 7 | describe("hes api", () => { 8 | const root = fs.realpathSync(process.cwd()); 9 | const hes = require(root); 10 | 11 | describe("#constructor (language) ~ toml", () => { 12 | const input = "content/**"; 13 | const output = "public/elasticsearch.json"; 14 | const language = "toml"; 15 | const delimiter = "+++"; 16 | const indexName = "index"; 17 | let Indexer; 18 | 19 | beforeAll(() => { 20 | process.chdir(root); 21 | 22 | mockfs({ "public/elasticsearch.json": mockfs.file() }); 23 | Indexer = new hes({ language, indexName }); 24 | Indexer.index(); 25 | mockfs.restore(); 26 | }); 27 | 28 | it("should set `input` property", () => { 29 | expect(Indexer.input).toEqual(input); 30 | }); 31 | 32 | it("should set `output` property", () => { 33 | expect(Indexer.output).toEqual(output); 34 | }); 35 | 36 | it("should set `language` property", () => { 37 | expect(Indexer.language).toEqual(language); 38 | }); 39 | 40 | it("should set `delimiter` property", () => { 41 | expect(Indexer.delimiter).toEqual(delimiter); 42 | }); 43 | 44 | it("should set `indexName` property", () => { 45 | expect(Indexer.indexName).toEqual(indexName); 46 | }); 47 | }); 48 | 49 | describe("#constructor (language) ~ yaml", () => { 50 | const input = "content/**"; 51 | const output = "public/elasticsearch.json"; 52 | const language = "yaml"; 53 | const delimiter = "---"; 54 | const indexName = "index"; 55 | let Indexer; 56 | 57 | beforeAll(() => { 58 | process.chdir(root); 59 | 60 | mockfs({ "public/elasticsearch.json": mockfs.file() }); 61 | Indexer = new hes({ language, indexName }); 62 | Indexer.index(); 63 | mockfs.restore(); 64 | }); 65 | 66 | it("should set `input` property", () => { 67 | expect(Indexer.input).toEqual(input); 68 | }); 69 | 70 | it("should set `output` property", () => { 71 | expect(Indexer.output).toEqual(output); 72 | }); 73 | 74 | it("should set `language` property", () => { 75 | expect(Indexer.language).toEqual(language); 76 | }); 77 | 78 | it("should set `delimiter` property", () => { 79 | expect(Indexer.delimiter).toEqual(delimiter); 80 | }); 81 | 82 | it("should set `indexName` property", () => { 83 | expect(Indexer.indexName).toEqual(indexName); 84 | }); 85 | }); 86 | 87 | describe("#constructor (input, output, language, delimiter)", () => { 88 | const input = "content/test-yaml.md"; 89 | const output = "public/elasticsearch-api-constructor-all.json"; 90 | const language = "yaml"; 91 | const delimiter = "---"; 92 | const indexName = "index"; 93 | let Indexer; 94 | 95 | beforeAll(() => { 96 | process.chdir(root); 97 | 98 | Indexer = new hes({ input, output, language, delimiter, indexName }); 99 | Indexer.index(input, output, language, delimiter); 100 | }); 101 | 102 | it("should set `input` property", () => { 103 | expect(Indexer.input).toEqual(input); 104 | }); 105 | 106 | it("should set `output` property", () => { 107 | expect(Indexer.output).toEqual(output); 108 | }); 109 | 110 | it("should set `language` property", () => { 111 | expect(Indexer.language).toEqual(language); 112 | }); 113 | 114 | it("should set `delimiter` property", () => { 115 | expect(Indexer.delimiter).toEqual(delimiter); 116 | }); 117 | 118 | it("should set `indexName` property", () => { 119 | expect(Indexer.indexName).toEqual(indexName); 120 | }); 121 | }); 122 | 123 | describe("#setters (input, output, language, delimiter) ~ toml", () => { 124 | const input = "content/test-toml.md"; 125 | const output = "public/elasticsearch-api-setters-toml.json"; 126 | const language = "toml"; 127 | const delimiter = "+++"; 128 | const indexName = "index"; 129 | let Indexer; 130 | 131 | beforeAll(() => { 132 | process.chdir(root); 133 | 134 | Indexer = new hes(); 135 | Indexer.setInput(input); 136 | Indexer.setOutput(output); 137 | Indexer.setLanguage(language); 138 | Indexer.setDelimiter(delimiter); 139 | Indexer.setIndexName(indexName); 140 | Indexer.index(); 141 | }); 142 | 143 | it("should set `input` property", () => { 144 | expect(Indexer.input).toEqual(input); 145 | }); 146 | 147 | it("should set `output` property", () => { 148 | expect(Indexer.output).toEqual(output); 149 | }); 150 | 151 | it("should set `language` property", () => { 152 | expect(Indexer.language).toEqual(language); 153 | }); 154 | 155 | it("should set `delimiter` property", () => { 156 | expect(Indexer.delimiter).toEqual(delimiter); 157 | }); 158 | 159 | it("should set `indexName` property", () => { 160 | expect(Indexer.indexName).toEqual(indexName); 161 | }); 162 | }); 163 | 164 | describe("#setters (input, output, language, delimiter) ~ yaml", () => { 165 | const input = "content/test-yaml.md"; 166 | const output = "public/elasticsearch-api-setters-yaml.json"; 167 | const language = "yaml"; 168 | const delimiter = "---"; 169 | const indexName = "index"; 170 | let Indexer; 171 | 172 | beforeAll(() => { 173 | process.chdir(root); 174 | 175 | Indexer = new hes(); 176 | Indexer.setInput(input); 177 | Indexer.setOutput(output); 178 | Indexer.setLanguage(language); 179 | Indexer.setDelimiter(delimiter); 180 | Indexer.setIndexName(indexName); 181 | Indexer.index(); 182 | }); 183 | 184 | it("should set `input` property", () => { 185 | expect(Indexer.input).toEqual(input); 186 | }); 187 | 188 | it("should set `output` property", () => { 189 | expect(Indexer.output).toEqual(output); 190 | }); 191 | 192 | it("should set `language` property", () => { 193 | expect(Indexer.language).toEqual(language); 194 | }); 195 | 196 | it("should set `delimiter` property", () => { 197 | expect(Indexer.delimiter).toEqual(delimiter); 198 | }); 199 | 200 | it("should set `indexName` property", () => { 201 | expect(Indexer.indexName).toEqual(indexName); 202 | }); 203 | }); 204 | }); 205 | -------------------------------------------------------------------------------- /test/hes.bin.spec.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | const assert = require("yeoman-assert"); 3 | const shell = require("shelljs"); 4 | 5 | global.console.error = jest.fn(i => i); 6 | global.process.exit = jest.fn(); 7 | 8 | describe("hes bin", () => { 9 | const root = fs.realpathSync(process.cwd()); 10 | 11 | describe("#hes", () => { 12 | beforeAll(() => { 13 | shell.exec(`node "${root}/bin/hes.js" -i "${root}/invalid/path/to/content" -o "${root}/public/invalid.json"`); 14 | }); 15 | 16 | it("should not create `output` file if content not found for `input` path", () => { 17 | assert.noFile(`${root}/public/invalid.json`); 18 | }); 19 | 20 | it("should print an error message to the console", () => { 21 | expect(global.console.error).toHaveBeenCalled(); 22 | expect(global.console.error).toHaveReturnedWith( 23 | `exec: No content found for specified input path: "${root}/invalid/path/to/content"\n` 24 | ); 25 | }); 26 | }); 27 | 28 | describe("#hes (-i, -o, -l, -d) ~ yaml", () => { 29 | beforeAll(() => { 30 | shell.exec(`rm -f "${root}/public/elasticsearch-yaml.json"`); 31 | shell.exec( 32 | `node "${root}/bin/hes.js" -i "${root}/content/test-yaml.md" -o "${root}/public/elasticsearch-yaml.json" -l "yaml" -d "---" -n "indexx"` 33 | ); 34 | }); 35 | 36 | it("should create `output` file", () => { 37 | assert.file(`${root}/public/elasticsearch-yaml.json`); 38 | }); 39 | 40 | it("should parse the correct `_index` key", () => { 41 | assert.fileContent(`${root}/public/elasticsearch-yaml.json`, '"_index":"indexx"'); 42 | }); 43 | 44 | it("should parse the correct `title` key", () => { 45 | assert.fileContent(`${root}/public/elasticsearch-yaml.json`, '"title":"Sample title"'); 46 | }); 47 | 48 | it("should parse the correct `description` key", () => { 49 | assert.fileContent(`${root}/public/elasticsearch-yaml.json`, '"description":"Sample description"'); 50 | }); 51 | 52 | it("should parse the correct `tags` key", () => { 53 | assert.fileContent(`${root}/public/elasticsearch-yaml.json`, '"tags":["tag1"]'); 54 | }); 55 | 56 | it("should parse the correct `uri` key", () => { 57 | assert.fileContent(`${root}/public/elasticsearch-yaml.json`, '"uri":"/test-yaml"'); 58 | }); 59 | 60 | it("should parse the correct `content` key", () => { 61 | assert.fileContent(`${root}/public/elasticsearch-yaml.json`, '"content":"\\nSample content header\\nSample content body"'); 62 | }); 63 | }); 64 | 65 | describe("#hes (-i, -o, -l, -d) ~ toml", () => { 66 | beforeAll(() => { 67 | shell.exec(`rm -f "${root}/public/elasticsearch-toml.json"`); 68 | shell.exec( 69 | `node "${root}/bin/hes.js" -i "${root}/content/test-toml.md" -o "${root}/public/elasticsearch-toml.json" -l "toml" -d "+++" -n "indexx"` 70 | ); 71 | }); 72 | 73 | it("should create `output` file", () => { 74 | assert.file(`${root}/public/elasticsearch-toml.json`); 75 | }); 76 | 77 | it("should parse the correct `_index` key", () => { 78 | assert.fileContent(`${root}/public/elasticsearch-toml.json`, '"_index":"indexx"'); 79 | }); 80 | 81 | it("should parse the correct `title` key", () => { 82 | assert.fileContent(`${root}/public/elasticsearch-toml.json`, '"title":"Sample title"'); 83 | }); 84 | 85 | it("should parse the correct `description` key", () => { 86 | assert.fileContent(`${root}/public/elasticsearch-toml.json`, '"description":"Sample description"'); 87 | }); 88 | 89 | it("should parse the correct `tags` key", () => { 90 | assert.fileContent(`${root}/public/elasticsearch-toml.json`, '"tags":["tag1"]'); 91 | }); 92 | 93 | it("should parse the correct `uri` key", () => { 94 | assert.fileContent(`${root}/public/elasticsearch-toml.json`, '"uri":"/test-toml"'); 95 | }); 96 | 97 | it("should parse the correct `content` key", () => { 98 | assert.fileContent(`${root}/public/elasticsearch-toml.json`, '"content":"\\nSample content header\\nSample content body"'); 99 | }); 100 | }); 101 | }); 102 | -------------------------------------------------------------------------------- /usage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clarketm/hugo-elasticsearch/2091c0d90ffbe3eef4634e4b4a4cfbf89b2eef7f/usage.gif --------------------------------------------------------------------------------