#13 - Added SEO description check

This commit is contained in:
Elio Struyf
2020-08-22 18:02:14 +02:00
parent ac2be1e021
commit c4908ab1d8
4 changed files with 52 additions and 29 deletions
+4
View File
@@ -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
+11 -29
View File
@@ -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
+36
View File
@@ -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<string>, 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]);
}
}
}
}
}
+1
View File
@@ -1,5 +1,6 @@
export * from './ArticleHelper';
export * from './FilesHelper';
export * from './SeoHelper';
export * from './SettingsHelper';
export * from './StringHelpers';
export * from './TomlEngine';