Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil-aexol committed Nov 18, 2023
1 parent 85ee150 commit 895b454
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"argsIgnorePattern": "^_"
}
],
"linebreak-style": ["error", "unix"],
"linebreak-style": 0,
"quotes": [2, "double", { "avoidEscape": true, "allowTemplateLiterals": true }],
"semi": ["error", "always"]
}
Expand Down
5 changes: 3 additions & 2 deletions packages/landing-friend-core/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export const websiteAnalyzer = async (config: ConfigFile, interval?: NodeJS.Time
keywordsIncluded:
tag !== AdditionalTagsName.Keywords ? value.keywordsIncluded : undefined,
multipleTags: value.multipleTags,
tagAmount: tag in AdvancedTagsName ? value.tagAmount : undefined,
tagAmount: Object.values(AdvancedTagsName).includes(tag as AdvancedTagsName)
? value.tagAmount
: undefined,
listOfFoundMeta: value.listOfFoundMeta,
isError: value.isError,
missingKeywords: value.missingKeywords,
Expand All @@ -92,7 +94,6 @@ export const websiteAnalyzer = async (config: ConfigFile, interval?: NodeJS.Time
cleanedTagsPatterns[file] = tagArray;
});
});

clearTimeout(interval);
try {
saveFile(pathName(FileName.analyze, ".json"), JSON.stringify(cleanedTagsPatterns, null, 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const checkFileToAdvanceAnalyzer = async ({

const metaObject: MetaNameWithProps = {
[metaName]: {
content,
content: content ? content.trim() : undefined,
forbiddenCharacters,
status: status ? status : undefined,
} as MetaNameTagsProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const arrayFilter = (firstArray: string[], secondArray: string[]) => {

const checkContent = (tagName: BasicTagsName, fileContent: string) => {
let regex: RegExp | undefined;
let matches: string[] | RegExpMatchArray | null = null;
let regexMatch: RegExpMatchArray | null = null;
let matches: string[] | undefined;

if (tagName === "description") {
regex = new RegExp(`<meta name="description" content="(.*?)"`, "g");
Expand All @@ -29,19 +30,20 @@ const checkContent = (tagName: BasicTagsName, fileContent: string) => {
regex = new RegExp(`<${tagName}.*?>(.*?)</${tagName}>`, "g");
}

matches = regex && fileContent.match(regex);
regexMatch = regex && fileContent.match(regex);

if (tagName === AdditionalTagsName.LastSentence && matches !== null) {
const lastMatch = matches[matches.length - 1];
matches = lastMatch !== undefined ? [lastMatch] : null;
if (tagName === AdditionalTagsName.LastSentence && regexMatch !== null) {
const lastMatch = regexMatch[regexMatch.length - 1];
regexMatch = lastMatch !== undefined ? [lastMatch] : null;
}

if (matches) {
const updatedMatches = [...matches];
if (regexMatch) {
const updatedMatches = [...regexMatch];
updatedMatches.forEach((match, index) => {
const captureGroups = regex!.exec(match);
if (captureGroups) {
updatedMatches[index] = clearContent(captureGroups[1])!;
const cleanedMatch = clearContent(captureGroups[1]);
updatedMatches[index] = cleanedMatch ? cleanedMatch : "";
}
});

Expand Down Expand Up @@ -72,7 +74,7 @@ export const checkFileToBasicAnalyzer = ({
let updatedTagsPatterns = { ...tagsPatterns[file] };
let mainKeywordsArray: string[] = [];
let h1Keywords: string[] = [];
const url = domain + file.replace("index.html", "");
const url = (domain + file.replace("index.html", "")).trim();

if (tags.keywords.count) {
const keywordsMatch = checkContent(AdditionalTagsName.Keywords, fileContent);
Expand Down Expand Up @@ -129,7 +131,10 @@ export const checkFileToBasicAnalyzer = ({
: undefined;

if (h1Keywords.length > 0) {
if (tag in TagsName || tag === AdditionalTagsName.LastSentence) {
if (
Object.values(TagsName).includes(tag as TagsName) ||
tag === AdditionalTagsName.LastSentence
) {
missingKeywords = arrayFilter(h1Keywords, tagKeywords ? tagKeywords : []);
toMuchKeywords = arrayFilter(tagKeywords ? tagKeywords : [], h1Keywords);
}
Expand Down Expand Up @@ -173,7 +178,7 @@ export const checkFileToBasicAnalyzer = ({
? "The canonical link must be the same as the URL."
: `Tag length should be between <strong>${value.minLength}</strong> and <strong>${value.maxLength}</strong>`,
quantity: match.length,
content: tag === AdditionalTagsName.Keywords ? tagKeywords : match,
content: tag === AdditionalTagsName.Keywords ? tagKeywords : match.trim(),
multipleTags: undefined,
keywordsIncluded: tagKeywords,
forbiddenCharacters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ const generateTableRows = ({
numberOfErrors++;
}

if (tag in TagsName || (tag in AdditionalTagsName && tag !== "canonical")) {
if (
Object.values(TagsName).includes(tag as TagsName) ||
(Object.values(AdditionalTagsName).includes(tag as AdditionalTagsName) &&
tag !== "canonical")
) {
dataForMissingSection[tag as TagsToMissingSection] = {
missingKeywords: missingKeywords ? missingKeywords : [],
toMuchKeywords: toMuchKeywords ? toMuchKeywords : [],
Expand All @@ -88,7 +92,7 @@ const generateTableRows = ({
const tag = _tag as AllTagsName;
const value = _value as CombineTagsWithReason;

if (!(tag in AdvancedTagsName)) {
if (!Object.values(AdvancedTagsName).includes(tag as AdvancedTagsName)) {
mainSection += `<tr>${generateMainSection({
countKeywords,
countWordsInLast,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdditionalTagsName, BasicTagsName, CombineTagsWithReason } from "@/index.js";
import { AdditionalTagsName, BasicTagsName, CombineTagsWithReason, TagsName } from "@/index.js";

interface Props {
tag: BasicTagsName;
Expand Down Expand Up @@ -26,34 +26,29 @@ export const generateMainSection = ({
} else {
const _pathname = __pathname.replace("index.html", "");
const pathname = trailingSlash ? _pathname : _pathname.replace(new RegExp("\\$", "g"), "");
const url = domain + pathname;
const url = (domain + pathname).trim();

const firstCell = `<td>
${tag === AdditionalTagsName.Keywords ? "Length of " : "List of "}
<strong>${tag === AdditionalTagsName.LastSentence ? "last sentence" : tag}</strong>:
${
value.minLength && value.maxLength
? `<strong style="color:${
value.quantity >= value.minLength && value.quantity <= value.maxLength ? "black" : "red"
}">${value.quantity}</strong>`
: tag in AdditionalTagsName
? value.quantity > 0
? `<strong style="color: ${
value.quantity > 0
? tag in TagsName && value.minLength && value.maxLength
? `<strong style="color:${
value.quantity >= value.minLength && value.quantity <= value.maxLength ? "black" : "red"
}">${value.quantity}</strong>`
: `<strong style="color: ${
tag !== AdditionalTagsName.Canonical ? "black" : value.content === url ? "black" : "red"
}">
${
typeof value.content === "string"
? value.content.includes("https")
? `<a href="${value.content}" style="cursor:pointer">${value.content}</a>${
value.content !== url ? ` | Url not match` : ""
}`
: value.content
: value.content?.join(", ")
}
</strong>
`
: `<strong style="color:red">No words detected</strong>`
: `<strong style="color:red">No characters detected/strong>`
}">${
typeof value.content === "string"
? value.content.includes("https")
? `<a href="${value.content}" style="cursor:pointer">${value.content}</a>${
value.content !== url ? ` | Url not match` : ""
}`
: value.content
: value.content?.join(", ")
}</strong>`
: `<strong style="color:red">No characters detected</strong>`
}
${
value.quantity > 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export const generateMetaTagsSection = ({ value, tag }: Props) => {
? `<td colspan="2" style="color:${
!metaValue.status || metaValue.status === "OK" ? "black" : "red"
}">Content of <strong>${title}</strong>: ${
metaValue.content ? metaValue.content : "No content detected"
metaValue.content
? metaValue.content.includes("https")
? `<a href="${metaValue.content}" style="cursor:pointer">${metaValue.content}</a>`
: metaValue.content
: "No content detected"
}
${metaValue.status ? ` | <strong>Url status: ${metaValue.status}</strong>` : ""}
${
Expand Down
20 changes: 12 additions & 8 deletions packages/landing-friend-core/src/utils/functions/clearContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import { staticTags, unicode } from "@/index.js";
export const clearContent = (content: string | undefined) => {
if (!content) return;
let finalContent = content;
staticTags.forEach(staticTag => {
const staticTagRegex = new RegExp(`<${staticTag}.*?>|</${staticTag}>`, "g");
Object.entries(unicode).forEach(([unicode, replacement]) => {
const unicodeRegex = new RegExp(`${unicode}`, "g");
finalContent = content.replace(unicodeRegex, replacement);
});

finalContent = content.replace(staticTagRegex, "");
});
for (const staticTag of staticTags) {
const staticTagRegex = new RegExp(
`<${staticTag}.*?>|</${staticTag}>|\\.css.*?}|@media.*?}|{|}`,
"g"
);
finalContent = finalContent.replace(staticTagRegex, "");
}

for (const [entity, replacement] of Object.entries(unicode)) {
const entityRegex = new RegExp(entity, "g");
finalContent = finalContent.replace(entityRegex, replacement);
}

return finalContent;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
export const readFile = (filePath: string) => {
return fs.readFileSync(filePath, "utf8");
};

export const getHtmlFiles = (base: string, deleteFileExtension: boolean) => {
const baseWithoutDot = base.replace(/\.\//g, "");

Expand Down
90 changes: 45 additions & 45 deletions packages/landing-friend/src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,6 @@ yargs(hideBin(process.argv))
//!!!!!!!
// USE IT INSTEAD OF BELOW COMMAND IF U WANT CONSOLE.LOG SOMETHING
//
// .command(
// "analyze",
// "Analyze your landing page",
// {
// help: {
// describe:
// "Analysis of your website through defined values in the config generates HTML and JSON files.",
// },
// },
// async () => {
// console.clear();
// const config = readConfig("landing-friend-config.ts");
// if (!config) {
// message("Config not found", "red");
// return;
// }
// try {
// message("Analyzing your page...", "yellow");
// await websiteAnalyzer(config);
// } catch (e) {
// const error = e as Error;
// message(error.message, "red");
// return;
// } finally {
// process.exit();
// }
// }
// )
.command(
"analyze",
"Analyze your landing page",
Expand All @@ -105,26 +77,12 @@ yargs(hideBin(process.argv))
console.clear();
const config = readConfig("landing-friend-config.ts", "generate");
if (!config) {
message(
"No config detected. Please create one using init command or create it manually",
"red"
);
message("Config not found", "red");
return;
}

const char = ".";
const maxChar = 3;
let progress = "";
const interval = setInterval(() => {
if (progress.length < maxChar) {
progress += char;
console.clear();
message(`Analyzing your page${progress}`, "yellow");
} else progress = "";
}, 500);

try {
await websiteAnalyzer(config, interval);
message("Analyzing your page...", "yellow");
await websiteAnalyzer(config);
} catch (e) {
const error = e as Error;
message(error.message, "red");
Expand All @@ -134,6 +92,48 @@ yargs(hideBin(process.argv))
}
}
)
// .command(
// "analyze",
// "Analyze your landing page",
// {
// help: {
// describe:
// "Analysis of your website through defined values in the config generates HTML and JSON files.",
// },
// },
// async () => {
// console.clear();
// const config = readConfig("landing-friend-config.ts", "generate");
// if (!config) {
// message(
// "No config detected. Please create one using init command or create it manually",
// "red"
// );
// return;
// }

// const char = ".";
// const maxChar = 3;
// let progress = "";
// const interval = setInterval(() => {
// if (progress.length < maxChar) {
// progress += char;
// console.clear();
// message(`Analyzing your page${progress}`, "yellow");
// } else progress = "";
// }, 500);

// try {
// await websiteAnalyzer(config, interval);
// } catch (e) {
// const error = e as Error;
// message(error.message, "red");
// return;
// } finally {
// process.exit();
// }
// }
// )
//!!!!!!!
// USE IT INSTEAD OF BELOW COMMAND IF U WANT CONSOLE.LOG SOMETHING
//
Expand Down

0 comments on commit 895b454

Please sign in to comment.