-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slugifyWithCount): persist slug count (#82)
* fix(slugification): persist slug count * fix(slugification): ensure correct count used for old plugin versions and projects with existing slugs * fix(slug CT): do not show plugin CT in content manager * fix(syncSlugCount): ensure we have slugs to sync for createMany * refactor(buildSlug): use early return
- Loading branch information
1 parent
9c336eb
commit 2d53829
Showing
8 changed files
with
167 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
const syncSlugCount = async (settings) => { | ||
const entries = await strapi.entityService.findMany('plugin::slugify.slug', { | ||
filters: { createdAt: { $gt: 1 } }, | ||
}); | ||
|
||
// if entries aready present we can skip sync | ||
if (entries && entries.length) { | ||
return; | ||
} | ||
|
||
strapi.log.info('[slugify] syncing slug count for registered content types'); | ||
|
||
const slugs = new Map(); | ||
|
||
// chec slugs in each reigistered model | ||
for (const uid in settings.modelsByUID) { | ||
if (!Object.hasOwnProperty.call(settings.modelsByUID, uid)) { | ||
continue; | ||
} | ||
|
||
const model = settings.modelsByUID[uid]; | ||
|
||
// using db query to avoid the need to check if CT has draftAndPublish enabled | ||
const modelEntries = await strapi.db.query(model.uid).findMany({ | ||
filters: { createdAt: { $gt: 1 } }, | ||
}); | ||
|
||
strapi.log.info(`[slugify] syncing slug count for ${model.uid}`); | ||
for (const entry of modelEntries) { | ||
const slug = entry[model.field]; | ||
if (!slug) { | ||
continue; | ||
} | ||
|
||
const record = slugs.get(getNonAppendedSlug(slug)); | ||
if (!record) { | ||
slugs.set(slug, { slug, count: 1 }); | ||
continue; | ||
} | ||
|
||
slugs.set(record.slug, { slug: record.slug, count: record.count + 1 }); | ||
} | ||
strapi.log.info(`[slugify] sync for ${model.uid} completed`); | ||
} | ||
|
||
if (slugs.size) { | ||
// create all required records | ||
const createResponse = await strapi.db.query('plugin::slugify.slug').createMany({ | ||
data: [...slugs.values()], | ||
}); | ||
|
||
strapi.log.info( | ||
`[slugify] ${createResponse.count} out of ${slugs.size} slugs synced successfully` | ||
); | ||
} else { | ||
strapi.log.info('[slugify] No syncable slugs found'); | ||
} | ||
}; | ||
|
||
// removes any appended number from a slug/string if found | ||
const getNonAppendedSlug = (slug) => { | ||
const match = slug.match('[\\-]{1}[\\d]+$'); | ||
|
||
if (!match) { | ||
return slug; | ||
} | ||
|
||
return slug.replace(match[0], ''); | ||
}; | ||
|
||
module.exports = { | ||
syncSlugCount, | ||
}; |
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,9 @@ | ||
'use strict'; | ||
|
||
const slugSchema = require('./slug/schema.json'); | ||
|
||
module.exports = { | ||
slug: { | ||
schema: slugSchema, | ||
}, | ||
}; |
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,29 @@ | ||
{ | ||
"kind": "collectionType", | ||
"collectionName": "slugs", | ||
"info": { | ||
"singularName": "slug", | ||
"pluralName": "slugs", | ||
"displayName": "slug" | ||
}, | ||
"options": { | ||
"draftAndPublish": false, | ||
"comment": "" | ||
}, | ||
"pluginOptions": { | ||
"content-manager": { | ||
"visible": false | ||
}, | ||
"content-type-builder": { | ||
"visible": false | ||
} | ||
}, | ||
"attributes": { | ||
"slug": { | ||
"type": "text" | ||
}, | ||
"count": { | ||
"type": "integer" | ||
} | ||
} | ||
} |
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,40 @@ | ||
const slugify = require('@sindresorhus/slugify'); | ||
|
||
const buildSlug = async (string, settings) => { | ||
let slug = slugify(string, settings.slugifyOptions); | ||
|
||
// slugify with count | ||
if (!settings.slugifyWithCount) { | ||
return slug; | ||
} | ||
|
||
const slugEntry = await strapi.db.query('plugin::slugify.slug').findOne({ | ||
select: ['id', 'count'], | ||
where: { slug }, | ||
}); | ||
|
||
// if no result then count is 1 and base slug is returned | ||
if (!slugEntry) { | ||
await strapi.entityService.create('plugin::slugify.slug', { | ||
data: { | ||
slug, | ||
count: 1, | ||
}, | ||
}); | ||
|
||
return slug; | ||
} | ||
|
||
const count = slugEntry.count + 1; | ||
await strapi.entityService.update('plugin::slugify.slug', slugEntry.id, { | ||
data: { | ||
count, | ||
}, | ||
}); | ||
|
||
return `${slug}-${count}`; | ||
}; | ||
|
||
module.exports = { | ||
buildSlug, | ||
}; |
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 was deleted.
Oops, something went wrong.