Skip to content

Commit 6617004

Browse files
committed
WIP blog subOntology
1 parent 356d5dd commit 6617004

File tree

6 files changed

+165
-14
lines changed

6 files changed

+165
-14
lines changed

src/compiler2/drivers/fs/index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,22 @@ class FileSystemDriver extends Driver {
181181
[key]: this.deepTokenize(obj[key])
182182
}
183183
}, {}),
184-
subTree: (obj.children || []).map(c => this.deepTokenize(c))
184+
subTree: (obj.children || []).map(child => {
185+
if (child.content) {
186+
const frontMatterResult = frontMatter(child.content || '')
187+
return this.deepTokenize({
188+
...child,
189+
...(frontMatterResult.attributes || {}),
190+
content: frontMatterResult.body,
191+
format: this.mapExtensionToFormat(child.extension),
192+
})
193+
}
194+
195+
return this.deepTokenize({
196+
...child,
197+
format: this.mapExtensionToFormat(child.extension),
198+
})
199+
})
185200
}
186201
}
187202

src/compiler2/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const compile = async () => {
2323
} = await fileSystem.parse(contentRootPath)
2424
console.log('settings.rootContentModel', rootContentModel)
2525
const RootOntology = Ontologies.get(rootContentModel)
26-
const root = new RootOntology(contentTree)
26+
const root = new RootOntology(contentTree.tree)
2727
await root.render(Renderer)
2828
Debug.timeEnd('compiler')
2929
return {

src/compiler2/lib/Ontology.js

+62
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,68 @@ const createContentModel = async (fsTree, initialModel) => {
4848
}
4949
*/
5050

51+
/*
52+
53+
class Ontology {
54+
constructor(contentTree, schema) {
55+
this.schema = schema
56+
this.contentTree = contentTree
57+
this.contentModel = this.map(contentTree, schema)
58+
}
59+
60+
async map(contentTree, schema) {
61+
console.log(contentTree, schema)
62+
return contentTree.reduce((contentNode, entry) => {
63+
const models = Array.isArray(schema) ? schema : [schema]
64+
return models.reduce((contentModel, model) => {
65+
const newContentModel = model.reduce(contentNode, entry)
66+
if (contentModel) {
67+
return contentModel
68+
}
69+
return model
70+
}, {})
71+
}, {})
72+
}
73+
74+
match(entry, _schema) {
75+
const schema = _schema || this.schema(entry)
76+
return Object.keys(schema).every((key) => {
77+
const expected = schema[key]
78+
const actual = entry[key]
79+
if (typeof expected === 'string') {
80+
return (actual.data || actual) === expected
81+
}
82+
if (expected instanceof RegExp) {
83+
return !!(actual.data || actual).match(expected)
84+
}
85+
if (expected instanceof Function) {
86+
return expected(actual.data || actual)
87+
}
88+
if (key === 'data') {
89+
return this.match(actual, expected)
90+
}
91+
})
92+
}
93+
94+
reduce(model, entry) {
95+
if (!this.match(entry)) {
96+
return undefined
97+
}
98+
const newModel = {
99+
...model,
100+
homepage: this.contentModel.data
101+
}
102+
return newModel
103+
}
104+
105+
async render(renderer, model) {
106+
await this.view(renderer, model)
107+
}
108+
}
109+
110+
module.exports = Ontology
111+
*/
112+
51113
class Ontology {
52114
constructor(name, contentTree) {
53115
this.name = name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
POSTS: 'posts',
3+
POST: 'post',
4+
FOLDERED_POST_INDEX: 'folderedPostIndex',
5+
LOCAL_ASSET: 'localAsset'
6+
}

src/compiler2/ontologies/blog/index.js

+71-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
11
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+
}
258

359
class Blog extends Ontology {
4-
constructor(contentTree) {
60+
constructor(contentTree, entry) {
561
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+
}, {})
876
}
977
}
1078

src/compiler2/ontologies/portal/index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ module.exports = ({ ontologies }) => {
2424
const schema = _schema || SubOntologies.schema(entry)
2525
return Object.keys(schema).every((key) => {
2626
const expected = schema[key]
27-
const actual = entry[key]
27+
const actual = entry[key]?.data || entry[key]
2828
if (typeof expected === 'string') {
29-
return actual.data === expected
29+
return actual === expected
3030
}
3131
if (expected instanceof RegExp) {
32-
return !!actual.data.match(expected)
32+
return !!actual.match(expected)
3333
}
3434
if (expected instanceof Function) {
35-
return expected(actual.data)
35+
return expected(actual)
3636
}
3737
if (typeof expected === 'object') {
3838
return SubOntologies.match(actual, expected)
@@ -41,14 +41,14 @@ module.exports = ({ ontologies }) => {
4141
},
4242

4343
reduce: (model, entry) => {
44-
return undefined
4544
if (!SubOntologies.match(entry)) {
4645
return undefined
4746
}
48-
const subOntology = ontologies.get(entry.data.name.data)
47+
const subOntologyClass = ontologies.get(entry.data.name.data)
48+
const subOntology = new subOntologyClass(model, entry)
4949
return {
5050
...model,
51-
[entry.data.name.data]: entry.contentModel
51+
[entry.data.name.data]: subOntology.contentModel
5252
}
5353
},
5454

@@ -166,8 +166,8 @@ module.exports = ({ ontologies }) => {
166166

167167
class Portal extends Ontology {
168168
constructor(contentTree) {
169-
super(contentTree)
170-
this.contentModel = contentTree.tree.reduce((model, entry) => {
169+
super('portal', contentTree)
170+
this.contentModel = contentTree.reduce((model, entry) => {
171171

172172
const withSubOntologies = SubOntologies.reduce(model, entry)
173173
if (withSubOntologies) {

0 commit comments

Comments
 (0)