Skip to content

Commit

Permalink
Merge branch 'hotfix/10.2.1' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jun 14, 2024
2 parents 6205675 + 1e1c0ce commit 314cadb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 44 deletions.
6 changes: 5 additions & 1 deletion src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class Article {
/**
* Retrieve the slug from the front matter
*/
public static getSlug() {
public static getSlug(pathname?: string) {
const editor = window.activeTextEditor;
if (!editor) {
return;
Expand Down Expand Up @@ -329,6 +329,10 @@ export class Article {
return `${prefix}${parsedFile.name}${suffix}`;
}

if (parsedFile.name.toLowerCase() === 'index' && pathname) {
return ``;
}

const folderName = basename(dirname(file));
return folderName;
}
Expand Down
57 changes: 54 additions & 3 deletions src/commands/Folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import {
} from './../constants';
import { commands, Uri, workspace, window } from 'vscode';
import { basename, dirname, join, relative, sep } from 'path';
import { ContentFolder, FileInfo, FolderInfo, I18nConfig, StaticFolder } from '../models';
import {
ContentFolder,
ContentType,
FileInfo,
FolderInfo,
I18nConfig,
StaticFolder
} from '../models';
import uniqBy = require('lodash.uniqby');
import { Template } from './Template';
import { Notifications } from '../helpers/Notifications';
Expand All @@ -29,6 +36,7 @@ import { mkdirAsync } from '../utils/mkdirAsync';
import { existsAsync, isWindows } from '../utils';
import * as l10n from '@vscode/l10n';
import { LocalizationKey } from '../localization';
import { Preview } from './Preview';

export const WORKSPACE_PLACEHOLDER = `[[workspace]]`;

Expand Down Expand Up @@ -640,7 +648,7 @@ export class Folders {
* @param filePath
* @returns
*/
public static async getFilePrefixBeFilePath(filePath: string) {
public static async getFilePrefixBeFilePath(filePath: string): Promise<string | undefined> {
const folders = await Folders.get();
if (folders.length > 0) {
filePath = parseWinPath(filePath);
Expand Down Expand Up @@ -672,7 +680,7 @@ export class Folders {
public static async getPageFolderByFilePath(
filePath: string
): Promise<ContentFolder | undefined> {
const folders = await Folders.get();
const folders = Folders.getCached();
const parsedPath = parseWinPath(filePath);
const pageFolderMatches = folders
.filter((folder) => parsedPath && folder.path && parsedPath.includes(folder.path))
Expand All @@ -685,6 +693,49 @@ export class Folders {
return;
}

/**
* Retrieves the folder associated with the specified content type and file path.
* If a single matching folder is found, it is returned. If multiple matching folders are found,
* the user is prompted to select one. If no matching folders are found, the user is prompted to
* select a folder with a preview path.
*
* @param contentType - The content type to match.
* @param filePath - The file path to match.
* @returns A Promise that resolves to the selected ContentFolder, or undefined if no matching folder is found.
*/
public static async getFolderByContentType(
contentType: ContentType,
filePath: string
): Promise<ContentFolder | undefined> {
if (!contentType) {
return;
}

const folders = Folders.getCached();
let selectedFolder: ContentFolder | undefined;

// Try to find the folder by content type
let crntFolders = folders.filter(
(folder) =>
folder.contentTypes?.includes((contentType as ContentType).name) && folder.previewPath
);

// Use file path to find the folder
if (crntFolders.length > 0) {
crntFolders = crntFolders.filter((folder) => filePath?.startsWith(folder.path));
}

if (crntFolders && crntFolders.length === 1) {
selectedFolder = crntFolders[0];
} else if (crntFolders && crntFolders.length > 1) {
selectedFolder = await Preview.askUserToPickFolder(crntFolders);
} else {
selectedFolder = await Preview.askUserToPickFolder(folders.filter((f) => f.previewPath));
}

return selectedFolder;
}

/**
* Retrieves the file stats for a given file.
* @param file - The URI of the file.
Expand Down
47 changes: 10 additions & 37 deletions src/commands/Preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,42 +247,15 @@ export class Preview {
contentType = await ArticleHelper.getContentType(article);
}

// Check if there is a pathname defined on content folder level
const folders = await Folders.get();
if (folders.length > 0) {
for (const folder of folders) {
const folderPath = parseWinPath(folder.path);
if (filePath.startsWith(folderPath)) {
if (!selectedFolder || selectedFolder.path.length < folderPath.length) {
selectedFolder = folder;
}
}
}

if (!selectedFolder && article?.data && contentType && !contentType.previewPath) {
// Try to find the folder by content type
let crntFolders = folders.filter(
(folder) =>
folder.contentTypes?.includes((contentType as ContentType).name) && folder.previewPath
);

// Use file path to find the folder
if (crntFolders.length > 0) {
crntFolders = crntFolders.filter((folder) => filePath?.startsWith(folder.path));
}
// Get the folder of the article by the file path
selectedFolder = await Folders.getPageFolderByFilePath(filePath);

if (crntFolders && crntFolders.length === 1) {
selectedFolder = crntFolders[0];
} else if (crntFolders && crntFolders.length > 1) {
selectedFolder = await Preview.askUserToPickFolder(crntFolders);
} else {
selectedFolder = await Preview.askUserToPickFolder(folders.filter((f) => f.previewPath));
}
}
if (!selectedFolder && contentType) {
selectedFolder = await Folders.getFolderByContentType(contentType, filePath);
}

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

// Check if there is a pathname defined on content type level
Expand All @@ -293,7 +266,7 @@ export class Preview {
}

if (!slug) {
slug = Article.getSlug();
slug = Article.getSlug(pathname);
}

const locale = await i18n.getLocale(filePath);
Expand Down Expand Up @@ -372,7 +345,7 @@ export class Preview {
slug = `${slug}/`;
}

return slug;
return join(slug);
}

/**
Expand Down Expand Up @@ -422,7 +395,7 @@ export class Preview {
* @param crntFolders
* @returns
*/
private static async askUserToPickFolder(
public static async askUserToPickFolder(
crntFolders: ContentFolder[]
): Promise<ContentFolder | undefined> {
let selectedFolder: ContentFolder | undefined = undefined;
Expand Down
16 changes: 14 additions & 2 deletions src/listeners/panel/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,21 @@ export class DataListener extends BaseListener {

// Check slug
if (!slugField && !updatedMetadata[DefaultFields.Slug]) {
const slug = Article.getSlug();
let pathname = contentType.previewPath || '';
if (!pathname) {
const selectedFolder = await Folders.getPageFolderByFilePath(filePath);
if (selectedFolder && selectedFolder.previewPath) {
pathname = selectedFolder.previewPath;
}
}

if (!pathname) {
pathname = Preview.getSettings().pathname || '';
}

const slug = Article.getSlug(pathname);

if (slug) {
if (typeof slug !== 'undefined') {
updatedMetadata[DefaultFields.Slug] = slug;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/panelWebView/components/Actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const Actions: React.FunctionComponent<IActionsProps> = ({
}

if (settings?.preview?.host && !disableActions.includes(`preview`)) {
if ((metadata && metadata.slug) || !metadata) {
if ((metadata && typeof metadata.slug !== "undefined") || !metadata) {
allActions.push(<Preview key="preview" />);
}
}
Expand Down

0 comments on commit 314cadb

Please sign in to comment.