Skip to content

Commit

Permalink
feat(memory): default slug and collection properties
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jun 25, 2024
1 parent 6734d63 commit 3ce3aea
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
3 changes: 1 addition & 2 deletions docs/plugins/mdx.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

# Plugin MDX

The plugin search for MDX files inside a root directory. By default, only the files
matching the `.md` and `.mdx` extensions are converted to documents.
The plugin search for MDX files inside a root directory. By default, only the files matching the `.md` and `.mdx` extensions are converted to documents.

## Configuration

Expand Down
17 changes: 17 additions & 0 deletions docs/plugins/memory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

# Plugin memory

The plugin receives a set of documents. It is quite minimalist, just pass the array of documents to the `documents` property.

## Configuration

The plugin accept one or multiple configurations.

A configuration is an object with the following attributes:

- `collection`: default collection name of the documents.
- `documents`: array of documents.

## Documents

The `slug` property is automatically set with the index integer value unless defined.
3 changes: 1 addition & 2 deletions docs/plugins/yaml.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

# Plugin YAML

The plugin search for YAML files inside a target directory. By default, only the files
matching the `.yml` and `.yaml` extensions are converted to documents.
The plugin search for YAML files inside a target directory. By default, only the files matching the `.yml` and `.yaml` extensions are converted to documents.

## Configuration

Expand Down
6 changes: 5 additions & 1 deletion packages/redac/lib/plugin-memory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export default (config) => {
},
'engine:source': async ({documents}) => {
configs.forEach( config => {
documents.push(...config.documents)
documents.push(...config.documents.map((document, i) => {
document.collection ??= config.collection
document.slug ??= [i+1]
return document
}))
})
}
}
Expand Down
65 changes: 49 additions & 16 deletions packages/redac/test/plugin-memory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import redac from 'redac'
import redacMemory from 'redac/plugins/memory'

describe('memory', async () => {

it('config is invalid', async () => {
(() =>
redac(redacMemory).memory(false)
).should.throw([
'REDAC_MEMORY_INVALID_ARGUMENTS:',
'plugin config must be an object,',
'got false.',
].join(' '))
;(() => redac(redacMemory).memory(false)).should.throw(
[
'REDAC_MEMORY_INVALID_ARGUMENTS:',
'plugin config must be an object,',
'got false.',
].join(' ')
)
})

it('config.documents is invalid', async () => {
(() =>
;(() =>
redac(redacMemory).memory({
documents: false
})
).should.throw([
'REDAC_MEMORY_INVALID_DOCUMENTS_ARGUMENTS:',
'config must contain an array of documents,',
'got false.',
].join(' '))
documents: false,
})).should.throw(
[
'REDAC_MEMORY_INVALID_DOCUMENTS_ARGUMENTS:',
'config must contain an array of documents,',
'got false.',
].join(' ')
)
})

it('config.documents is an array of documents', async () =>
Expand All @@ -41,7 +41,40 @@ describe('memory', async () => {
{
collection: 'memory',
key: 'value',
slug: [1],
},
]))

it('config.collection defined the default document collection', async () =>
redac(redacMemory)
.memory({
collection: 'collection_1',
documents: [
{
key: 'value 1',
},
{
collection: 'collection_1',
key: 'value 2',
},
{
collection: 'collection_2',
key: 'value 3',
},
],
})
.from('collection_1')
.list()
.should.be.resolvedWith([
{
collection: 'collection_1',
key: 'value 1',
slug: [1],
},
{
collection: 'collection_1',
key: 'value 2',
slug: [2],
},
]))
})
22 changes: 22 additions & 0 deletions packages/redac/test/utils.tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,27 @@ describe('functions', async () => {
},
])
})

it('treat number as index', async () => {
const pages = [
{ slug: [1], title: 'a' },
{ slug: [2], title: 'b' },
{ slug: [2, 3], title: 'c' },
]
tree(pages).should.match([
{ title: 'a', slug: [1], children: [] },
{
title: 'b',
slug: [2],
children: [
{
title: 'c',
slug: [3, 'c'],
children: [],
},
],
},
])
})

})

0 comments on commit 3ce3aea

Please sign in to comment.