diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c2420fa..fc04ada7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [1.3.0] - 2020-08-22 + +- Added SEO description warning when over 140 characters is used + ## [1.2.0] - 2020-07-03 - Added SEO title warning when over 60 characters is used diff --git a/src/commands/StatusListener.ts b/src/commands/StatusListener.ts index 243f09e2..f75e61b8 100644 --- a/src/commands/StatusListener.ts +++ b/src/commands/StatusListener.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { ArticleHelper } from '../helpers'; +import { ArticleHelper, SeoHelper } from '../helpers'; export class StatusListener { @@ -30,36 +30,18 @@ export class StatusListener { } } - // Check SEO of the title - if (article && article.data && article.data.title) { - const title: string = article.data.title; - console.log(`Title length: ${title.length}`); - if (title.length >= 60) { - const text = editor.document.getText(); - - const markdown = ArticleHelper.stringifyFrontMatter("", article.data); - - const txtIdx = text.indexOf(title); - if (txtIdx !== -1 && txtIdx < markdown.length) { - collection.clear(); - const posStart = editor.document.positionAt(txtIdx); - const posEnd = editor.document.positionAt(txtIdx + 1 + title.length); - - collection.set(editor.document.uri, [{ - code: '', - message: `Article title is longer than 60 characters (current length: ${title.length}). For SEO reasons, it would be better to make it less than 60 characters.`, - range: new vscode.Range(posStart, posEnd), - severity: vscode.DiagnosticSeverity.Warning, - source: 'Front Matter' - }]); - } else { - collection.clear(); - } - } else { - collection.clear(); + // Check SEO for title and description length + if (article && article.data) { + collection.clear(); + + if (article.data.title) { + SeoHelper.checkLength(editor, collection, article, "title", 60); + } + + if (article.data.description) { + SeoHelper.checkLength(editor, collection, article, "description", 140); } } - return; } catch (e) { // Nothing to do diff --git a/src/helpers/SeoHelper.ts b/src/helpers/SeoHelper.ts new file mode 100644 index 00000000..cf603a0e --- /dev/null +++ b/src/helpers/SeoHelper.ts @@ -0,0 +1,36 @@ +import * as vscode from 'vscode'; +import { ArticleHelper } from '.'; +import matter = require('gray-matter'); + +export class SeoHelper { + + public static checkLength(editor: vscode.TextEditor, collection: vscode.DiagnosticCollection, article: matter.GrayMatterFile, fieldName: string, length: number) { + const value = article.data[fieldName]; + if (value.length >= length) { + const text = editor.document.getText(); + + const markdown = ArticleHelper.stringifyFrontMatter("", article.data); + + const txtIdx = text.indexOf(value); + if (txtIdx !== -1 && txtIdx < markdown.length) { + const posStart = editor.document.positionAt(txtIdx); + const posEnd = editor.document.positionAt(txtIdx + 1 + value.length); + + const diagnostic: vscode.Diagnostic = { + code: '', + message: `Article ${fieldName} is longer than ${length} characters (current length: ${value.length}). For SEO reasons, it would be better to make it less than 60 characters.`, + range: new vscode.Range(posStart, posEnd), + severity: vscode.DiagnosticSeverity.Warning, + source: 'Front Matter' + }; + + if (collection.has(editor.document.uri)) { + const otherDiag = collection.get(editor.document.uri) || []; + collection.set(editor.document.uri, [...otherDiag, diagnostic]); + } else { + collection.set(editor.document.uri, [diagnostic]); + } + } + } + } +} \ No newline at end of file diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 609e4897..88ec1f5d 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -1,5 +1,6 @@ export * from './ArticleHelper'; export * from './FilesHelper'; +export * from './SeoHelper'; export * from './SettingsHelper'; export * from './StringHelpers'; export * from './TomlEngine';