Skip to content

Commit b6088ff

Browse files
authored
Use the front matter defaults computed by Jekyll (#585)
Jekyll already computes front matter defaults by comparing against `path` and `type` for a given resource. However, the front-end port is still necessary for `"(**/)new"` routes, as the resource-path is `undefined` by default.
1 parent e92c9e5 commit b6088ff

File tree

7 files changed

+26
-27
lines changed

7 files changed

+26
-27
lines changed

docs/_frontend/containers.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ Container for editing a draft.
284284
errors: Array,
285285
params: Object,
286286
router: Object,
287-
route: Object,
288-
config: Object
287+
route: Object
289288
}
290289
```
291290

lib/jekyll-admin/apiable.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ def file_read_options
134134
Jekyll::Utils.merged_file_read_opts(site, {})
135135
end
136136

137+
def front_matter_defaults
138+
return unless file_exists?
139+
140+
@front_matter_defaults ||= begin
141+
return {} unless respond_to?(:relative_path) && respond_to?(:type)
142+
143+
site.frontmatter_defaults.all(relative_path, type)
144+
end
145+
end
146+
137147
def front_matter
138148
return unless file_exists?
139149

@@ -187,6 +197,7 @@ def content_fields
187197
else
188198
output["raw_content"] = raw_content
189199
output["front_matter"] = front_matter
200+
output["front_matter_defaults"] = front_matter_defaults
190201
end
191202

192203
# Include next and previous documents non-recursively

src/containers/views/DocumentEdit.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
} from '../../ducks/collections';
1919
import { updateTitle, updateBody, updatePath } from '../../ducks/metadata';
2020
import { clearErrors } from '../../ducks/utils';
21-
import { injectDefaultFields } from '../../utils/metadata';
2221
import { preventDefault, getDocumentTitle } from '../../utils/helpers';
2322
import { ADMIN_PREFIX } from '../../constants';
2423

@@ -95,7 +94,6 @@ export class DocumentEdit extends Component {
9594
updated,
9695
fieldChanged,
9796
params,
98-
config,
9997
} = this.props;
10098

10199
if (isFetching) {
@@ -112,10 +110,10 @@ export class DocumentEdit extends Component {
112110
http_url,
113111
collection,
114112
front_matter,
113+
front_matter_defaults,
115114
name,
116115
} = currentDocument;
117116
const directory = params.splat[0];
118-
const defaultMetadata = injectDefaultFields(config, directory, collection);
119117

120118
const keyboardHandlers = {
121119
save: this.handleClickSave,
@@ -143,7 +141,7 @@ export class DocumentEdit extends Component {
143141
updateBody={updateBody}
144142
onSave={this.handleClickSave}
145143
metafields={{ title, path: name, raw_content, ...front_matter }}
146-
staticmetafields={defaultMetadata}
144+
staticmetafields={front_matter_defaults}
147145
/>
148146
<div className="content-side">
149147
<Button
@@ -185,7 +183,6 @@ DocumentEdit.propTypes = {
185183
params: PropTypes.object.isRequired,
186184
router: PropTypes.object.isRequired,
187185
route: PropTypes.object.isRequired,
188-
config: PropTypes.object.isRequired,
189186
};
190187

191188
const mapStateToProps = state => ({
@@ -194,7 +191,6 @@ const mapStateToProps = state => ({
194191
fieldChanged: state.metadata.fieldChanged,
195192
updated: state.collections.updated,
196193
errors: state.utils.errors,
197-
config: state.config.config,
198194
});
199195

200196
const mapDispatchToProps = dispatch =>

src/containers/views/DraftEdit.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
} from '../../ducks/drafts';
2121
import { updateTitle, updateBody, updatePath } from '../../ducks/metadata';
2222
import { clearErrors } from '../../ducks/utils';
23-
import { injectDefaultFields } from '../../utils/metadata';
2423
import { preventDefault, getDocumentTitle } from '../../utils/helpers';
2524
import { ADMIN_PREFIX } from '../../constants';
2625

@@ -118,7 +117,6 @@ export class DraftEdit extends Component {
118117
updated,
119118
fieldChanged,
120119
params,
121-
config,
122120
} = this.props;
123121

124122
if (isFetching) {
@@ -135,13 +133,12 @@ export class DraftEdit extends Component {
135133
name,
136134
path,
137135
raw_content,
138-
collection,
139136
http_url,
140137
front_matter,
138+
front_matter_defaults,
141139
} = draft;
142140
const directory = params.splat[0];
143141
const title = front_matter && front_matter.title ? front_matter.title : '';
144-
const defaultMetadata = injectDefaultFields(config, directory, collection);
145142
const document_title = getDocumentTitle('drafts', directory, title || name);
146143

147144
return (
@@ -163,7 +160,7 @@ export class DraftEdit extends Component {
163160
updateTitle={updateTitle}
164161
onSave={this.handleClickSave}
165162
metafields={{ title, raw_content, path: name, ...front_matter }}
166-
staticmetafields={defaultMetadata}
163+
staticmetafields={front_matter_defaults}
167164
/>
168165
<div className="content-side">
169166
<Button
@@ -212,7 +209,6 @@ DraftEdit.propTypes = {
212209
params: PropTypes.object.isRequired,
213210
router: PropTypes.object.isRequired,
214211
route: PropTypes.object.isRequired,
215-
config: PropTypes.object.isRequired,
216212
};
217213

218214
const mapStateToProps = state => ({
@@ -221,7 +217,6 @@ const mapStateToProps = state => ({
221217
fieldChanged: state.metadata.fieldChanged,
222218
updated: state.drafts.updated,
223219
errors: state.utils.errors,
224-
config: state.config.config,
225220
});
226221

227222
const mapDispatchToProps = dispatch =>

src/containers/views/PageEdit.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import MarkdownPageBody from '../../components/MarkdownPageBody';
1414
import { fetchPage, deletePage, putPage } from '../../ducks/pages';
1515
import { updateTitle, updateBody, updatePath } from '../../ducks/metadata';
1616
import { clearErrors } from '../../ducks/utils';
17-
import { injectDefaultFields } from '../../utils/metadata';
1817
import { preventDefault, getDocumentTitle } from '../../utils/helpers';
1918
import { ADMIN_PREFIX } from '../../constants';
2019

@@ -85,7 +84,6 @@ export class PageEdit extends Component {
8584
updated,
8685
fieldChanged,
8786
params,
88-
config,
8987
} = this.props;
9088

9189
if (isFetching) {
@@ -100,12 +98,16 @@ export class PageEdit extends Component {
10098
save: this.handleClickSave,
10199
};
102100

103-
const { name, raw_content, http_url, front_matter } = page;
101+
const {
102+
name,
103+
raw_content,
104+
http_url,
105+
front_matter,
106+
front_matter_defaults,
107+
} = page;
104108
const directory = params.splat[0];
105109

106110
const title = front_matter && front_matter.title ? front_matter.title : '';
107-
const defaultMetadata = injectDefaultFields(config, directory, 'pages');
108-
109111
const document_title = getDocumentTitle('pages', directory, title || name);
110112

111113
return (
@@ -128,7 +130,7 @@ export class PageEdit extends Component {
128130
title={title}
129131
body={raw_content}
130132
metafields={{ title, raw_content, path: name, ...front_matter }}
131-
staticmetafields={defaultMetadata}
133+
staticmetafields={front_matter_defaults}
132134
/>
133135
<div className="content-side">
134136
<Button
@@ -170,7 +172,6 @@ PageEdit.propTypes = {
170172
params: PropTypes.object.isRequired,
171173
router: PropTypes.object.isRequired,
172174
route: PropTypes.object.isRequired,
173-
config: PropTypes.object.isRequired,
174175
};
175176

176177
const mapStateToProps = state => ({
@@ -179,7 +180,6 @@ const mapStateToProps = state => ({
179180
fieldChanged: state.metadata.fieldChanged,
180181
updated: state.pages.updated,
181182
errors: state.utils.errors,
182-
config: state.config.config,
183183
});
184184

185185
const mapDispatchToProps = dispatch =>

src/containers/views/tests/documentedit.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { DocumentEdit } from '../DocumentEdit';
55
import Errors from '../../../components/Errors';
66
import Button from '../../../components/Button';
77

8-
import { config, doc } from './fixtures';
8+
import { doc } from './fixtures';
99

1010
const defaultProps = {
11-
config,
1211
currentDocument: doc,
1312
errors: [],
1413
fieldChanged: false,

src/containers/views/tests/pageedit.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { PageEdit } from '../PageEdit';
55
import Errors from '../../../components/Errors';
66
import Button from '../../../components/Button';
77

8-
import { config, page } from './fixtures';
8+
import { page } from './fixtures';
99

1010
const defaultProps = {
1111
page,
12-
config,
1312
errors: [],
1413
fieldChanged: false,
1514
updated: false,

0 commit comments

Comments
 (0)