mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-07 17:32:58 +02:00
#243 - Refactoring front matter parsing
This commit is contained in:
@@ -6,6 +6,10 @@
|
|||||||
|
|
||||||
- Updated the activity bar icon for better visibility
|
- Updated the activity bar icon for better visibility
|
||||||
|
|
||||||
|
### ⚡️ Optimizations
|
||||||
|
|
||||||
|
- [#243](https://github.com/estruyf/vscode-front-matter/issues/243): Refactoring front matter parsing
|
||||||
|
|
||||||
### 🐞 Fixes
|
### 🐞 Fixes
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1468,4 +1468,4 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"node-fetch": "^2.6.7"
|
"node-fetch": "^2.6.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import * as vscode from 'vscode';
|
|||||||
import { Field, TaxonomyType } from "../models";
|
import { Field, TaxonomyType } from "../models";
|
||||||
import { format } from "date-fns";
|
import { format } from "date-fns";
|
||||||
import { ArticleHelper, Settings, SlugHelper } from '../helpers';
|
import { ArticleHelper, Settings, SlugHelper } from '../helpers';
|
||||||
import matter = require('gray-matter');
|
|
||||||
import { Notifications } from '../helpers/Notifications';
|
import { Notifications } from '../helpers/Notifications';
|
||||||
import { extname, basename, parse, dirname } from 'path';
|
import { extname, basename, parse, dirname } from 'path';
|
||||||
import { COMMAND_NAME, DefaultFields } from '../constants';
|
import { COMMAND_NAME, DefaultFields } from '../constants';
|
||||||
@@ -13,6 +12,7 @@ import { ExplorerView } from '../explorerView/ExplorerView';
|
|||||||
import { DateHelper } from '../helpers/DateHelper';
|
import { DateHelper } from '../helpers/DateHelper';
|
||||||
import { parseWinPath } from '../helpers/parseWinPath';
|
import { parseWinPath } from '../helpers/parseWinPath';
|
||||||
import { Telemetry } from '../helpers/Telemetry';
|
import { Telemetry } from '../helpers/Telemetry';
|
||||||
|
import { ParsedFrontMatter } from '../parsers';
|
||||||
|
|
||||||
|
|
||||||
export class Article {
|
export class Article {
|
||||||
@@ -103,7 +103,7 @@ export class Article {
|
|||||||
* Update the date in the front matter
|
* Update the date in the front matter
|
||||||
* @param article
|
* @param article
|
||||||
*/
|
*/
|
||||||
public static updateDate(article: matter.GrayMatterFile<string>, forceCreate: boolean = false) {
|
public static updateDate(article: ParsedFrontMatter, forceCreate: boolean = false) {
|
||||||
article.data = ArticleHelper.updateDates(article.data);
|
article.data = ArticleHelper.updateDates(article.data);
|
||||||
return article;
|
return article;
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ export class Article {
|
|||||||
|
|
||||||
ArticleHelper.update(
|
ArticleHelper.update(
|
||||||
editor,
|
editor,
|
||||||
updatedArticle as matter.GrayMatterFile<string>
|
updatedArticle as ParsedFrontMatter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ export class Article {
|
|||||||
|
|
||||||
private static setLastModifiedDateInner(
|
private static setLastModifiedDateInner(
|
||||||
document: vscode.TextDocument
|
document: vscode.TextDocument
|
||||||
): matter.GrayMatterFile<string> | undefined {
|
): ParsedFrontMatter | undefined {
|
||||||
const article = ArticleHelper.getFrontMatterFromDocument(document);
|
const article = ArticleHelper.getFrontMatterFromDocument(document);
|
||||||
|
|
||||||
if (!article) {
|
if (!article) {
|
||||||
@@ -343,7 +343,7 @@ export class Article {
|
|||||||
/**
|
/**
|
||||||
* Get the current article
|
* Get the current article
|
||||||
*/
|
*/
|
||||||
private static getCurrent(): matter.GrayMatterFile<string> | undefined {
|
private static getCurrent(): ParsedFrontMatter | undefined {
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
return;
|
return;
|
||||||
@@ -364,7 +364,7 @@ export class Article {
|
|||||||
* @param field
|
* @param field
|
||||||
* @param forceCreate
|
* @param forceCreate
|
||||||
*/
|
*/
|
||||||
private static articleDate(article: matter.GrayMatterFile<string>, field: string, forceCreate: boolean) {
|
private static articleDate(article: ParsedFrontMatter, field: string, forceCreate: boolean) {
|
||||||
if (typeof article.data[field] !== "undefined" || forceCreate) {
|
if (typeof article.data[field] !== "undefined" || forceCreate) {
|
||||||
article.data[field] = Article.formatDate(new Date());
|
article.data[field] = Article.formatDate(new Date());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as matter from 'gray-matter';
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { TaxonomyType } from "../models";
|
import { TaxonomyType } from "../models";
|
||||||
import { SETTING_TAXONOMY_TAGS, SETTING_TAXONOMY_CATEGORIES, EXTENSION_NAME } from '../constants';
|
import { SETTING_TAXONOMY_TAGS, SETTING_TAXONOMY_CATEGORIES, EXTENSION_NAME } from '../constants';
|
||||||
import { ArticleHelper, Settings as SettingsHelper, FilesHelper } from '../helpers';
|
import { ArticleHelper, Settings as SettingsHelper, FilesHelper } from '../helpers';
|
||||||
import { TomlEngine, getFmLanguage, getFormatOpts } from '../helpers/TomlEngine';
|
import { FrontMatterParser } from '../parsers';
|
||||||
import { DumpOptions } from 'js-yaml';
|
import { DumpOptions } from 'js-yaml';
|
||||||
import { Notifications } from '../helpers/Notifications';
|
import { Notifications } from '../helpers/Notifications';
|
||||||
|
|
||||||
@@ -90,10 +89,6 @@ export class Settings {
|
|||||||
const progressNr = allMdFiles.length/100;
|
const progressNr = allMdFiles.length/100;
|
||||||
progress.report({ increment: 0});
|
progress.report({ increment: 0});
|
||||||
|
|
||||||
// Get language options
|
|
||||||
const language = getFmLanguage();
|
|
||||||
const langOpts = getFormatOpts(language);
|
|
||||||
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for (const file of allMdFiles) {
|
for (const file of allMdFiles) {
|
||||||
progress.report({ increment: (++i/progressNr) });
|
progress.report({ increment: (++i/progressNr) });
|
||||||
@@ -102,10 +97,7 @@ export class Settings {
|
|||||||
const txtData = mdFile.getText();
|
const txtData = mdFile.getText();
|
||||||
if (txtData) {
|
if (txtData) {
|
||||||
try {
|
try {
|
||||||
const article = matter(txtData, {
|
const article = FrontMatterParser.fromFile(txtData);
|
||||||
...TomlEngine,
|
|
||||||
...langOpts
|
|
||||||
});
|
|
||||||
if (article && article.data) {
|
if (article && article.data) {
|
||||||
const { data } = article;
|
const { data } = article;
|
||||||
const mdTags = data["tags"];
|
const mdTags = data["tags"];
|
||||||
@@ -218,13 +210,8 @@ export class Settings {
|
|||||||
progress.report({ increment: (++i/progressNr) });
|
progress.report({ increment: (++i/progressNr) });
|
||||||
const mdFile = fs.readFileSync(file.path, { encoding: "utf8" });
|
const mdFile = fs.readFileSync(file.path, { encoding: "utf8" });
|
||||||
if (mdFile) {
|
if (mdFile) {
|
||||||
const language = getFmLanguage();
|
|
||||||
const langOpts = getFormatOpts(language);
|
|
||||||
try {
|
try {
|
||||||
const article = matter(mdFile, {
|
const article = FrontMatterParser.fromFile(mdFile);
|
||||||
...TomlEngine,
|
|
||||||
...langOpts
|
|
||||||
});
|
|
||||||
if (article && article.data) {
|
if (article && article.data) {
|
||||||
const { data } = article;
|
const { data } = article;
|
||||||
let taxonomies: string[] = data[matterProp];
|
let taxonomies: string[] = data[matterProp];
|
||||||
@@ -239,9 +226,7 @@ export class Settings {
|
|||||||
data[matterProp] = [...new Set(taxonomies)].sort();
|
data[matterProp] = [...new Set(taxonomies)].sort();
|
||||||
const spaces = vscode.window.activeTextEditor?.options?.tabSize;
|
const spaces = vscode.window.activeTextEditor?.options?.tabSize;
|
||||||
// Update the file
|
// Update the file
|
||||||
fs.writeFileSync(file.path, matter.stringify(article.content, article.data, {
|
fs.writeFileSync(file.path, FrontMatterParser.toFile(article.content, article.data, {
|
||||||
...TomlEngine,
|
|
||||||
...langOpts,
|
|
||||||
indent: spaces || 2
|
indent: spaces || 2
|
||||||
} as DumpOptions as any), { encoding: "utf8" });
|
} as DumpOptions as any), { encoding: "utf8" });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { MarkdownFoldingProvider } from './../providers/MarkdownFoldingProvider';
|
import { MarkdownFoldingProvider } from './../providers/MarkdownFoldingProvider';
|
||||||
import { DEFAULT_CONTENT_TYPE, DEFAULT_CONTENT_TYPE_NAME } from './../constants/ContentType';
|
import { DEFAULT_CONTENT_TYPE, DEFAULT_CONTENT_TYPE_NAME } from './../constants/ContentType';
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as matter from "gray-matter";
|
|
||||||
import * as fs from "fs";
|
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 { 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 { DumpOptions } from 'js-yaml';
|
||||||
import { TomlEngine, getFmLanguage, getFormatOpts } from './TomlEngine';
|
import { FrontMatterParser, ParsedFrontMatter } from '../parsers';
|
||||||
import { Extension, Logger, Settings, SlugHelper } from '.';
|
import { Extension, Logger, Settings, SlugHelper } from '.';
|
||||||
import { format, parse } from 'date-fns';
|
import { format, parse } from 'date-fns';
|
||||||
import { Notifications } from './Notifications';
|
import { Notifications } from './Notifications';
|
||||||
@@ -56,7 +55,7 @@ export class ArticleHelper {
|
|||||||
* @param editor
|
* @param editor
|
||||||
* @param article
|
* @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);
|
const update = this.generateUpdate(editor.document, article);
|
||||||
|
|
||||||
await editor.edit(builder => builder.replace(update.range, update.newText));
|
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.
|
* Generate the update to be applied to the article.
|
||||||
* @param 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 nrOfLines = document.lineCount as number;
|
||||||
const range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(nrOfLines, 0));
|
const range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(nrOfLines, 0));
|
||||||
const removeQuotes = Settings.get(SETTING_REMOVE_QUOTES) as string[];
|
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 indentArray = Settings.get(SETTING_INDENT_ARRAY) as boolean;
|
||||||
const commaSeparated = Settings.get<string[]>(SETTING_COMMA_SEPARATED_FIELDS);
|
const commaSeparated = Settings.get<string[]>(SETTING_COMMA_SEPARATED_FIELDS);
|
||||||
|
|
||||||
const language = getFmLanguage();
|
|
||||||
const langOpts = getFormatOpts(language);
|
|
||||||
|
|
||||||
const spaces = vscode.window.activeTextEditor?.options?.tabSize;
|
const spaces = vscode.window.activeTextEditor?.options?.tabSize;
|
||||||
|
|
||||||
if (commaSeparated) {
|
if (commaSeparated) {
|
||||||
@@ -130,9 +126,7 @@ export class ArticleHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return matter.stringify(content, data, ({
|
return FrontMatterParser.toFile(content, data, ({
|
||||||
...TomlEngine,
|
|
||||||
...langOpts,
|
|
||||||
noArrayIndent: !indentArray,
|
noArrayIndent: !indentArray,
|
||||||
skipInvalid: true,
|
skipInvalid: true,
|
||||||
noCompatMode: true,
|
noCompatMode: true,
|
||||||
@@ -169,7 +163,7 @@ export class ArticleHelper {
|
|||||||
/**
|
/**
|
||||||
* Get date from front matter
|
* Get date from front matter
|
||||||
*/
|
*/
|
||||||
public static getDate(article: matter.GrayMatterFile<string> | null) {
|
public static getDate(article: ParsedFrontMatter | null) {
|
||||||
if (!article) {
|
if (!article) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -358,17 +352,12 @@ export class ArticleHelper {
|
|||||||
* @param fileContents
|
* @param fileContents
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
private static parseFile(fileContents: string, fileName: string): matter.GrayMatterFile<string> | null {
|
private static parseFile(fileContents: string, fileName: string): ParsedFrontMatter | null {
|
||||||
try {
|
try {
|
||||||
const commaSeparated = Settings.get<string[]>(SETTING_COMMA_SEPARATED_FIELDS);
|
const commaSeparated = Settings.get<string[]>(SETTING_COMMA_SEPARATED_FIELDS);
|
||||||
|
|
||||||
if (fileContents) {
|
if (fileContents) {
|
||||||
const language: string = getFmLanguage();
|
let article = FrontMatterParser.fromFile(fileContents);
|
||||||
const langOpts = getFormatOpts(language);
|
|
||||||
let article: matter.GrayMatterFile<string> | null = matter(fileContents, {
|
|
||||||
...TomlEngine,
|
|
||||||
...langOpts
|
|
||||||
});
|
|
||||||
|
|
||||||
if (article?.data) {
|
if (article?.data) {
|
||||||
if (commaSeparated) {
|
if (commaSeparated) {
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import { window, env as vscodeEnv, ProgressLocation } from 'vscode';
|
|||||||
import { ArticleHelper } from '.';
|
import { ArticleHelper } from '.';
|
||||||
import { Folders } from '../commands/Folders';
|
import { Folders } from '../commands/Folders';
|
||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
import matter = require('gray-matter');
|
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { Notifications } from './Notifications';
|
import { Notifications } from './Notifications';
|
||||||
import ContentProvider from '../providers/ContentProvider';
|
import ContentProvider from '../providers/ContentProvider';
|
||||||
import { Dashboard } from '../commands/Dashboard';
|
import { Dashboard } from '../commands/Dashboard';
|
||||||
import { DashboardCommand } from '../dashboardWebView/DashboardCommand';
|
import { DashboardCommand } from '../dashboardWebView/DashboardCommand';
|
||||||
|
import { ParsedFrontMatter } from '../parsers';
|
||||||
|
|
||||||
export class CustomScript {
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
let articleData = "";
|
let articleData = "";
|
||||||
if (os.type() === "Windows_NT") {
|
if (os.type() === "Windows_NT") {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { ArticleHelper } from '.';
|
import { ArticleHelper } from '.';
|
||||||
import matter = require('gray-matter');
|
import { ParsedFrontMatter } from '../parsers';
|
||||||
|
|
||||||
export class SeoHelper {
|
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];
|
const value = article.data[fieldName];
|
||||||
if (value.length > length) {
|
if (value.length > length) {
|
||||||
const text = editor.document.getText();
|
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 './GroupBy';
|
||||||
export * from './ImageHelper';
|
export * from './ImageHelper';
|
||||||
export * from './Logger';
|
export * from './Logger';
|
||||||
|
export * from './MediaHelpers';
|
||||||
export * from './MediaLibrary';
|
export * from './MediaLibrary';
|
||||||
export * from './MessageHelper';
|
export * from './MessageHelper';
|
||||||
export * from './Notifications';
|
export * from './Notifications';
|
||||||
@@ -19,7 +20,7 @@ export * from './SettingsHelper';
|
|||||||
export * from './SlugHelper';
|
export * from './SlugHelper';
|
||||||
export * from './Sorting';
|
export * from './Sorting';
|
||||||
export * from './StringHelpers';
|
export * from './StringHelpers';
|
||||||
export * from './TomlEngine';
|
export * from './Telemetry';
|
||||||
export * from './decodeBase64Image';
|
export * from './decodeBase64Image';
|
||||||
export * from './getNonce';
|
export * from './getNonce';
|
||||||
export * from './isValidFile';
|
export * from './isValidFile';
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { Engines, getFormatOpts } from ".";
|
||||||
|
import * as matter from "gray-matter";
|
||||||
|
import { Settings } from "../helpers";
|
||||||
|
import { SETTING_FRONTMATTER_TYPE } from "../constants";
|
||||||
|
|
||||||
|
export interface Format {
|
||||||
|
language: string;
|
||||||
|
delimiters: string | [string, string] | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ParsedFrontMatter {
|
||||||
|
data: { [key: string]: any };
|
||||||
|
content: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export class FrontMatterParser {
|
||||||
|
|
||||||
|
public static fromFile(content: string): ParsedFrontMatter {
|
||||||
|
const format = getFormatOpts(this.getLanguage());
|
||||||
|
const result = matter(content, { ...Engines, ...format });
|
||||||
|
// in the absent of a body when serializing an entry we use an empty one
|
||||||
|
// when calling `toFile`, so we don't want to add it when parsing.
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: { ...result.data },
|
||||||
|
content: (result.content.trim() && result.content)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static toFile(
|
||||||
|
content: string,
|
||||||
|
metadata: Object,
|
||||||
|
options?: any
|
||||||
|
) {
|
||||||
|
// Stringify to YAML if the format was not set
|
||||||
|
const format = getFormatOpts(this.getLanguage());
|
||||||
|
|
||||||
|
const trimLastLineBreak = content.slice(-1) !== '\n';
|
||||||
|
const file = matter.stringify(content, metadata, {
|
||||||
|
...Engines,
|
||||||
|
...options,
|
||||||
|
...format
|
||||||
|
});
|
||||||
|
return trimLastLineBreak && file.slice(-1) === '\n' ? file.substring(0, file.length - 1) : file;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getLanguage() {
|
||||||
|
const language = Settings.get(SETTING_FRONTMATTER_TYPE) as string || "YAML";
|
||||||
|
return language.toLowerCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import * as toml from '@iarna/toml';
|
||||||
|
import { Format } from '.';
|
||||||
|
|
||||||
|
export const getFormatOpts = (format: string): Format => {
|
||||||
|
const formats: { [prop: string]: Format} = {
|
||||||
|
yaml: { language: 'yaml', delimiters: '---' },
|
||||||
|
toml: { language: 'toml', delimiters: '+++' },
|
||||||
|
json: { language: 'json', delimiters: '---' },
|
||||||
|
};
|
||||||
|
|
||||||
|
return formats[format];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Engines = {
|
||||||
|
engines: {
|
||||||
|
toml: {
|
||||||
|
parse: (value: string) => {
|
||||||
|
return toml.parse(value);
|
||||||
|
},
|
||||||
|
stringify: (value: any) => {
|
||||||
|
return toml.stringify(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './FrontMatterParser';
|
||||||
|
export * from './ParserEngines';
|
||||||
Reference in New Issue
Block a user