From c9f4c8f94e3e3c179d74867c82f30a18729eadde Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 17 Dec 2021 19:04:41 +0100 Subject: [PATCH 1/5] #206 - Added front matter diagnostic parsing --- CHANGELOG.md | 1 + src/helpers/ArticleHelper.ts | 31 +++++++++- src/helpers/Extension.ts | 11 +++- src/providers/MarkdownFoldingProvider.ts | 73 ++++++++++++++++-------- 4 files changed, 88 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db9bc4fb..a3bd6557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Optimized the `getMedia` call from the webview - Keep the dashboard its context when switching tabs - [#205](https://github.com/estruyf/vscode-front-matter/issues/205): Define a logging level setting +- [#206](https://github.com/estruyf/vscode-front-matter/issues/206): Add front matter issues to the diagnostic tab ## [5.7.0] - 2021-12-07 - [Release Notes](https://beta.frontmatter.codes/updates/v5.7.0) diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 6a4057cd..6a2f22f6 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -1,3 +1,4 @@ +import { MarkdownFoldingProvider } from './../providers/MarkdownFoldingProvider'; import { DEFAULT_CONTENT_TYPE, DEFAULT_CONTENT_TYPE_NAME } from './../constants/ContentType'; import * as vscode from 'vscode'; import * as matter from "gray-matter"; @@ -5,7 +6,7 @@ import * as fs from "fs"; import { DefaultFields, SETTING_COMMA_SEPARATED_FIELDS, SETTING_DATE_FIELD, SETTING_DATE_FORMAT, SETTING_INDENT_ARRAY, SETTING_REMOVE_QUOTES, SETTING_TAXONOMY_CONTENT_TYPES, SETTING_TEMPLATES_PREFIX } from '../constants'; import { DumpOptions } from 'js-yaml'; import { TomlEngine, getFmLanguage, getFormatOpts } from './TomlEngine'; -import { Settings } from '.'; +import { Extension, Settings } from '.'; import { format, parse } from 'date-fns'; import { Notifications } from './Notifications'; import { Article } from '../commands'; @@ -15,6 +16,7 @@ import sanitize from '../helpers/Sanitize'; import { existsSync, mkdirSync } from 'fs'; import { ContentType } from '../models'; import { DateHelper } from './DateHelper'; +import { Diagnostic, DiagnosticSeverity, Position, window, Range } from 'vscode'; export class ArticleHelper { private static notifiedFiles: string[] = []; @@ -229,6 +231,7 @@ export class ArticleHelper { private static parseFile(fileContents: string, fileName: string, surpressNotification: boolean = false): matter.GrayMatterFile | null { try { const commaSeparated = Settings.get(SETTING_COMMA_SEPARATED_FIELDS); + const diagnostics: Diagnostic[] = []; if (fileContents) { const language: string = getFmLanguage(); @@ -249,6 +252,10 @@ export class ArticleHelper { this.notifiedFiles = this.notifiedFiles.filter(n => n !== fileName); + if (window.activeTextEditor?.document.uri) { + Extension.getInstance().diagnosticCollection.delete(window.activeTextEditor.document.uri); + } + return article; } } @@ -260,6 +267,28 @@ export class ArticleHelper { } }]; + const editor = window.activeTextEditor; + if (editor?.document.uri) { + let fmRange = null; + + if (error?.mark && typeof error.mark.line !== "undefined") { + const curLineText = editor.document.lineAt(error.mark.line - 1); + const lastCharPos = new Position(error.mark.line - 1, Math.max(curLineText.text.length, 0)); + fmRange = new Range(new Position(error.mark.line - 1, 0), lastCharPos); + } else { + fmRange = MarkdownFoldingProvider.getFrontMatterRange(editor.document); + } + + + if (fmRange) { + Extension.getInstance().diagnosticCollection.set(editor.document.uri, [{ + severity: DiagnosticSeverity.Error, + message: `${error.name ? `${error.name}: ` : ""}Error parsing the front matter of ${fileName}`, + range: fmRange + }]); + } + } + if (!surpressNotification) { if (this.notifiedFiles.indexOf(fileName) === -1) { Notifications.error(`There seems to be an issue parsing the content its front matter. FileName: ${basename(fileName)}. ERROR: ${error.message || error}`, ...items).then((result: any) => { diff --git a/src/helpers/Extension.ts b/src/helpers/Extension.ts index 264bec97..0900ed54 100644 --- a/src/helpers/Extension.ts +++ b/src/helpers/Extension.ts @@ -1,6 +1,6 @@ import { existsSync, renameSync } from "fs"; import { basename, join } from "path"; -import { extensions, Uri, ExtensionContext, window, workspace, commands, ExtensionMode } from "vscode"; +import { extensions, Uri, ExtensionContext, window, workspace, commands, ExtensionMode, DiagnosticCollection, languages } from "vscode"; import { Folders, WORKSPACE_PLACEHOLDER } from "../commands/Folders"; import { EXTENSION_NAME, GITHUB_LINK, SETTINGS_CONTENT_FOLDERS, SETTINGS_CONTENT_PAGE_FOLDERS, SETTING_DATE_FIELD, SETTING_MODIFIED_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTING_TAXONOMY_CONTENT_TYPES, DEFAULT_CONTENT_TYPE_NAME, EXTENSION_BETA_ID, EXTENSION_ID, ExtensionState, DefaultFields, LocalStore, SETTING_TEMPLATES_FOLDER } from "../constants"; import { ContentType } from "../models"; @@ -11,8 +11,11 @@ import { Settings } from "./SettingsHelper"; export class Extension { private static instance: Extension; + private _collection: DiagnosticCollection; - private constructor(private ctx: ExtensionContext) {} + private constructor(private ctx: ExtensionContext) { + this._collection = languages.createDiagnosticCollection(this.title); + } /** * Creates the singleton instance for the panel @@ -88,6 +91,10 @@ export class Extension { return this.ctx.extensionMode === ExtensionMode.Production; } + public get diagnosticCollection(): DiagnosticCollection { + return this._collection; + } + /** * Set the current version information for the extension */ diff --git a/src/providers/MarkdownFoldingProvider.ts b/src/providers/MarkdownFoldingProvider.ts index 17d0fb1f..f3e5e6e2 100644 --- a/src/providers/MarkdownFoldingProvider.ts +++ b/src/providers/MarkdownFoldingProvider.ts @@ -13,31 +13,15 @@ export class MarkdownFoldingProvider implements FoldingRangeProvider { public async provideFoldingRanges(document: TextDocument, context: FoldingContext, token: CancellationToken): Promise { const ranges: FoldingRange[] = []; - const lines = document.getText().split('\n'); - let start: number | null = null; - let end: number | null = null; + const range = MarkdownFoldingProvider.getFrontMatterRange(document); + if (range) { + MarkdownFoldingProvider.start = null; + MarkdownFoldingProvider.end = null; + MarkdownFoldingProvider.endLine = null; - MarkdownFoldingProvider.start = null; - MarkdownFoldingProvider.end = null; - MarkdownFoldingProvider.endLine = null; + MarkdownFoldingProvider.triggerHighlighting(); - for (let i = 0; i < lines.length; i++) { - const line = lines[i]; - if (line.startsWith('---')) { - if (i === 0 && start === null) { - start = i; - MarkdownFoldingProvider.start = start; - } else if (start !== null && end === null) { - end = i; - MarkdownFoldingProvider.end = end; - MarkdownFoldingProvider.endLine = line.length; - - MarkdownFoldingProvider.triggerHighlighting(); - - ranges.push(new FoldingRange(start, end, FoldingRangeKind.Region)); - return ranges; - } - } + ranges.push(new FoldingRange(range.start.line, range.end.line, FoldingRangeKind.Region)); } return ranges; @@ -46,9 +30,9 @@ export class MarkdownFoldingProvider implements FoldingRangeProvider { public static triggerHighlighting() { const fmHighlight = Settings.get(SETTINGS_CONTENT_FRONTMATTER_HIGHLIGHT); - if (MarkdownFoldingProvider.start !== null && MarkdownFoldingProvider.end !== null && MarkdownFoldingProvider.endLine !== null) { - const range = new Range(new Position(MarkdownFoldingProvider.start, 0), new Position(MarkdownFoldingProvider.end, MarkdownFoldingProvider.endLine)); + const range = this.getFrontMatterRange(); + if (range) { if (MarkdownFoldingProvider.decType !== null) { MarkdownFoldingProvider.decType.dispose(); } @@ -59,4 +43,43 @@ export class MarkdownFoldingProvider implements FoldingRangeProvider { } } } + + /** + * Retrieve the range of the current Front Matter page + * @param document + * @returns + */ + public static getFrontMatterRange(document?: TextDocument) { + if (document) { + const lines = document.getText().split('\n'); + + let start = null; + let end = null; + let endLine = null; + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + if (line.startsWith('---')) { + if (i === 0 && start === null) { + start = i; + } else if (start !== null && end === null) { + end = i; + endLine = line.length; + + MarkdownFoldingProvider.triggerHighlighting(); + + return new Range(new Position(start, 0), new Position(end, endLine)); + } + } + } + } + + if (MarkdownFoldingProvider.start !== null && MarkdownFoldingProvider.end !== null && MarkdownFoldingProvider.endLine !== null) { + const range = new Range(new Position(MarkdownFoldingProvider.start, 0), new Position(MarkdownFoldingProvider.end, MarkdownFoldingProvider.endLine)); + + return range; + } + + return null; + } } \ No newline at end of file From e576b6e8a423768b67c5368f9a306edcb8b06f14 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 17 Dec 2021 19:21:41 +0100 Subject: [PATCH 2/5] #207 - fix quick picks --- CHANGELOG.md | 4 ++++ src/helpers/Questions.ts | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3bd6557..79d459d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ - [#205](https://github.com/estruyf/vscode-front-matter/issues/205): Define a logging level setting - [#206](https://github.com/estruyf/vscode-front-matter/issues/206): Add front matter issues to the diagnostic tab +### 🐞 Fixes + +- [#207](https://github.com/estruyf/vscode-front-matter/issues/207): Fix the quick picks for content creation + ## [5.7.0] - 2021-12-07 - [Release Notes](https://beta.frontmatter.codes/updates/v5.7.0) ### 🎨 Enhancements diff --git a/src/helpers/Questions.ts b/src/helpers/Questions.ts index 1051dfd3..2252fd8a 100644 --- a/src/helpers/Questions.ts +++ b/src/helpers/Questions.ts @@ -45,7 +45,8 @@ export class Questions { let selectedFolder: string | undefined; if (folders.length > 1) { selectedFolder = await window.showQuickPick(folders.map(f => f.title), { - placeHolder: `Select where you want to create your content` + placeHolder: `Select where you want to create your content`, + ignoreFocusOut: true }); } else { selectedFolder = folders[0].title; From af9865d91bb0ecc9ff5b74de69e57f94689d4e59 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 17 Dec 2021 19:21:59 +0100 Subject: [PATCH 3/5] Refresh the pages on content creation --- src/commands/Template.ts | 4 ++++ src/helpers/ContentType.ts | 4 ++++ src/listeners/PagesListener.ts | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/commands/Template.ts b/src/commands/Template.ts index c7cdecd1..1bd112bc 100644 --- a/src/commands/Template.ts +++ b/src/commands/Template.ts @@ -11,6 +11,7 @@ import { Project } from './Project'; import { Folders } from './Folders'; import { ContentType } from '../helpers/ContentType'; import { ContentType as IContentType } from '../models'; +import { PagesListener } from '../listeners'; export class Template { @@ -176,6 +177,9 @@ export class Template { } Notifications.info(`Your new content has been created.`); + + // Trigger a refresh for the dashboard + PagesListener.refresh(); } /** diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 6a7cc15c..55fc33ec 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -1,3 +1,4 @@ +import { PagesListener } from './../listeners/PagesListener'; import { ArticleHelper, Settings } from "."; import { SETTINGS_CONTENT_DRAFT_FIELD, SETTING_TAXONOMY_CONTENT_TYPES } from "../constants"; import { ContentType as IContentType, DraftField } from '../models'; @@ -128,5 +129,8 @@ export class ContentType { } Notifications.info(`Your new content has been created.`); + + // Trigger a refresh for the dashboard + PagesListener.refresh(); } } \ No newline at end of file diff --git a/src/listeners/PagesListener.ts b/src/listeners/PagesListener.ts index a837bc11..7296a9f5 100644 --- a/src/listeners/PagesListener.ts +++ b/src/listeners/PagesListener.ts @@ -124,4 +124,8 @@ export class PagesListener extends BaseListener { this.sendMsg(DashboardCommand.pages, pages); } + + public static refresh() { + this.getPagesData(); + } } \ No newline at end of file From 87e80ccfe96185497b25c5ecb095df46c9b5d882 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Sat, 18 Dec 2021 21:15:57 +0100 Subject: [PATCH 4/5] Update readme --- README.beta.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.beta.md b/README.beta.md index e5649abd..fb5916bc 100644 --- a/README.beta.md +++ b/README.beta.md @@ -106,7 +106,7 @@ If you have the courage to test out the beta features, we made available a beta

-## 🖤 Sponsors 👇 🤘 +## 🖤 Backers & Sponsors 👇 🤘

diff --git a/README.md b/README.md index 28be793d..76f6cb36 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ If you have the courage to test out the beta features, we made available a beta

-## 🖤 Sponsors 👇 🤘 +## 🖤 Backers & Sponsors 👇 🤘

From 6da3ddb5dd56551d5d7703b71c01844f53b0e1b3 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 20 Dec 2021 20:38:56 +0100 Subject: [PATCH 5/5] #206 - Removal of the front matter parsing error notification --- src/helpers/ArticleHelper.ts | 22 +++------------------- src/helpers/CustomScript.ts | 2 +- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 6a2f22f6..1304218f 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -35,9 +35,9 @@ export class ArticleHelper { * Retrieve the file's front matter by its path * @param filePath */ - public static getFrontMatterByPath(filePath: string, surpressNotification: boolean = false) { + public static getFrontMatterByPath(filePath: string) { const file = fs.readFileSync(filePath, { encoding: "utf-8" }); - return ArticleHelper.parseFile(file, filePath, surpressNotification); + return ArticleHelper.parseFile(file, filePath); } /** @@ -228,10 +228,9 @@ export class ArticleHelper { * @param fileContents * @returns */ - private static parseFile(fileContents: string, fileName: string, surpressNotification: boolean = false): matter.GrayMatterFile | null { + private static parseFile(fileContents: string, fileName: string): matter.GrayMatterFile | null { try { const commaSeparated = Settings.get(SETTING_COMMA_SEPARATED_FIELDS); - const diagnostics: Diagnostic[] = []; if (fileContents) { const language: string = getFmLanguage(); @@ -288,21 +287,6 @@ export class ArticleHelper { }]); } } - - if (!surpressNotification) { - if (this.notifiedFiles.indexOf(fileName) === -1) { - Notifications.error(`There seems to be an issue parsing the content its front matter. FileName: ${basename(fileName)}. ERROR: ${error.message || error}`, ...items).then((result: any) => { - if (result?.title) { - const item = items.find(i => i.title === result.title); - if (item) { - item.action(); - } - } - }); - - this.notifiedFiles.push(fileName); - } - } } return null; } diff --git a/src/helpers/CustomScript.ts b/src/helpers/CustomScript.ts index 1d06e679..ad0501f0 100644 --- a/src/helpers/CustomScript.ts +++ b/src/helpers/CustomScript.ts @@ -67,7 +67,7 @@ export class CustomScript { if (folder.lastModified.length > 0) { for await (const file of folder.lastModified) { try { - const article = ArticleHelper.getFrontMatterByPath(file.filePath, true); + const article = ArticleHelper.getFrontMatterByPath(file.filePath); if (article) { const crntOutput = await CustomScript.runScript(wsPath, article, file.filePath, script); if (crntOutput) {