From e95e9a8fc7473304738b5e125b5faf788ff4e4ef Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 6 Sep 2024 14:12:12 +0200 Subject: [PATCH] #842 - Allow the slug to be set to an empty value --- CHANGELOG.md | 1 + package.json | 3 ++- src/commands/Article.ts | 8 ++++++-- src/helpers/SlugHelper.ts | 6 +++--- src/panelWebView/components/Fields/SlugField.tsx | 4 ++-- src/panelWebView/components/Fields/WrapperField.tsx | 2 ++ 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0920536..a6ccbf1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### 🐞 Fixes +- [#842](https://github.com/estruyf/vscode-front-matter/issues/842): Allow to set the `frontMatter.taxonomy.slugTemplate` setting to an empty string - [#845](https://github.com/estruyf/vscode-front-matter/issues/845): Fix empty values for number fields ## [10.3.0] - 2024-08-13 - [Release notes](https://beta.frontmatter.codes/updates/v10.3.0) diff --git a/package.json b/package.json index b1467d61..14863ebd 100644 --- a/package.json +++ b/package.json @@ -1993,7 +1993,8 @@ "scope": "Taxonomy" }, "frontMatter.taxonomy.slugTemplate": { - "type": "string", + "type": ["string", "null"], + "default": null, "markdownDescription": "%setting.frontMatter.taxonomy.slugTemplate.markdownDescription%", "scope": "Taxonomy" }, diff --git a/src/commands/Article.ts b/src/commands/Article.ts index 53ce77ac..6c686429 100644 --- a/src/commands/Article.ts +++ b/src/commands/Article.ts @@ -175,7 +175,7 @@ export class Article { if (article?.data) { const slug = SlugHelper.createSlug(title, article?.data, slugTemplate); - if (slug) { + if (typeof slug === 'string') { return { slug, slugWithPrefixAndSuffix: `${prefix}${slug}${suffix}` @@ -214,7 +214,11 @@ export class Article { const articleTitle: string = article.data[titleField]; const slugInfo = Article.generateSlug(articleTitle, article, contentType.slugTemplate); - if (slugInfo && slugInfo.slug && slugInfo.slugWithPrefixAndSuffix) { + if ( + slugInfo && + typeof slugInfo.slug === 'string' && + typeof slugInfo.slugWithPrefixAndSuffix === 'string' + ) { article.data['slug'] = slugInfo.slugWithPrefixAndSuffix; if (contentType) { diff --git a/src/helpers/SlugHelper.ts b/src/helpers/SlugHelper.ts index 9eed5c4d..fbfa7de5 100644 --- a/src/helpers/SlugHelper.ts +++ b/src/helpers/SlugHelper.ts @@ -17,11 +17,11 @@ export class SlugHelper { return null; } - if (!slugTemplate) { - slugTemplate = Settings.get(SETTING_SLUG_TEMPLATE) || undefined; + if (slugTemplate === undefined || slugTemplate === null) { + slugTemplate = Settings.get(SETTING_SLUG_TEMPLATE); } - if (slugTemplate) { + if (typeof slugTemplate === 'string') { if (slugTemplate.includes('{{title}}')) { const regex = new RegExp('{{title}}', 'g'); slugTemplate = slugTemplate.replace(regex, articleTitle.toLowerCase().replace(/\s/g, '-')); diff --git a/src/panelWebView/components/Fields/SlugField.tsx b/src/panelWebView/components/Fields/SlugField.tsx index 2222ff34..069aef15 100644 --- a/src/panelWebView/components/Fields/SlugField.tsx +++ b/src/panelWebView/components/Fields/SlugField.tsx @@ -46,7 +46,7 @@ export const SlugField: React.FunctionComponent = ({ if (text !== value) { setText(value); } - }, [value]); + }, [text, value]); useEffect(() => { if (titleValue) { @@ -54,7 +54,7 @@ export const SlugField: React.FunctionComponent = ({ title: titleValue, slugTemplate }).then((slug) => { - if (slug.slugWithPrefixAndSuffix) { + if (typeof slug.slugWithPrefixAndSuffix === "string") { setSlug(slug.slugWithPrefixAndSuffix); } }).catch((_) => { diff --git a/src/panelWebView/components/Fields/WrapperField.tsx b/src/panelWebView/components/Fields/WrapperField.tsx index 5bf4e98a..9de2e2ae 100644 --- a/src/panelWebView/components/Fields/WrapperField.tsx +++ b/src/panelWebView/components/Fields/WrapperField.tsx @@ -131,6 +131,8 @@ export const WrapperField: React.FunctionComponent = ({ if (fieldValue === undefined || value !== fieldValue) { if (typeof value === 'number') { setFieldValue(value); + } else if (field.type === "slug") { + setFieldValue(value); } else { setFieldValue(value || null); }