|
1 | 1 | const Ontology = require('../../lib/Ontology')
|
| 2 | +const contentTypes = require('./contentTypes') |
| 3 | + |
| 4 | +class Post { |
| 5 | + constructor(entry) { |
| 6 | + this.contentModel = { |
| 7 | + type: contentTypes.POST, |
| 8 | + data: { |
| 9 | + title: entry.data.name.data |
| 10 | + } |
| 11 | + } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +const Models = { |
| 16 | + Post: { |
| 17 | + schema: (entry) => ({ |
| 18 | + type: 'object', |
| 19 | + data: { |
| 20 | + format: /(markdown|plaintext|hypertext|handlebars)/, |
| 21 | + } |
| 22 | + }), |
| 23 | + |
| 24 | + match: (entry, _schema) => { |
| 25 | + const schema = _schema || Models.Post.schema(entry) |
| 26 | + return Object.keys(schema).every((key) => { |
| 27 | + const expected = schema[key] |
| 28 | + const actual = entry[key]?.data || entry[key] |
| 29 | + console.log(key, actual, expected, entry) |
| 30 | + if (typeof expected === 'string') { |
| 31 | + return actual === expected |
| 32 | + } |
| 33 | + if (expected instanceof RegExp) { |
| 34 | + return !!actual.match(expected) |
| 35 | + } |
| 36 | + if (expected instanceof Function) { |
| 37 | + return expected(actual) |
| 38 | + } |
| 39 | + if (typeof expected === 'object') { |
| 40 | + return Models.Post.match(actual, expected) |
| 41 | + } |
| 42 | + }) |
| 43 | + }, |
| 44 | + |
| 45 | + reduce: (model, entry) => { |
| 46 | + if (!Models.Post.match(entry)) { |
| 47 | + return undefined |
| 48 | + } |
| 49 | + const post = new Post(entry) |
| 50 | + const newModel = { |
| 51 | + ...model, |
| 52 | + posts: (model.posts || []).concat(post.contentModel.data) |
| 53 | + } |
| 54 | + return newModel |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
2 | 58 |
|
3 | 59 | class Blog extends Ontology {
|
4 |
| - constructor(contentTree) { |
| 60 | + constructor(contentTree, entry) { |
5 | 61 | super('blog', contentTree)
|
6 |
| - this.contentModel = 'blog contentModel' |
7 |
| - // console.log(`Blog contentTree`, JSON.stringify(contentTree, null, 2)) |
| 62 | + console.log('Blog contentTree', contentTree, entry) |
| 63 | + this.contentModel = entry.subTree.reduce((model, entry) => { |
| 64 | + const withPosts = Models.Post.reduce(model, entry) |
| 65 | + if (withPosts) { |
| 66 | + console.log('yes posts', withPosts) |
| 67 | + return withPosts |
| 68 | + } |
| 69 | + |
| 70 | + console.log('no posts', withPosts) |
| 71 | + |
| 72 | + // localAssets |
| 73 | + |
| 74 | + return model |
| 75 | + }, {}) |
8 | 76 | }
|
9 | 77 | }
|
10 | 78 |
|
|
0 commit comments