Added the path to the parsed article

This commit is contained in:
Elio Struyf
2023-10-31 11:44:02 +01:00
parent b60aadc4a3
commit 14ddd8b53c
14 changed files with 98 additions and 47 deletions
+60 -17
View File
@@ -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
};
}
/**
@@ -79,7 +89,10 @@ export class ArticleHelper {
return;
}
return article;
return {
...article,
path: editor.document.uri.fsPath
};
}
/**
@@ -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
};
}
/**
@@ -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;
}
@@ -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 (
@@ -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 (
@@ -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);
}
@@ -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;
}
/**
+9 -9
View File
@@ -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) {
@@ -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() || [];
@@ -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;
}
@@ -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);
+2 -2
View File
@@ -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;
@@ -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> {
+1 -1
View File
@@ -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));
+2 -2
View File
@@ -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);
@@ -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);