Skip to content

Commit

Permalink
#842 - Allow the slug to be set to an empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Sep 6, 2024
1 parent d8e3338 commit e95e9a8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,8 @@
"scope": "Taxonomy"
},
"frontMatter.taxonomy.slugTemplate": {
"type": "string",
"type": ["string", "null"],
"default": null,
"markdownDescription": "%setting.frontMatter.taxonomy.slugTemplate.markdownDescription%",
"scope": "Taxonomy"
},
Expand Down
8 changes: 6 additions & 2 deletions src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/SlugHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export class SlugHelper {
return null;
}

if (!slugTemplate) {
slugTemplate = Settings.get<string>(SETTING_SLUG_TEMPLATE) || undefined;
if (slugTemplate === undefined || slugTemplate === null) {
slugTemplate = Settings.get<string>(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, '-'));
Expand Down
4 changes: 2 additions & 2 deletions src/panelWebView/components/Fields/SlugField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export const SlugField: React.FunctionComponent<ISlugFieldProps> = ({
if (text !== value) {
setText(value);
}
}, [value]);
}, [text, value]);

useEffect(() => {
if (titleValue) {
messageHandler.request<{ slug: string; slugWithPrefixAndSuffix: string; }>(CommandToCode.generateSlug, {
title: titleValue,
slugTemplate
}).then((slug) => {
if (slug.slugWithPrefixAndSuffix) {
if (typeof slug.slugWithPrefixAndSuffix === "string") {
setSlug(slug.slugWithPrefixAndSuffix);
}
}).catch((_) => {
Expand Down
2 changes: 2 additions & 0 deletions src/panelWebView/components/Fields/WrapperField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
if (fieldValue === undefined || value !== fieldValue) {
if (typeof value === 'number') {
setFieldValue(value);
} else if (field.type === "slug") {
setFieldValue(value);
} else {
setFieldValue(value || null);
}
Expand Down

0 comments on commit e95e9a8

Please sign in to comment.