-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): Allow customising the collection list based on multiple fields #2140
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
66ee500
Allow customising the collection list based on multiple fields
devinea 351c485
Update docs
devinea b5e8e4b
prettier
devinea 703d594
applied feedback, use template strings for summary field
4723576
format code
8ae1173
adjust search bar to make summary string fields searchable
c5a112e
format code
46c3fb3
make searchList unique
a80bd04
removes getIdentifier wrapper
d493d5f
refactor
erquhart 3d1d099
fix template string replacements
6e6ca1c
fix imports
8993fa7
Update config.yml
erquhart ceed3c1
fix template string regex on case of no matches
6cb5dc1
refactor
erquhart c39756b
fixed issue if there are no fields
0bda57d
simplify dev-test example
erquhart af645e9
fixed issue with the search not picking up dates
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import moment from 'moment'; | ||
import { selectInferedField } from 'Reducers/collections'; | ||
|
||
// prepends a Zero if the date has only 1 digit | ||
function formatDate(date) { | ||
return `0${date}`.slice(-2); | ||
} | ||
|
||
export const dateParsers = { | ||
year: date => date.getFullYear(), | ||
month: date => formatDate(date.getMonth() + 1), | ||
day: date => formatDate(date.getDate()), | ||
hour: date => formatDate(date.getHours()), | ||
minute: date => formatDate(date.getMinutes()), | ||
second: date => formatDate(date.getSeconds()), | ||
}; | ||
|
||
export const SLUG_MISSING_REQUIRED_DATE = 'SLUG_MISSING_REQUIRED_DATE'; | ||
|
||
const FIELD_PREFIX = 'fields.'; | ||
const templateContentPattern = '[^}{]+'; | ||
const templateVariablePattern = `{{(${templateContentPattern})}}`; | ||
|
||
// Allow `fields.` prefix in placeholder to override built in replacements | ||
// like "slug" and "year" with values from fields of the same name. | ||
function getExplicitFieldReplacement(key, data) { | ||
if (!key.startsWith(FIELD_PREFIX)) { | ||
return; | ||
} | ||
const fieldName = key.substring(FIELD_PREFIX.length); | ||
return data.get(fieldName, ''); | ||
} | ||
|
||
export function parseDateFromEntry(entry, collection, fieldName) { | ||
const dateFieldName = fieldName || selectInferedField(collection, 'date'); | ||
if (!dateFieldName) { | ||
return; | ||
} | ||
|
||
const dateValue = entry.getIn(['data', dateFieldName]); | ||
const dateMoment = dateValue && moment(dateValue); | ||
if (dateMoment && dateMoment.isValid()) { | ||
return dateMoment.toDate(); | ||
} | ||
} | ||
|
||
export function compileStringTemplate(template, date, identifier = '', data = Map(), processor) { | ||
let missingRequiredDate; | ||
|
||
// Turn off date processing (support for replacements like `{{year}}`), by passing in | ||
// `null` as the date arg. | ||
const useDate = date !== null; | ||
|
||
const slug = template.replace(RegExp(templateVariablePattern, 'g'), (_, key) => { | ||
let replacement; | ||
const explicitFieldReplacement = getExplicitFieldReplacement(key, data); | ||
|
||
if (explicitFieldReplacement) { | ||
replacement = explicitFieldReplacement; | ||
} else if (dateParsers[key] && !date) { | ||
missingRequiredDate = true; | ||
return ''; | ||
} else if (dateParsers[key]) { | ||
replacement = dateParsers[key](date); | ||
} else if (key === 'slug') { | ||
replacement = identifier; | ||
} else { | ||
replacement = data.get(key, ''); | ||
} | ||
|
||
if (processor) { | ||
return processor(replacement); | ||
} | ||
|
||
return replacement; | ||
}); | ||
|
||
if (useDate && missingRequiredDate) { | ||
const err = new Error(); | ||
err.name = SLUG_MISSING_REQUIRED_DATE; | ||
throw err; | ||
} else { | ||
return slug; | ||
} | ||
} | ||
|
||
export function extractTemplateVars(template) { | ||
const regexp = RegExp(templateVariablePattern, 'g'); | ||
const contentRegexp = RegExp(templateContentPattern, 'g'); | ||
const matches = template.match(regexp) || []; | ||
return matches.map(elem => elem.match(contentRegexp)[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have looked at this in annoyance a thousand times but never bothered to fix lol 👍👍