Skip to content

Commit

Permalink
code refactor, new advance analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil-aexol committed Aug 23, 2023
1 parent 471f997 commit 9048be0
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 232 deletions.
69 changes: 29 additions & 40 deletions packages/landing-friend-core/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {
message,
forbiddenCharacters as _forbiddenCharacters,
TagsPatterns,
AdvancedTagsPatterns,
checkFiles,
prepareHTMLWithTables,
AllTagsName,
} from "./index.js";

export const websiteAnalyzer = (config: ConfigFile) => {
Expand All @@ -28,23 +30,7 @@ export const websiteAnalyzer = (config: ConfigFile) => {
const allFiles = getFilesToAnalyze(input);

let tagsPatterns: TagsPatterns = {};
let countKeywords: boolean = true;
let countWordsInLast: boolean = true;
if (
Object.entries(analyzer).some(
([tag, value]) => tag === "keywords" && value.countKeywords !== true
)
) {
countKeywords = false;
}
if (
Object.entries(analyzer).some(
([tag, value]) =>
tag === "lastSentence" && value.countWordsInLast !== true
)
) {
countWordsInLast = false;
}
let advancedTagsPatterns: AdvancedTagsPatterns = {};

allFiles.forEach((file) => {
if (!matchedSetting(file, excludedPage, input)) {
Expand All @@ -53,38 +39,41 @@ export const websiteAnalyzer = (config: ConfigFile) => {
tags,
advancedTags,
tagsPatterns,
countKeywords,
countWordsInLast,
advancedTagsPatterns,
countKeywords: tags.keywords.count,
countWordsInLast: tags.lastSentence.count,
});
}
});

const htmlWithTablesAndCharts = prepareHTMLWithTables({
tagsPatterns,
});

if (analyzer) {
const cleanedTagsPatterns: TagsPatterns = {};
let cleanedTagsPatterns: TagsPatterns = {};
Object.entries(tagsPatterns).forEach(([file, tagData]) => {
cleanedTagsPatterns[file] = {};
Object.entries(tagData).forEach(([tag, value]) => {
!(tag === "keywords" && !value.countKeywords) &&
!(tag === "lastSentence" && !value.countWordsInLast)
? (cleanedTagsPatterns[file][tag] = {
requirement:
value.requirement &&
value.requirement.replace(/<\/?strong>/gs, ""),
count: value.count,
content: value.content,
forbiddenCharacters: value.forbiddenCharacters,
keywordsIncluded:
tag !== "keywords" ? value.keywordsIncluded : undefined,
countKeywords:
tag === "keywords" ? value.countKeywords : undefined,
countWordsInLast:
tag === "lastSentence" ? value.countWordsInLast : undefined,
})
: undefined;
cleanedTagsPatterns[file] = { ...cleanedTagsPatterns[file] };
Object.entries(tagData).forEach(([_tag, value]) => {
const tag = _tag as AllTagsName;
if (
!(tag === "keywords" && !value.countKeywords) &&
!(tag === "lastSentence" && !value.countWordsInLast)
) {
cleanedTagsPatterns[file][tag] = {
requirement:
value.requirement &&
value.requirement.replace(/<\/?strong>/gs, ""),
quantity: value.quantity,
content: value.content,
forbiddenCharacters: value.forbiddenCharacters,
keywordsIncluded:
tag !== "keywords" ? value.keywordsIncluded : undefined,
countKeywords:
tag === "keywords" ? value.countKeywords : undefined,
countWordsInLast:
tag === "lastSentence" ? value.countWordsInLast : undefined,
};
}
});
});
try {
Expand Down
68 changes: 8 additions & 60 deletions packages/landing-friend-core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,8 @@
import fs from "fs";
import { message } from "./console.js";
import { LanguageCode } from "iso-639-1";
import ts from "typescript";
import { ConfigFile } from "./index.js";

type SitemapSettings = {
locale: {
defaultLocale: LanguageCode;
localeWildcard: string;
};
trailingSlash: boolean;
sortBy: "priority" | "alphabetically-asc" | "alphabetically-desc";
};

export type AdvancedTagsProps = {
og?: boolean;
};

export type TagsProps =
| {
h1: {
minLength: number;
maxLength: number;
};
}
| {
title: {
minLength: number;
maxLength: number;
};
}
| {
description: {
minLength: number;
maxLength: number;
};
}
| {
lastSentence: {
countWordsInLast: boolean;
};
}
| {
keywords: {
countKeywords: boolean;
};
};

export type ConfigFile = {
domain: string;
input: string;
output: string;
robots: boolean;
excludedPage: string[];
sitemap?: SitemapSettings;
analyzer?: TagsProps;
advancedAnalyzer?: AdvancedTagsProps;
};
export const GLOBAL_CONFIG_FILE: ConfigFile = {
domain: "https://www.example.com",
input: "./out",
Expand All @@ -81,18 +28,18 @@ export const EXTENDED_ANALYZER_GLOBAL_CONFIG_FILE: Pick<
analyzer: {
h1: {
minLength: 10,
maxLength: 70,
maxLength: 100,
},
title: {
minLength: 10,
maxLength: 70,
maxLength: 60,
},
description: {
maxLength: 200,
minLength: 50,
maxLength: 160,
minLength: 120,
},
lastSentence: { countWordsInLast: true },
keywords: { countKeywords: true },
lastSentence: { count: true },
keywords: { count: true },
},
};
export const EXTENDED_ADVANCED_ANALYZER_GLOBAL_CONFIG_FILE: Pick<
Expand All @@ -101,6 +48,7 @@ export const EXTENDED_ADVANCED_ANALYZER_GLOBAL_CONFIG_FILE: Pick<
> = {
advancedAnalyzer: {
og: true,
twitter: true,
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
AdvancedTagsProps,
staticTags,
unicode,
forbiddenCharacters as _forbiddenCharacters,
AdvancedTagsName,
AdvancedTagsPatterns,
} from "../../index.js";

interface MatchedArrayProps {
content: string;
}

const matchedTags = (advancedTags: AdvancedTagsName, fileContent: string) => {
let regex: RegExp | undefined;
const matchedArray: { [tagName: string]: MatchedArrayProps }[] = [];

if (advancedTags === "og") {
regex = new RegExp(`<meta property="og:(.*?)" content="(.*?)"`, "gs");
} else if (advancedTags === "twitter") {
regex = new RegExp(`<meta name="twitter:(.*?)" content="(.*?)"`, "gs");
}

if (regex) {
const matches = fileContent.match(regex);
if (matches) {
matches.forEach((match) => {
const captureGroups = regex!.exec(match);
if (captureGroups) {
const tagName = captureGroups[1];
const content = captureGroups[2];
const tagObject = { [tagName]: { content } };
matchedArray.push(tagObject);
}
});
}
}

return matchedArray;
};

export const checkFileToAdvanceAnalyzer = ({
file,
fileContent,
advancedTags,
advancedTagsPatterns,
}: {
file: string;
fileContent: string;
advancedTags?: AdvancedTagsProps;
advancedTagsPatterns: AdvancedTagsPatterns;
}) => {
if (!advancedTags) {
return;
}
Object.entries(advancedTags).forEach(([_tag, value]) => {
if (!value) return;
const tag = _tag as AdvancedTagsName;
const matches = matchedTags(tag, fileContent);

if (matches) {
matches.forEach((match) => {
Object.entries(match).map(([metaName, value]) => {
let content: string;
staticTags.forEach((staticTag) => {
const _content = value.content;
const staticTagRegex = new RegExp(
`<${staticTag}.*?>|<\/${staticTag}>`,
"g"
);
Object.entries(unicode).forEach(([unicode, replacement]) => {
const unicodeRegex = new RegExp(`${unicode}`, "g");
content = _content.replace(unicodeRegex, replacement);
});

content = _content.replace(staticTagRegex, "");
});

const forbiddenCharacters = _forbiddenCharacters.filter((char) =>
content.includes(char)
);
return (advancedTagsPatterns[file] = {
...advancedTagsPatterns[file],
[tag]: {
tagAmount: matches.length,
metaName,
content: value.content,
forbiddenCharacters,
},
});
});
});
} else {
return (advancedTagsPatterns[file] = {
...advancedTagsPatterns[file],
[tag]: {
tagAmount: NaN,
},
});
}
});
};
Loading

0 comments on commit 9048be0

Please sign in to comment.