Skip to content

Commit

Permalink
#350 - New previewPath property for page folders
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jun 10, 2022
1 parent e9258e1 commit 1a6acce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 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 @@
- [#307](https://github.com/estruyf/vscode-front-matter/issues/307): New `list` field which allows to create a list of items
- [#345](https://github.com/estruyf/vscode-front-matter/issues/345): Media dashboard UI improvements to visualize the content and public folders
- [#349](https://github.com/estruyf/vscode-front-matter/issues/349): New `slug` field which allows you to manage the slug of your post from the Front Matter panel
- [#350](https://github.com/estruyf/vscode-front-matter/issues/350): New `previewPath` property for the `frontMatter.content.pageFolders` setting. This allows you to specify a section prefix for all content created in that directory.
- [#351](https://github.com/estruyf/vscode-front-matter/issues/351): New `template` property for content types which allows you to combine templates and content types for content creation

### 🐞 Fixes
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@
"type": "boolean",
"default": false,
"description": "Exclude sub-directories"
},
"previewPath": {
"type": [
"null",
"string"
],
"default": null,
"description": "Defines a custom preview path for the folder."
}
},
"additionalProperties": false,
Expand Down
27 changes: 25 additions & 2 deletions src/commands/Preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { SETTING_PREVIEW_HOST, SETTING_PREVIEW_PATHNAME, CONTEXT, TelemetryEvent
import { ArticleHelper } from './../helpers/ArticleHelper';
import { join } from "path";
import { commands, env, Uri, ViewColumn, window } from "vscode";
import { Extension, Settings } from '../helpers';
import { PreviewSettings } from '../models';
import { Extension, parseWinPath, Settings } from '../helpers';
import { ContentFolder, PreviewSettings } from '../models';
import { format } from 'date-fns';
import { DateHelper } from '../helpers/DateHelper';
import { Article } from '.';
import { urlJoin } from 'url-join-ts';
import { WebviewHelper } from '@estruyf/vscode';
import { Folders } from './Folders';


export class Preview {
Expand Down Expand Up @@ -37,6 +38,28 @@ export class Preview {
let slug = article?.data ? article.data.slug : "";

let pathname = settings.pathname;

// Check if there is a pathname defined on content folder level
const folders = Folders.get();
if (folders.length > 0) {
const foldersWithPath = folders.filter(folder => folder.previewPath);
const filePath = parseWinPath(editor?.document.uri.fsPath);

let selectedFolder: ContentFolder | null = null;
for (const folder of foldersWithPath) {
if (filePath.startsWith(folder.path)) {
if (!selectedFolder || selectedFolder.path.length < folder.path.length) {
selectedFolder = folder;
}
}
}

if (selectedFolder) {
pathname = selectedFolder.previewPath;
}
}

// Check if there is a pathname defined on content type level
if (article?.data) {
const contentType = ArticleHelper.getContentType(article.data);
if (contentType && contentType.previewPath) {
Expand Down
1 change: 1 addition & 0 deletions src/models/ContentFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface ContentFolder {
path: string;

excludeSubdir?: boolean;
previewPath?: string;
}

0 comments on commit 1a6acce

Please sign in to comment.