Skip to content

Commit

Permalink
Added the path to the parsed article
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Oct 31, 2023
1 parent b60aadc commit 14ddd8b
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/commands/Article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class Article {
* @param article
*/
public static updateDate(article: ParsedFrontMatter) {
article.data = ArticleHelper.updateDates(article.data);
article.data = ArticleHelper.updateDates(article);
return article;
}

Expand Down Expand Up @@ -227,7 +227,7 @@ export class Article {
}

let filePrefix = Settings.get<string>(SETTING_TEMPLATES_PREFIX);
const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);
filePrefix = ArticleHelper.getFilePrefix(filePrefix, editor.document.uri.fsPath, contentType);

const titleField = 'title';
Expand Down Expand Up @@ -393,7 +393,7 @@ export class Article {

const article = ArticleHelper.getFrontMatter(editor);
const contentType =
article && article.data ? ArticleHelper.getContentType(article.data) : DEFAULT_CONTENT_TYPE;
article && article.data ? ArticleHelper.getContentType(article) : DEFAULT_CONTENT_TYPE;

const position = editor.selection.active;
const selectionText = editor.document.getText(editor.selection);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/Preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class Preview {
* @returns
*/
public static async getContentSlug(
article: ParsedFrontMatter | null,
article: ParsedFrontMatter | null | undefined,
filePath?: string
): Promise<string | undefined> {
if (!filePath) {
Expand All @@ -230,7 +230,7 @@ export class Preview {

let contentType: ContentType | undefined = undefined;
if (article?.data) {
contentType = ArticleHelper.getContentType(article.data);
contentType = ArticleHelper.getContentType(article);
}

// Check if there is a pathname defined on content folder level
Expand Down
8 changes: 6 additions & 2 deletions src/commands/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,15 @@ export class Template {
newFilePath
);

frontMatter = Article.updateDate(frontMatter);
const article = Article.updateDate(frontMatter);

if (!article) {
return;
}

await writeFileAsync(
newFilePath,
ArticleHelper.stringifyFrontMatter(frontMatter.content, frontMatter.data),
ArticleHelper.stringifyFrontMatter(article.content, article.data),
{ encoding: 'utf8' }
);

Expand Down
2 changes: 1 addition & 1 deletion src/dashboardWebView/components/Header/Searchbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const Searchbox: React.FunctionComponent<ISearchboxProps> = ({
name="search"
className={`block w-full py-2 pl-10 pr-3 sm:text-sm appearance-none disabled:opacity-50 rounded ${getColors(
'bg-white dark:bg-vulcan-300 border border-gray-300 dark:border-vulcan-100 text-vulcan-500 dark:text-whisper-500 placeholder-gray-400 dark:placeholder-whisper-800 focus:outline-none',
'bg-[var(--vscode-input-background)] text-[var(--vscode-input-foreground)] border-[var(--vscode-input-border)] placeholder-[var(--vscode-input-placeholderForeground)] focus:outline-[var(--vscode-focusBorder)] focus:outline-1 focus:outline-offset-0 focus:shadow-none focus:border-transparent'
'bg-[var(--vscode-input-background)] text-[var(--vscode-input-foreground)] border-[var(--vscode-input-border, --vscode-editorWidget-border)] placeholder-[var(--vscode-input-placeholderForeground)] focus:outline-[var(--vscode-focusBorder)] focus:outline-1 focus:outline-offset-0 focus:shadow-none focus:border-transparent'
)
}`}
placeholder={placeholder || l10n.t(LocalizationKey.commonSearch)}
Expand Down
77 changes: 60 additions & 17 deletions src/helpers/ArticleHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ export class ArticleHelper {
*
* @param document The document to parse.
*/
public static getFrontMatterFromDocument(document: vscode.TextDocument) {
public static getFrontMatterFromDocument(
document: vscode.TextDocument
): ParsedFrontMatter | undefined {
const fileContents = document.getText();
return ArticleHelper.parseFile(fileContents, document.fileName);
const article = ArticleHelper.parseFile(fileContents, document.fileName);
if (!article) {
return undefined;
}

return {
...article,
path: document.uri.fsPath
};
}

/**
Expand All @@ -79,7 +89,10 @@ export class ArticleHelper {
return;
}

return article;
return {
...article,
path: editor.document.uri.fsPath
};
}

/**
Expand All @@ -88,7 +101,15 @@ export class ArticleHelper {
*/
public static async getFrontMatterByPath(filePath: string) {
const file = await readFileAsync(filePath, { encoding: 'utf-8' });
return ArticleHelper.parseFile(file, filePath);
const article = ArticleHelper.parseFile(file, filePath);
if (!article) {
return undefined;
}

return {
...article,
path: filePath
};
}

/**
Expand Down Expand Up @@ -240,7 +261,7 @@ export class ArticleHelper {
/**
* Get date from front matter
*/
public static getDate(article: ParsedFrontMatter | null) {
public static getDate(article: ParsedFrontMatter | null | undefined) {
if (!article || !article.data) {
return;
}
Expand Down Expand Up @@ -270,7 +291,7 @@ export class ArticleHelper {
return;
}

const articleCt = ArticleHelper.getContentType(article.data);
const articleCt = ArticleHelper.getContentType(article);
const pubDateField = articleCt.fields.find((f) => f.isPublishDate);

return (
Expand All @@ -290,7 +311,7 @@ export class ArticleHelper {
return;
}

const articleCt = ArticleHelper.getContentType(article.data);
const articleCt = ArticleHelper.getContentType(article);
const modDateField = articleCt.fields.find((f) => f.isModifiedDate);

return (
Expand All @@ -312,16 +333,38 @@ export class ArticleHelper {
* Retrieve the content type of the current file
* @param updatedMetadata
*/
public static getContentType(metadata: { [field: string]: string }): IContentType {
public static getContentType(article: ParsedFrontMatter): IContentType {
const contentTypes = ArticleHelper.getContentTypes();

if (!contentTypes || !metadata) {
if (!contentTypes || !article.data) {
return DEFAULT_CONTENT_TYPE;
}

let contentType = contentTypes.find(
(ct) => ct.name === (metadata.type || DEFAULT_CONTENT_TYPE_NAME)
);
let contentType: IContentType | undefined = undefined;

// Get content type by type name in the front matter
if (article.data.type) {
contentType = contentTypes.find((ct) => ct.name === article.data.type);
} else if (!contentType && article.path) {
// Get the content type by the folder name
let folders = Folders.get();
let parsedPath = parseWinPath(article.path);
let pageFolderMatches = folders.filter(
(folder) => parsedPath && folder.path && parsedPath.includes(folder.path)
);

// Sort by longest path
pageFolderMatches = pageFolderMatches.sort((a, b) => b.path.length - a.path.length);
if (
pageFolderMatches.length > 0 &&
pageFolderMatches[0].contentTypes &&
pageFolderMatches[0].contentTypes.length === 1
) {
const contentTypeName = pageFolderMatches[0].contentTypes[0];
contentType = contentTypes.find((ct) => ct.name === contentTypeName);
}
}

if (!contentType) {
contentType = contentTypes.find((ct) => ct.name === DEFAULT_CONTENT_TYPE_NAME);
}
Expand All @@ -343,17 +386,17 @@ export class ArticleHelper {
* Update all dates in the metadata
* @param metadata
*/
public static updateDates(metadata: { [field: string]: string }) {
const contentType = ArticleHelper.getContentType(metadata);
public static updateDates(article: ParsedFrontMatter) {
const contentType = ArticleHelper.getContentType(article);
const dateFields = contentType.fields.filter((field) => field.type === 'datetime');

for (const dateField of dateFields) {
if (typeof metadata[dateField.name] !== 'undefined') {
metadata[dateField.name] = Article.formatDate(new Date(), dateField.dateFormat);
if (typeof article?.data[dateField.name] !== 'undefined') {
article.data[dateField.name] = Article.formatDate(new Date(), dateField.dateFormat);
}
}

return metadata;
return article.data;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/helpers/ContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ export class ContentType {
* @param data
* @returns
*/
public static getDraftStatus(data: { [field: string]: any }) {
const contentType = ArticleHelper.getContentType(data);
public static getDraftStatus(article: ParsedFrontMatter) {
const contentType = ArticleHelper.getContentType(article);
const draftSetting = ContentType.getDraftField();

const draftField = contentType.fields.find((f) => f.type === 'draft');

let fieldValue = null;

if (draftField) {
fieldValue = data[draftField.name];
} else if (draftSetting && data && data[draftSetting.name]) {
fieldValue = data[draftSetting.name];
if (draftField && article?.data) {
fieldValue = article?.data[draftField.name];
} else if (draftSetting && article?.data && article?.data[draftSetting.name]) {
fieldValue = article?.data[draftSetting.name];
}

if (draftSetting && fieldValue !== null) {
Expand Down Expand Up @@ -282,7 +282,7 @@ export class ContentType {
return;
}

const contentType = ArticleHelper.getContentType(content?.data);
const contentType = ArticleHelper.getContentType(content);
const updatedFields = ContentType.generateFields(content.data, contentType.fields);

const contentTypes = ContentType.getAll() || [];
Expand Down Expand Up @@ -492,7 +492,7 @@ export class ContentType {
* Find the required fields
*/
public static findEmptyRequiredFields(article: ParsedFrontMatter): Field[][] | undefined {
const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);
if (!contentType) {
return;
}
Expand Down Expand Up @@ -793,7 +793,7 @@ export class ContentType {
}

let templatePath = contentType.template;
let templateData: ParsedFrontMatter | null = null;
let templateData: ParsedFrontMatter | null | undefined = null;
if (templatePath) {
templatePath = Folders.getAbsFilePath(templatePath);
templateData = await ArticleHelper.getFrontMatterByPath(templatePath);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/CustomScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class CustomScript {
path: string | null = null
): Promise<void> {
let articlePath: string | null = path;
let article: ParsedFrontMatter | null = null;
let article: ParsedFrontMatter | null | undefined = null;

if (!path) {
const editor = window.activeTextEditor;
Expand Down Expand Up @@ -214,7 +214,7 @@ export class CustomScript {
*/
private static async runScript(
wsPath: string,
article: ParsedFrontMatter | null,
article: ParsedFrontMatter | null | undefined,
contentPath: string,
script: ICustomScript
): Promise<string | null> {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/MediaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ export class MediaHelpers {

const article = editor ? ArticleHelper.getFrontMatter(editor) : null;
const articleCt =
article && article.data ? ArticleHelper.getContentType(article.data) : DEFAULT_CONTENT_TYPE;
article && article.data ? ArticleHelper.getContentType(article) : DEFAULT_CONTENT_TYPE;

const absImgPath = join(parseWinPath(wsFolder?.fsPath || ''), relPath);
const fileDir = parseWinPath(dirname(filePath));
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/TaxonomyHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export class TaxonomyHelper {
if (mdFile) {
try {
const article = FrontMatterParser.fromFile(mdFile);
const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);

let fieldNames: string[] = this.getFieldsHierarchy(taxonomyType, contentType);

Expand Down Expand Up @@ -415,7 +415,7 @@ export class TaxonomyHelper {
if (mdFile) {
try {
const article = FrontMatterParser.fromFile(mdFile);
const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);

let oldFieldNames: string[] = this.getFieldsHierarchy(oldType, contentType);
let newFieldNames: string[] = this.getFieldsHierarchy(newType, contentType, true);
Expand Down
7 changes: 5 additions & 2 deletions src/listeners/dashboard/PagesListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class PagesListener extends BaseListener {
if (!article) {
return;
}
const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);

Logger.info(`Deleting file: ${path}`);

Expand Down Expand Up @@ -240,7 +240,10 @@ export class PagesListener extends BaseListener {
* @param pages
*/
private static async createSearchIndex(pages: Page[]) {
const pagesIndex = Fuse.createIndex(['title', 'slug', 'description', 'fmBody', 'type'], pages);
const pagesIndex = Fuse.createIndex(
['title', 'slug', 'description', 'fmBody', 'type', 'fmContentType'],
pages
);
await Extension.getInstance().setState(
ExtensionState.Dashboard.Pages.Index,
pagesIndex.toJSON(),
Expand Down
4 changes: 2 additions & 2 deletions src/listeners/panel/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class DataListener extends BaseListener {
return;
}

const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);

if (!value && field !== titleField && contentType.clearEmpty) {
value = undefined;
Expand Down Expand Up @@ -374,7 +374,7 @@ export class DataListener extends BaseListener {
) {
let parentObj = data;
let allParents = Object.assign([], parents);
const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);
let selectedIndexes: number[] = [];
if (blockData?.selectedIndex) {
if (typeof blockData.selectedIndex === 'string') {
Expand Down
4 changes: 2 additions & 2 deletions src/listeners/panel/FieldsListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class FieldsListener extends BaseListener {

PagesListener.getPagesData(false, async (pages) => {
const fuseOptions: Fuse.IFuseOptions<Page> = {
keys: [{ name: 'type', weight: 1 }]
keys: [{ name: 'fmContentType', weight: 1 }]
};

const pagesIndex = await Extension.getInstance().getState<Fuse.FuseIndex<Page>>(
Expand All @@ -48,7 +48,7 @@ export class FieldsListener extends BaseListener {
const results = fuse.search({
$and: [
{
type
fmContentType: type
}
]
});
Expand Down
1 change: 1 addition & 0 deletions src/parsers/FrontMatterParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Format {
export interface ParsedFrontMatter {
data: { [key: string]: any };
content: string;
path?: string;
}

export class FrontMatterParser {
Expand Down
4 changes: 2 additions & 2 deletions src/services/PagesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class PagesParser {

const dateField = ArticleHelper.getPublishDateField(article) || DefaultFields.PublishingDate;

const contentType = ArticleHelper.getContentType(article.data);
const contentType = ArticleHelper.getContentType(article);
let dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;
const ctDateField = ContentType.findFieldByName(contentType.fields, dateField);
if (ctDateField && ctDateField.dateFormat) {
Expand Down Expand Up @@ -211,7 +211,7 @@ export class PagesParser {
fmRelFileWsPath: FilesHelper.absToRelPath(filePath),
fmRelFilePath: parseWinPath(filePath).replace(wsFolder?.fsPath || '', ''),
fmFileName: fileName,
fmDraft: ContentType.getDraftStatus(article?.data),
fmDraft: ContentType.getDraftStatus(article),
fmModified: modifiedFieldValue ? modifiedFieldValue : fileMtime,
fmPublished: dateFieldValue ? dateFieldValue.getTime() : null,
fmYear: dateFieldValue ? dateFieldValue.getFullYear() : null,
Expand Down

0 comments on commit 14ddd8b

Please sign in to comment.