Skip to content

Commit e96e263

Browse files
committed
Map and render blog posts
1 parent f82962a commit e96e263

File tree

4 files changed

+350
-110
lines changed

4 files changed

+350
-110
lines changed

src/compiler2/ontologies/blog/index.js

+8-110
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,36 @@
11
const Settings = require('../../../settings')
22
const { makePermalink, getSlug } = require('../../../helpers')
33
const Ontology = require('../../lib/Ontology')
4-
const contentTypes = require('./contentTypes')
5-
6-
const POST_DEFAULT_TYPE = 'text'
7-
8-
const maybeRawHTMLType = (entry) => {
9-
return entry.data.format.data === 'hypertext'
10-
}
11-
12-
class Post {
13-
constructor(entry) {
14-
this.contentModel = this.mapContentTree(entry)
15-
}
16-
17-
getPermalink(entry) {
18-
const { permalinkPrefix } = Settings.getSettings()
19-
return makePermalink({
20-
prefix: permalinkPrefix,
21-
parts: [
22-
entry.data.name.data
23-
],
24-
addHTMLExtension: true
25-
})
26-
}
27-
28-
mapContentTree(entry) {
29-
const permalink = this.getPermalink(entry)
30-
return {
31-
type: contentTypes.POST,
32-
data: {
33-
...entry.data,
34-
type: entry.data.type?.data || maybeRawHTMLType(entry) || POST_DEFAULT_TYPE,
35-
format: entry.data.format?.data,
36-
title: entry.data.title?.data || entry.data.name.data || '',
37-
content: entry.data.content?.data || '',
38-
mentions: entry.data.mentions?.data || [],
39-
cover: entry.data.cover ? [permalink, entry.data.cover.data].join('/') : '',
40-
media: entry.data.media ? [permalink, entry.data.media.data].join('/') : '',
41-
summary: entry.data.summary?.data || '',
42-
tags: entry.data.tags?.data || [],
43-
publishDatePrototype: {
44-
value: entry.data.publishDate?.data || entry.data.stats.data.birthtime.data,
45-
checkCache: !entry.data.publishDate?.data
46-
},
47-
slug: getSlug(entry.data.name.data),
48-
permalink,
49-
// category: getPostCategory(entry, categorized),
50-
path: entry.data.path.data,
51-
outputPath: getSlug(entry.data.name.data) + '.html',
52-
// handle: removeExtension(entry.path),
53-
localAssets: entry.data.localAssets?.data || [],
54-
// transcript: getTranscript(entry.data, localAssets),
55-
}
56-
}
57-
}
58-
}
59-
60-
const Models = {
61-
Post: {
62-
view: require('./views/posts'),
63-
64-
schema: (entry) => ({
65-
type: 'object',
66-
data: {
67-
format: /(markdown|plaintext|hypertext|handlebars)/,
68-
}
69-
}),
70-
71-
match: (entry, _schema) => {
72-
const schema = _schema || Models.Post.schema(entry)
73-
return Object.keys(schema).every((key) => {
74-
const expected = schema[key]
75-
const actual = entry[key]?.data || entry[key]
76-
if (typeof expected === 'string') {
77-
return actual === expected
78-
}
79-
if (expected instanceof RegExp) {
80-
return !!actual.match(expected)
81-
}
82-
if (expected instanceof Function) {
83-
return expected(actual)
84-
}
85-
if (typeof expected === 'object') {
86-
return Models.Post.match(actual, expected)
87-
}
88-
})
89-
},
90-
91-
render: async (renderer, post, rootModel) => {
92-
return Models.Post.view(renderer, post, rootModel)
93-
}
94-
}
95-
}
4+
const Posts = require('./post')
965

976
const BLOG_DEFAULT_TYPE = 'text'
987

998
class Blog extends Ontology {
1009
constructor(blogEntry) {
10110
super('blog', blogEntry)
10211
// console.log('Blog contentTree', JSON.stringify(contentTree, null, 2), JSON.stringify(blogEntry, null, 2))
103-
console.log('blogEntry.subTree', JSON.stringify(blogEntry.subTree, null, 2))
104-
this.contentModel = this.mapContentTree(blogEntry)
105-
}
106-
107-
mapContentTree(blogEntry) {
108-
return {
12+
this.contentModel = {
10913
type: 'Blog',
11014
data: {
11115
type: blogEntry.data.type?.data || BLOG_DEFAULT_TYPE,
11216
format: blogEntry.data.format.data,
11317
name: blogEntry.data.name.data,
11418
path: blogEntry.data.path.data,
11519
posts: [
116-
...blogEntry.subTree.reduce((results, childEntry) => {
117-
if (Models.Post.match(childEntry)) {
118-
return [
119-
...results,
120-
new Post(childEntry)
121-
]
20+
...blogEntry.subTree.reduce((contentModel, childEntry) => {
21+
const withPosts = Posts.reduce(contentModel, childEntry)
22+
if (withPosts) {
23+
return withPosts
12224
}
123-
return results
25+
return contentModel
12426
}, [])
12527
]
12628
}
12729
}
12830
}
12931

13032
async render(renderer, blogEntry, rootModel) {
131-
return Promise.all(
132-
blogEntry.contentModel.data.posts.map(post => {
133-
return Models.Post.render(renderer, post, rootModel)
134-
})
135-
)
33+
await Posts.render(renderer, blogEntry.contentModel, rootModel)
13634
}
13735
}
13836

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const model = require('./model')
2+
const view = require('./view')
3+
4+
const render = (renderer, blog, contentModel) => {
5+
return Promise.all(
6+
blog.data.posts.map(post => {
7+
return view(renderer, post, contentModel)
8+
})
9+
)
10+
}
11+
12+
const reduce = (contentModel, entry) => {
13+
if (!model.match(entry)) {
14+
return undefined
15+
}
16+
return [
17+
...contentModel,
18+
model.create(entry).data
19+
]
20+
}
21+
22+
module.exports = {
23+
model,
24+
view,
25+
render,
26+
reduce
27+
}

0 commit comments

Comments
 (0)