+
Skip to content

feat: allow for second parameter on getSlug function for allowed char… #6695

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

Merged
merged 6 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/three-lamps-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@reactioncommerce/api-plugin-tags": patch
"@reactioncommerce/api-utils": patch
---

Allow getSlug to take a second parameter of allowedChars
3 changes: 2 additions & 1 deletion packages/api-plugin-tags/src/mutations/addTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export default async function addTag(context, input) {
slug = slugInput;
}

const allowedCharacters = "a-zA-Z0-9-/";
const now = new Date();
const tag = {
_id: Random.id(),
isDeleted: false,
isTopLevel: false,
isVisible,
slug: getSlug(slug),
slug: getSlug(slug, allowedCharacters),
metafields,
name,
displayTitle,
Expand Down
3 changes: 2 additions & 1 deletion packages/api-plugin-tags/src/mutations/updateTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default async function updateTag(context, input) {
await context.validatePermissions(`reaction:legacy:tags:${tagId}`, "update", { shopId });

const metafields = [];
const allowedCharacters = "a-zA-Z0-9-/";

// Filter out blank meta fields
Array.isArray(input.metafields) && input.metafields.forEach((field) => {
Expand All @@ -55,7 +56,7 @@ export default async function updateTag(context, input) {
}

const params = {
slug: getSlug(slug),
slug: getSlug(slug, allowedCharacters),
name: input.name,
displayTitle: input.displayTitle,
isVisible: input.isVisible,
Expand Down
7 changes: 5 additions & 2 deletions packages/api-utils/lib/getSlug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ const require = createRequire(import.meta.url);

const { slugify } = require("transliteration");

const standardSlug = "a-zA-Z0-9-_.~'";

/**
* @name getSlug
* @summary Return a slugified string using "slugify" from transliteration
* @see https://www.npmjs.com/package/transliteration
* @memberof Utils
* @param {String} slugString - string to slugify
* @param {String} [allowedChars=a-zA-Z0-9-_.~'] - specify extra characters that are not removed by slugify
* @returns {String} slugified string
*/
export default function getSlug(slugString) {
return (typeof slugString === "string" && slugify(slugString, { allowedChars: "a-zA-Z0-9-/" })) || "";
export default function getSlug(slugString, allowedChars = standardSlug) {
return (typeof slugString === "string" && slugify(slugString, { allowedChars })) || "";
}
10 changes: 9 additions & 1 deletion packages/api-utils/lib/getSlug.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import getSlug from "./getSlug.js";

const allowedCharacters = "a-zA-Z0-9-/";

it("should slugify a latin text", () => {
const text = "This is a title";
const slugified = getSlug(text);
Expand All @@ -8,6 +10,12 @@ it("should slugify a latin text", () => {

it("should slugify a latin text with / (backslash)", () => {
const text = "men/jacket";
const slugified = getSlug(text);
const slugified = getSlug(text, allowedCharacters);
expect(slugified).toEqual("men/jacket");
});

it("should slugify with a mix of slash and others", () => {
const text = "extremely tall men/jacket";
const slugified = getSlug(text, allowedCharacters);
expect(slugified).toEqual("extremely-tall-men/jacket");
});
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载