Skip to content

Commit 3dceadb

Browse files
committed
DRY up portal
1 parent 73cf2ca commit 3dceadb

File tree

16 files changed

+198
-291
lines changed

16 files changed

+198
-291
lines changed

src/compiler2/lib/Model.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Model {
2+
constructor({ schema, create }) {
3+
if (!schema) {
4+
throw new Error('Model requires a schema')
5+
}
6+
if (typeof create !== 'function') {
7+
throw new Error('Model requires a create fn')
8+
}
9+
this.schema = schema
10+
this.create = create
11+
}
12+
13+
match(entry, _schema) {
14+
const schema = _schema || this.schema(entry)
15+
return Object.keys(schema).every((key) => {
16+
const expected = schema[key]
17+
const actual = entry[key]
18+
if (typeof expected === 'string') {
19+
return (actual.data || actual) === expected
20+
}
21+
if (expected instanceof RegExp) {
22+
return !!(actual.data || actual).match(expected)
23+
}
24+
if (expected instanceof Function) {
25+
return expected(actual.data || actual)
26+
}
27+
if (key === 'data') {
28+
return this.match(actual, expected)
29+
}
30+
})
31+
}
32+
}
33+
34+
module.exports = Model

src/compiler2/ontologies/portal/contentTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
SUB_ONTOLOGY: 'subOntology',
23
HOMEPAGE: 'homepage',
34
SUBPAGES: 'subpages',
45
SUBPAGE: 'subpage',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const model = require('./model')
2+
const view = require('./view')
3+
4+
const render = (renderer, contentModel) => {
5+
return view(renderer, contentModel)
6+
}
7+
8+
const reduce = (contentModel, entry) => {
9+
if (!model.match(entry)) {
10+
return undefined
11+
}
12+
return {
13+
...contentModel,
14+
homepage: model.create(entry).data
15+
}
16+
}
17+
18+
module.exports = {
19+
model,
20+
view,
21+
render,
22+
reduce
23+
}

src/compiler2/ontologies/portal/models/homepage.js renamed to src/compiler2/ontologies/portal/homepage/model.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,24 @@ const createFolderedHomepage = (fsObject) => {
8989

9090
const Settings = require('../../../../settings')
9191
const contentTypes = require('../contentTypes')
92+
const Model = require('../../../lib/Model')
93+
94+
const DEFAULT_TYPE = 'basic'
9295

9396
const maybeRawHTMLType = (entry) => {
9497
return entry.data.format.data === 'hypertext'
9598
}
9699

97-
const DEFAULT_TYPE = 'basic'
98-
99-
module.exports = class Homepage {
100-
constructor(contentTree) {
101-
this.contentModel = this.mapContentTree(contentTree)
102-
}
100+
const Homepage = new Model({
101+
schema: (entry) => ({
102+
type: 'object',
103+
data: {
104+
name: /(homepage|home|index)/,
105+
format: /(markdown|plaintext|hypertext|handlebars)/,
106+
},
107+
}),
103108

104-
mapContentTree(entry) {
109+
create(entry) {
105110
const indexFile = entry
106111
const localAssets = []
107112
const permalink = Settings.getSettings().permalinkPrefix
@@ -118,4 +123,6 @@ module.exports = class Homepage {
118123
}
119124
}
120125
}
121-
}
126+
})
127+
128+
module.exports = Homepage

src/compiler2/ontologies/portal/index.js

Lines changed: 12 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,25 @@
1-
const Settings = require('../../../settings')
21
const Ontology = require('../../lib/Ontology')
3-
// const Assets = require('./models/assets')
4-
// const LocalAsset = require('./models/localAsset')
5-
2+
const SubOntologies = require('./subontology')
3+
const Homepage = require('./homepage')
4+
const Subpages = require('./subpage')
65

76
module.exports = ({ ontologies }) => {
8-
const Models = {
9-
Homepage: require('./models/homepage'),
10-
Subpage: require('./models/subpage')
11-
}
12-
13-
const SubOntologies = {
14-
view: 'should it have a view?',
15-
16-
schema: (entry) => ({
17-
type: 'object',
18-
data: {
19-
name: ontologies.get
20-
},
21-
}),
22-
23-
match: (entry, _schema) => {
24-
const schema = _schema || SubOntologies.schema(entry)
25-
return Object.keys(schema).every((key) => {
26-
const expected = schema[key]
27-
const actual = entry[key]?.data || entry[key]
28-
if (typeof expected === 'string') {
29-
return actual === expected
30-
}
31-
if (expected instanceof RegExp) {
32-
return !!actual.match(expected)
33-
}
34-
if (expected instanceof Function) {
35-
return expected(actual)
36-
}
37-
if (typeof expected === 'object') {
38-
return SubOntologies.match(actual, expected)
39-
}
40-
})
41-
},
42-
43-
reduce: (contentModel, entry) => {
44-
if (!SubOntologies.match(entry)) {
45-
return undefined
46-
}
47-
const subOntologyClass = ontologies.get(entry.data.name.data)
48-
return {
49-
...contentModel,
50-
[entry.data.name.data]: new subOntologyClass(entry)
51-
}
52-
},
53-
54-
async render(renderer, contentModel) {
55-
return Promise.all(
56-
Object.keys(contentModel).map(key => {
57-
const entry = contentModel[key]
58-
if (entry instanceof Ontology) {
59-
return entry.render(renderer, entry, contentModel)
60-
}
61-
})
62-
)
63-
}
64-
}
65-
66-
/*
67-
* Organize Models together with their Views in a [modelName] folder?
68-
* */
69-
const HomepageCenter = {
70-
view: require('./views/homepage'),
71-
72-
schema: (entry) => ({
73-
type: 'object',
74-
data: {
75-
name: /(homepage|home|index)/,
76-
format: /(markdown|plaintext|hypertext|handlebars)/,
77-
},
78-
}),
79-
80-
match: (entry, _schema) => {
81-
const schema = _schema || HomepageCenter.schema(entry)
82-
return Object.keys(schema).every((key) => {
83-
const expected = schema[key]
84-
const actual = entry[key]
85-
if (typeof expected === 'string') {
86-
return (actual.data || actual) === expected
87-
}
88-
if (expected instanceof RegExp) {
89-
return !!(actual.data || actual).match(expected)
90-
}
91-
if (expected instanceof Function) {
92-
return expected(actual.data || actual)
93-
}
94-
if (key === 'data') {
95-
return HomepageCenter.match(actual, expected)
96-
}
97-
})
98-
},
99-
100-
reduce: (contentModel, entry) => {
101-
if (!HomepageCenter.match(entry)) {
102-
return undefined
103-
}
104-
const homepage = new Models.Homepage(entry)
105-
const newModel = {
106-
...contentModel,
107-
homepage: homepage.contentModel.data
108-
}
109-
return newModel
110-
},
111-
112-
render: async (renderer, contentModel) => {
113-
await HomepageCenter.view(renderer, contentModel)
114-
}
115-
}
116-
117-
/*
118-
*
119-
* Seeing if an interface emerges
120-
* Will merge with their models under a renewed interface?
121-
*
122-
* */
123-
const Subpages = {
124-
view: require('./views/subpages'),
125-
126-
schema: (entry) => ({
127-
type: 'object',
128-
data: {
129-
format: /(markdown|plaintext|hypertext|handlebars)/
130-
}
131-
}),
132-
133-
match: (entry, _schema) => {
134-
const schema = _schema || Subpages.schema(entry)
135-
return Object.keys(schema).every((key) => {
136-
const expected = schema[key]
137-
const actual = entry[key]
138-
if (typeof expected === 'string') {
139-
return (actual.data || actual) === expected
140-
}
141-
if (expected instanceof RegExp) {
142-
return !!(actual.data || actual).match(expected)
143-
}
144-
if (expected instanceof Function) {
145-
return expected(actual.data || actual)
146-
}
147-
if (key === 'data') {
148-
return Subpages.match(actual, expected)
149-
}
150-
})
151-
},
152-
153-
reduce: (contentModel, entry) => {
154-
if (!Subpages.match(entry)) {
155-
return undefined
156-
}
157-
const subpage = new Models.Subpage(entry)
158-
const newModel = {
159-
...contentModel,
160-
subpages: [
161-
...(contentModel.subpages || []),
162-
subpage.contentModel.data
163-
]
164-
}
165-
return newModel
166-
},
167-
168-
render: async (renderer, contentModel) => {
169-
await Subpages.view(renderer, contentModel)
170-
}
171-
}
172-
1737
class Portal extends Ontology {
1748
constructor(contentTree) {
1759
super('portal', contentTree)
10+
11+
this.subOntologies = SubOntologies({
12+
ontologies
13+
})
14+
17615
this.contentModel = contentTree.reduce((contentModel, entry) => {
17716

178-
const withSubOntologies = SubOntologies.reduce(contentModel, entry)
17+
const withSubOntologies = this.subOntologies.reduce(contentModel, entry)
17918
if (withSubOntologies) {
18019
return withSubOntologies
18120
}
18221

183-
const withHomepage = HomepageCenter.reduce(
22+
const withHomepage = Homepage.reduce(
18423
(withSubOntologies || contentModel),
18524
entry
18625
)
@@ -204,8 +43,8 @@ module.exports = ({ ontologies }) => {
20443
}
20544

20645
async render(renderer) {
207-
await SubOntologies.render(renderer, this.contentModel)
208-
await HomepageCenter.render(renderer, this.contentModel)
46+
await this.subOntologies.render(renderer, this.contentModel)
47+
await Homepage.render(renderer, this.contentModel)
20948
await Subpages.render(renderer, this.contentModel)
21049
}
21150
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const createModel = require('./model')
2+
const view = require('./view')
3+
4+
module.exports = ({ ontologies }) => {
5+
const model = createModel({ ontologies })
6+
7+
const render = (renderer, contentModel) => {
8+
return view(renderer, contentModel)
9+
}
10+
11+
const reduce = (contentModel, entry) => {
12+
if (!model.match(entry)) {
13+
return undefined
14+
}
15+
return {
16+
...contentModel,
17+
[entry.data.name.data]: model.create(entry).data
18+
}
19+
}
20+
21+
return {
22+
model,
23+
view,
24+
render,
25+
reduce
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Settings = require('../../../../settings')
2+
const contentTypes = require('../contentTypes')
3+
const Model = require('../../../lib/Model')
4+
5+
const SubOntology = ({ ontologies }) => new Model({
6+
schema: (entry) => ({
7+
type: 'object',
8+
data: {
9+
name: ontologies.get
10+
},
11+
}),
12+
13+
create: (entry) => {
14+
const subOntologyClass = ontologies.get(entry.data.name.data)
15+
return {
16+
type: contentTypes.SUB_ONTOLOGY,
17+
data: new subOntologyClass(entry)
18+
}
19+
}
20+
})
21+
22+
module.exports = SubOntology

0 commit comments

Comments
 (0)