mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-06-26 21:11:38 +02:00
#243 - Refactoring front matter parsing
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
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";
|
||||
import * as fs from "fs";
|
||||
import { DefaultFields, SETTINGS_CONTENT_DEFAULT_FILETYPE, SETTINGS_CONTENT_PLACEHOLDERS, SETTINGS_CONTENT_SUPPORTED_FILETYPES, 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 { FrontMatterParser, ParsedFrontMatter } from '../parsers';
|
||||
import { Extension, Logger, Settings, SlugHelper } from '.';
|
||||
import { format, parse } from 'date-fns';
|
||||
import { Notifications } from './Notifications';
|
||||
@@ -56,7 +55,7 @@ export class ArticleHelper {
|
||||
* @param editor
|
||||
* @param article
|
||||
*/
|
||||
public static async update(editor: vscode.TextEditor, article: matter.GrayMatterFile<string>) {
|
||||
public static async update(editor: vscode.TextEditor, article: ParsedFrontMatter) {
|
||||
const update = this.generateUpdate(editor.document, article);
|
||||
|
||||
await editor.edit(builder => builder.replace(update.range, update.newText));
|
||||
@@ -66,7 +65,7 @@ export class ArticleHelper {
|
||||
* Generate the update to be applied to the article.
|
||||
* @param article
|
||||
*/
|
||||
public static generateUpdate(document: vscode.TextDocument, article: matter.GrayMatterFile<string>): vscode.TextEdit {
|
||||
public static generateUpdate(document: vscode.TextDocument, article: ParsedFrontMatter): vscode.TextEdit {
|
||||
const nrOfLines = document.lineCount as number;
|
||||
const range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(nrOfLines, 0));
|
||||
const removeQuotes = Settings.get(SETTING_REMOVE_QUOTES) as string[];
|
||||
@@ -117,9 +116,6 @@ export class ArticleHelper {
|
||||
const indentArray = Settings.get(SETTING_INDENT_ARRAY) as boolean;
|
||||
const commaSeparated = Settings.get<string[]>(SETTING_COMMA_SEPARATED_FIELDS);
|
||||
|
||||
const language = getFmLanguage();
|
||||
const langOpts = getFormatOpts(language);
|
||||
|
||||
const spaces = vscode.window.activeTextEditor?.options?.tabSize;
|
||||
|
||||
if (commaSeparated) {
|
||||
@@ -130,9 +126,7 @@ export class ArticleHelper {
|
||||
}
|
||||
}
|
||||
|
||||
return matter.stringify(content, data, ({
|
||||
...TomlEngine,
|
||||
...langOpts,
|
||||
return FrontMatterParser.toFile(content, data, ({
|
||||
noArrayIndent: !indentArray,
|
||||
skipInvalid: true,
|
||||
noCompatMode: true,
|
||||
@@ -169,7 +163,7 @@ export class ArticleHelper {
|
||||
/**
|
||||
* Get date from front matter
|
||||
*/
|
||||
public static getDate(article: matter.GrayMatterFile<string> | null) {
|
||||
public static getDate(article: ParsedFrontMatter | null) {
|
||||
if (!article) {
|
||||
return;
|
||||
}
|
||||
@@ -358,17 +352,12 @@ export class ArticleHelper {
|
||||
* @param fileContents
|
||||
* @returns
|
||||
*/
|
||||
private static parseFile(fileContents: string, fileName: string): matter.GrayMatterFile<string> | null {
|
||||
private static parseFile(fileContents: string, fileName: string): ParsedFrontMatter | null {
|
||||
try {
|
||||
const commaSeparated = Settings.get<string[]>(SETTING_COMMA_SEPARATED_FIELDS);
|
||||
|
||||
if (fileContents) {
|
||||
const language: string = getFmLanguage();
|
||||
const langOpts = getFormatOpts(language);
|
||||
let article: matter.GrayMatterFile<string> | null = matter(fileContents, {
|
||||
...TomlEngine,
|
||||
...langOpts
|
||||
});
|
||||
let article = FrontMatterParser.fromFile(fileContents);
|
||||
|
||||
if (article?.data) {
|
||||
if (commaSeparated) {
|
||||
|
||||
@@ -3,13 +3,13 @@ import { window, env as vscodeEnv, ProgressLocation } from 'vscode';
|
||||
import { ArticleHelper } from '.';
|
||||
import { Folders } from '../commands/Folders';
|
||||
import { exec } from 'child_process';
|
||||
import matter = require('gray-matter');
|
||||
import * as os from 'os';
|
||||
import { join } from 'path';
|
||||
import { Notifications } from './Notifications';
|
||||
import ContentProvider from '../providers/ContentProvider';
|
||||
import { Dashboard } from '../commands/Dashboard';
|
||||
import { DashboardCommand } from '../dashboardWebView/DashboardCommand';
|
||||
import { ParsedFrontMatter } from '../parsers';
|
||||
|
||||
export class CustomScript {
|
||||
|
||||
@@ -117,7 +117,7 @@ export class CustomScript {
|
||||
});
|
||||
}
|
||||
|
||||
private static async runScript(wsPath: string, article: matter.GrayMatterFile<string> | null, contentPath: string, script: ICustomScript): Promise<string | null> {
|
||||
private static async runScript(wsPath: string, article: ParsedFrontMatter | null, contentPath: string, script: ICustomScript): Promise<string | null> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let articleData = "";
|
||||
if (os.type() === "Windows_NT") {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { ArticleHelper } from '.';
|
||||
import matter = require('gray-matter');
|
||||
import { ParsedFrontMatter } from '../parsers';
|
||||
|
||||
export class SeoHelper {
|
||||
|
||||
public static checkLength(editor: vscode.TextEditor, collection: vscode.DiagnosticCollection, article: matter.GrayMatterFile<string>, fieldName: string, length: number) {
|
||||
public static checkLength(editor: vscode.TextEditor, collection: vscode.DiagnosticCollection, article: ParsedFrontMatter, fieldName: string, length: number) {
|
||||
const value = article.data[fieldName];
|
||||
if (value.length > length) {
|
||||
const text = editor.document.getText();
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import * as toml from '@iarna/toml';
|
||||
import { SETTING_FRONTMATTER_TYPE } from '../constants';
|
||||
import { Settings } from './SettingsHelper';
|
||||
|
||||
export const getFmLanguage = (): string => {
|
||||
const language = Settings.get(SETTING_FRONTMATTER_TYPE) as string || "YAML";
|
||||
return language.toLowerCase();
|
||||
};
|
||||
|
||||
export const getFormatOpts = (format: string): { language: string, delimiters: string | [string, string] | undefined } => {
|
||||
const formats: { [prop: string]: { language: string, delimiters: string | [string, string] | undefined }} = {
|
||||
yaml: { language: 'yaml', delimiters: '---' },
|
||||
toml: { language: 'toml', delimiters: '+++' },
|
||||
json: { language: 'json', delimiters: '---' },
|
||||
};
|
||||
|
||||
return formats[format];
|
||||
};
|
||||
|
||||
export const TomlEngine = {
|
||||
engines: {
|
||||
toml: {
|
||||
parse: (value: string) => {
|
||||
return toml.parse(value);
|
||||
},
|
||||
stringify: (value: any) => {
|
||||
return toml.stringify(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -9,6 +9,7 @@ export * from './FrameworkDetector';
|
||||
export * from './GroupBy';
|
||||
export * from './ImageHelper';
|
||||
export * from './Logger';
|
||||
export * from './MediaHelpers';
|
||||
export * from './MediaLibrary';
|
||||
export * from './MessageHelper';
|
||||
export * from './Notifications';
|
||||
@@ -19,7 +20,7 @@ export * from './SettingsHelper';
|
||||
export * from './SlugHelper';
|
||||
export * from './Sorting';
|
||||
export * from './StringHelpers';
|
||||
export * from './TomlEngine';
|
||||
export * from './Telemetry';
|
||||
export * from './decodeBase64Image';
|
||||
export * from './getNonce';
|
||||
export * from './isValidFile';
|
||||
|
||||
Reference in New Issue
Block a user