Update SLUG field in server side post_create and put_update hooks.
This commit is contained in:
11
api/hooks/articles/post_create.js
Normal file
11
api/hooks/articles/post_create.js
Normal file
@@ -0,0 +1,11 @@
|
||||
// @ts-check
|
||||
const { generateArticleSlugUrlString: generateSlugUrlStringByArticle } = require("../lib/helper")
|
||||
|
||||
;(function () {
|
||||
/** @type {import("tibi-types").HookResponse} */
|
||||
let hookResponse
|
||||
|
||||
context.data.article.content.slug = generateSlugUrlStringByArticle(context.data.article)
|
||||
|
||||
return hookResponse
|
||||
})()
|
||||
11
api/hooks/articles/put_update.js
Normal file
11
api/hooks/articles/put_update.js
Normal file
@@ -0,0 +1,11 @@
|
||||
// @ts-check
|
||||
const { generateArticleSlugUrlString: generateSlugUrlStringByArticle } = require("../lib/helper")
|
||||
|
||||
;(function () {
|
||||
/** @type {import("tibi-types").HookResponse} */
|
||||
let hookResponse
|
||||
|
||||
context.data.article.content.slug = generateSlugUrlStringByArticle(context.data.article)
|
||||
|
||||
return hookResponse
|
||||
})()
|
||||
17
api/hooks/lib/helper.js
Normal file
17
api/hooks/lib/helper.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const generateArticleSlugUrlString = (article) => {
|
||||
let slug = article.content.slug
|
||||
let title = article.content.title
|
||||
|
||||
if (!slug || slug === "") {
|
||||
slug = title
|
||||
.replace(/[^a-zA-Z0-9 ]/g, "")
|
||||
.replace(/\s/g, "-")
|
||||
.toLowerCase()
|
||||
}
|
||||
|
||||
return slug
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generateArticleSlugUrlString,
|
||||
}
|
||||
Reference in New Issue
Block a user