Skip to content

Commit

Permalink
feat: add XML support (#182)
Browse files Browse the repository at this point in the history
* add xml file

* add xml

* add dependencies

* Update index.js

* bugfix

* Create xml.xml

* Update index.vue

* Create xml.xml

* Update writing.md

* Update writing.md

* Update utils.js

* fix dependencies

* fix

* update docs

* add config docs

* Update index.md

Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
mathe42 and benjamincanac authored Jun 29, 2020
1 parent 210187e commit d334f1e
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 10 deletions.
11 changes: 10 additions & 1 deletion docs/content/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ This module uses `js-yaml` to parse `.yaml`, `.yml` files, you can check here fo

Note that we force `json: true` option.


### `xml`

- Type: `Object`
- Default: `{}`

This module uses `xml2js` to parse `.xml` files, you can check here for [options](https://www.npmjs.com/package/xml2js#options).

### `csv`

- Type: `Object`
Expand Down Expand Up @@ -298,7 +306,8 @@ export default {
}
},
yaml: {},
csv: {}
csv: {},
xml: {}
}
}
```
4 changes: 2 additions & 2 deletions docs/content/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ features:
- Powerful QueryBuilder API (MongoDB like)
- Syntax highlighting to code blocks in markdown files using PrismJS.
- Table of contents generation
- Handles Markdown, CSV, YAML, JSON(5)
- Handles Markdown, CSV, YAML, JSON(5), XML
- Extend with hooks
csb_link: https://codesandbox.io/embed/nuxt-content-l164h?hidenavigation=1&theme=dark
---

Empower your NuxtJS application with `@nuxt/content` module: write in a `content/` directory and fetch your Markdown, JSON, YAML and CSV files through a MongoDB like API, acting as a **Git-based Headless CMS**.
Empower your NuxtJS application with `@nuxt/content` module: write in a `content/` directory and fetch your Markdown, JSON, YAML, XML and CSV files through a MongoDB like API, acting as a **Git-based Headless CMS**.

## Features

Expand Down
47 changes: 46 additions & 1 deletion docs/content/en/writing.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ content/
home.md
```

This module will parse `.md`, `.yaml`, `.yml`, `.csv`, `.json`, `.json5` files and generate the following properties:
This module will parse `.md`, `.yaml`, `.yml`, `.csv`, `.json`, `.json5`, `.xml` files and generate the following properties:

- `dir`
- `path`
Expand Down Expand Up @@ -438,6 +438,51 @@ Will be transformed into:
}
```

## XML

XML will be parsed

### Example

A file `content/home.xml`:

```xml
<xml>
<item prop="abc">
<title>Title</title>
<description>Hello World</description>
</item>
</xml>
```

Will be transformed into:

```json
{
"dir": "/",
"slug": "home",
"path": "/home",
"extension": ".xml",
"body": {
"xml": {
"item": [
{
"$": {
"prop": "abc"
},
"title": [
"Title"
],
"description": [
"Hello World"
]
}
]
}
}
```


## YAML / YML

Data defined will be injected into the document.
Expand Down
6 changes: 6 additions & 0 deletions example/content/xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<xml>
<item prop="abc">
<title>Title</title>
<description>Hello World</description>
</item>
</xml>
6 changes: 5 additions & 1 deletion example/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<pre>{{ yaml }}</pre>
<h2>YML</h2>
<pre>{{ yml }}</pre>
<h2>XML</h2>
<pre>{{ xml }}</pre>
</div>
</template>

Expand All @@ -31,14 +33,16 @@ export default {
const csv = await $content('csv').fetch()
const yaml = await $content('yaml').fetch()
const yml = await $content('yml').fetch()
const xml = await $content('xml').fetch()
return {
markdown,
json,
json5,
csv,
yaml,
yml
yml,
xml
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const { default: PQueue } = require('p-queue')
const Markdown = require('./parsers/markdown')
const Yaml = require('./parsers/yaml')
const Csv = require('./parsers/csv')
const XML = require('./parsers/xml')
const QueryBuilder = require('./query-builder')
const EXTENSIONS = ['.md', '.json', '.json5', '.yaml', '.yml', '.csv']
const EXTENSIONS = ['.md', '.json', '.json5', '.yaml', '.yml', '.csv', '.xml']

LokiFullTextSearch.register()

Expand All @@ -24,6 +25,7 @@ class Database extends Hookable {
this.markdown = new Markdown(options.markdown)
this.yaml = new Yaml(options.yaml)
this.csv = new Csv(options.csv)
this.xml = new XML(options.xml)
// Create Loki database
this.db = new Loki('content.db')
// Init collection
Expand Down Expand Up @@ -174,7 +176,8 @@ class Database extends Hookable {
'.md': file => this.markdown.toJSON(file),
'.csv': file => this.csv.toJSON(file),
'.yaml': file => this.yaml.toJSON(file),
'.yml': file => this.yaml.toJSON(file)
'.yml': file => this.yaml.toJSON(file),
'.xml': file => this.xml.toJSON(file)
})[extension]
// Collect data from file
let data = {}
Expand Down
22 changes: 22 additions & 0 deletions lib/parsers/xml/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const xml = require('xml2js')

class XML {
constructor (options = {}) {
this.options = options
}

/**
* Converts xml document to it's JSON structure.
* @param {string} file - xml file
* @return {Object}
*/
async toJSON (file) {
const body = await xml.parseStringPromise(file, this.options)

return {
body
}
}
}

module.exports = XML
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const getDefaults = ({ dev = false } = {}) => ({
}
},
yaml: {},
csv: {}
csv: {},
xml: {}
})

const mergeConfig = (content, defaults, options = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"remark-slug": "^6.0.0",
"remark-squeeze-paragraphs": "^4.0.0",
"unified": "^9.0.0",
"ws": "^7.3.0"
"ws": "^7.3.0",
"xml2js": "^0.4.23"
},
"devDependencies": {
"@babel/core": "^7.10.3",
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/content/formats/xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<xml>
<item prop="abc">
<title>Title</title>
<description>Hello World</description>
</item>
</xml>
20 changes: 19 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7036,6 +7036,11 @@ jiti@^0.1.11:
resolved "https://registry.yarnpkg.com/jiti/-/jiti-0.1.11.tgz#8b27b92e4c0866b3c8c91945c55a99a1db17a782"
integrity sha512-zSPegl+ageMLSYcq1uAZa6V56pX2GbNl/eU3Or7PFHu10a2YhLAXj5fnHJGd6cHZTalSR8zXGH8WmyuyufMhLA==

jiti@^0.1.9:
version "0.1.9"
resolved "https://registry.yarnpkg.com/jiti/-/jiti-0.1.9.tgz#2567bb84fa101582716387f1d53b88dc03f0e1a4"
integrity sha512-HR7z/lkCbx7VUdjWaAUVMU5gSDMg9DOB2NoKWAoN8oYMurosG7UlCVvmYCt1ADn+Uf+wvO7s9LsfiuPYzHMx9Q==

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -10288,7 +10293,7 @@ sane@^4.0.3:
minimist "^1.1.1"
walker "~1.0.5"

sax@~1.2.4:
sax@>=0.6.0, sax@~1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
Expand Down Expand Up @@ -12326,6 +12331,19 @@ xml-name-validator@^3.0.0:
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==

xml2js@^0.4.23:
version "0.4.23"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.23.tgz#a0c69516752421eb2ac758ee4d4ccf58843eac66"
integrity sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==
dependencies:
sax ">=0.6.0"
xmlbuilder "~11.0.0"

xmlbuilder@~11.0.0:
version "11.0.1"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==

xmlchars@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"
Expand Down

0 comments on commit d334f1e

Please sign in to comment.