Skip to content

Commit 5e258ac

Browse files
committed
Feat: add file path support for slug generation and enhance slug template placeholders #922
1 parent d2b0228 commit 5e258ac

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

Diff for: CHANGELOG.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
## [10.8.0] - 2025-02-xx
44

5-
### ✨ New features
6-
75
### 🎨 Enhancements
86

97
- [#915](https://github.com/estruyf/vscode-front-matter/issues/915): Added a new setting `frontMatter.panel.openOnSupportedFile` which allows you to open the panel view on supported files
108
- [#921](https://github.com/estruyf/vscode-front-matter/issues/921): Improve the filename sanitization
11-
12-
### ⚡️ Optimizations
9+
- [#922](https://github.com/estruyf/vscode-front-matter/issues/922): Added `{{fileName}}` and `{{sluggedFileName}}` placeholders for the slug template setting
1310

1411
### 🐞 Fixes
1512

Diff for: src/commands/Article.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ export class Article {
172172
/**
173173
* Generate the new slug
174174
*/
175-
public static generateSlug(title: string, article?: ParsedFrontMatter, slugTemplate?: string) {
175+
public static generateSlug(
176+
title: string,
177+
article?: ParsedFrontMatter,
178+
filePath?: string,
179+
slugTemplate?: string
180+
) {
176181
if (!title) {
177182
return;
178183
}
@@ -181,7 +186,7 @@ export class Article {
181186
const suffix = Settings.get(SETTING_SLUG_SUFFIX) as string;
182187

183188
if (article?.data) {
184-
const slug = SlugHelper.createSlug(title, article?.data, slugTemplate);
189+
const slug = SlugHelper.createSlug(title, article?.data, filePath, slugTemplate);
185190

186191
if (typeof slug === 'string') {
187192
return {
@@ -224,7 +229,12 @@ export class Article {
224229
articleDate
225230
);
226231

227-
const slugInfo = Article.generateSlug(articleTitle, article, contentType.slugTemplate);
232+
const slugInfo = Article.generateSlug(
233+
articleTitle,
234+
article,
235+
editor.document.uri.fsPath,
236+
contentType.slugTemplate
237+
);
228238

229239
if (
230240
slugInfo &&
@@ -255,7 +265,8 @@ export class Article {
255265
article.data[pField.name] = processArticlePlaceholdersFromData(
256266
article.data[pField.name],
257267
article.data,
258-
contentType
268+
contentType,
269+
editor.document.uri.fsPath
259270
);
260271
article.data[pField.name] = processTimePlaceholders(
261272
article.data[pField.name],
@@ -335,7 +346,7 @@ export class Article {
335346
} else {
336347
const article = ArticleHelper.getFrontMatter(editor);
337348
if (article?.data) {
338-
return SlugHelper.createSlug(article.data[titleField], article.data, slugTemplate);
349+
return SlugHelper.createSlug(article.data[titleField], article.data, file, slugTemplate);
339350
}
340351
}
341352
}

Diff for: src/helpers/ArticleHelper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ export class ArticleHelper {
684684
}
685685

686686
if (fieldName === 'slug' && (fieldValue === null || fieldValue === '')) {
687-
fmData[fieldName] = SlugHelper.createSlug(title, fmData, slugTemplate);
687+
fmData[fieldName] = SlugHelper.createSlug(title, fmData, filePath, slugTemplate);
688688
}
689689

690690
fmData[fieldName] = await processArticlePlaceholdersFromPath(fmData[fieldName], filePath);

Diff for: src/helpers/ContentType.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,8 @@ export class ContentType {
10631063
data[field.name] = processArticlePlaceholdersFromData(
10641064
field.default as string,
10651065
data,
1066-
contentType
1066+
contentType,
1067+
filePath
10671068
);
10681069
data[field.name] = processTimePlaceholders(
10691070
data[field.name],

Diff for: src/helpers/SlugHelper.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Settings } from '.';
1+
import { parseWinPath, Settings } from '.';
22
import { stopWords, charMap, SETTING_DATE_FORMAT, SETTING_SLUG_TEMPLATE } from '../constants';
33
import { processTimePlaceholders, processFmPlaceholders } from '.';
4+
import { parse } from 'path';
45

56
export class SlugHelper {
67
/**
@@ -11,6 +12,7 @@ export class SlugHelper {
1112
public static createSlug(
1213
articleTitle: string,
1314
articleData: { [key: string]: any },
15+
filePath?: string,
1416
slugTemplate?: string
1517
): string | null {
1618
if (!articleTitle) {
@@ -28,6 +30,16 @@ export class SlugHelper {
2830
} else if (slugTemplate.includes('{{seoTitle}}')) {
2931
const regex = new RegExp('{{seoTitle}}', 'g');
3032
slugTemplate = slugTemplate.replace(regex, SlugHelper.slugify(articleTitle));
33+
} else if (slugTemplate.includes(`{{fileName}}`)) {
34+
const file = parse(filePath || '');
35+
const fileName = file.name;
36+
const regex = new RegExp('{{fileName}}', 'g');
37+
slugTemplate = slugTemplate.replace(regex, fileName);
38+
} else if (slugTemplate.includes(`{{sluggedFileName}}`)) {
39+
const file = parse(filePath || '');
40+
const fileName = SlugHelper.slugify(file.name);
41+
const regex = new RegExp('{{sluggedFileName}}', 'g');
42+
slugTemplate = slugTemplate.replace(regex, fileName);
3143
}
3244

3345
const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;

Diff for: src/helpers/processArticlePlaceholders.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { SlugHelper } from './SlugHelper';
66
export const processArticlePlaceholdersFromData = (
77
value: string,
88
data: { [key: string]: any },
9-
contentType: ContentType
9+
contentType: ContentType,
10+
filePath?: string
1011
): string => {
1112
const titleField = getTitleField();
1213
if (value.includes('{{title}}') && data[titleField]) {
@@ -18,7 +19,7 @@ export const processArticlePlaceholdersFromData = (
1819
const regex = new RegExp('{{slug}}', 'g');
1920
value = value.replace(
2021
regex,
21-
SlugHelper.createSlug(data[titleField] || '', data, contentType.slugTemplate) || ''
22+
SlugHelper.createSlug(data[titleField] || '', data, filePath, contentType.slugTemplate) || ''
2223
);
2324
}
2425

@@ -50,6 +51,7 @@ export const processArticlePlaceholdersFromPath = async (
5051
SlugHelper.createSlug(
5152
article.data[titleField] || '',
5253
article.data,
54+
filePath,
5355
contentType.slugTemplate
5456
) || ''
5557
);

Diff for: src/listeners/panel/DataListener.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,9 @@ export class DataListener extends BaseListener {
794794
const crntFile = window.activeTextEditor?.document;
795795
const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;
796796
value =
797-
data && contentType ? processArticlePlaceholdersFromData(value, data, contentType) : value;
797+
data && contentType
798+
? processArticlePlaceholdersFromData(value, data, contentType, crntFile?.uri.fsPath)
799+
: value;
798800
value = processTimePlaceholders(value, dateFormat);
799801
value = processFmPlaceholders(value, data);
800802

0 commit comments

Comments
 (0)