|
1 | 1 | const Settings = require('../../../settings')
|
2 | 2 | const { makePermalink, getSlug } = require('../../../helpers')
|
3 | 3 | 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') |
96 | 5 |
|
97 | 6 | const BLOG_DEFAULT_TYPE = 'text'
|
98 | 7 |
|
99 | 8 | class Blog extends Ontology {
|
100 | 9 | constructor(blogEntry) {
|
101 | 10 | super('blog', blogEntry)
|
102 | 11 | // 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 = { |
109 | 13 | type: 'Blog',
|
110 | 14 | data: {
|
111 | 15 | type: blogEntry.data.type?.data || BLOG_DEFAULT_TYPE,
|
112 | 16 | format: blogEntry.data.format.data,
|
113 | 17 | name: blogEntry.data.name.data,
|
114 | 18 | path: blogEntry.data.path.data,
|
115 | 19 | 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 |
122 | 24 | }
|
123 |
| - return results |
| 25 | + return contentModel |
124 | 26 | }, [])
|
125 | 27 | ]
|
126 | 28 | }
|
127 | 29 | }
|
128 | 30 | }
|
129 | 31 |
|
130 | 32 | 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) |
136 | 34 | }
|
137 | 35 | }
|
138 | 36 |
|
|
0 commit comments