Skip to content

Commit

Permalink
duplicated html
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil-aexol committed Nov 20, 2023
1 parent 71849e1 commit a02181d
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/landing-friend-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@landing-friend/core",
"version": "0.0.22",
"version": "0.0.23",
"description": "",
"main": "lib/index.js",
"keywords": [],
Expand Down
17 changes: 14 additions & 3 deletions packages/landing-friend-core/src/searchDuplicated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ import {
matchedSetting,
message,
pathName,
prepareDuplicatedHtml,
saveFile,
} from "@/index.js";

export const searchDuplicated = async (config: ConfigFile, interval?: NodeJS.Timer) => {
const { input, searchDuplicated, excludedPage } = config;
const { input, domain, searchDuplicated, excludedPage } = config;

if (!searchDuplicated) {
return message("Define analyzer in config", "redBright");
}

let contentArray: FileWithDuplicateContent[] = [];
const contentArrayWithDuplication: FileWithDuplicateContent[] = [];
const dataToJson: FileWithDuplicateContent = {};

const allHtmlFiles = getHtmlFiles(input, false);
Expand All @@ -38,7 +40,6 @@ export const searchDuplicated = async (config: ConfigFile, interval?: NodeJS.Tim
file: file.replace("\\", "/"),
input: input.replace(/\.\//g, ""),
});

if (contentArray.length > 0) {
contentArray = findAndStoreDuplicates(fileWithContent, contentArray, file);
} else {
Expand Down Expand Up @@ -75,11 +76,21 @@ export const searchDuplicated = async (config: ConfigFile, interval?: NodeJS.Tim
delete dataToJson[key];
}
});
Object.entries(dataToJson).forEach(([key, value]) => {
const dataToArray: FileWithDuplicateContent = {};
dataToArray[key] = value;
contentArrayWithDuplication.push(dataToArray);
});

const htmlWithTablesAndCharts = prepareDuplicatedHtml({
dataArray: contentArrayWithDuplication,
domain: domain,
});

clearTimeout(interval);
try {
saveFile(pathName(FileName.duplicated, ".json"), JSON.stringify(dataToJson, null, 2));
// saveFile(pathName(FileName.duplicated, ".html"), htmlWithTablesAndCharts);
saveFile(pathName(FileName.duplicated, ".html"), htmlWithTablesAndCharts);
message(
"Your website has been analyzed, JSON and html files have been generated in ./SEO",
"green"
Expand Down
2 changes: 1 addition & 1 deletion packages/landing-friend-core/src/utils/analyzer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "../functions/index.js";
export * from "./analyzers/index.js";
export * from "./functions/index.js";
export * from "./checkContent/index.js";
export * from "./prepareHTML.js";
export * from "./sections/index.js";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DuplicatedContentWithName, DuplicatedSearchName } from "@/index.js";

const nameChanger = (name: DuplicatedSearchName) => {
return name === DuplicatedSearchName.SameMetaDesc
? "Found duplicated meta description"
: name === DuplicatedSearchName.SamePage
? "Found duplicated pages"
: "Found duplicated page title";
};

interface GenerateContent {
contentWithOption: DuplicatedContentWithName;
name: DuplicatedSearchName;
}

export const generateContentToRow = ({ contentWithOption, name }: GenerateContent) => {
const value = contentWithOption[name];
if (!value) return;
return {
numberOfErrors: value.numberOfDuplicates,
content: `
<tr>
<td>${nameChanger(name)}: <strong>${value.numberOfDuplicates}</strong></td>
<td>${value.duplicatesOnSite.map(d => `<div style="padding: 2px 0;">${d}</div>`).join("")}</td>
</tr>
`,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { DuplicatedSearchName, FileWithDuplicateContent, generateContentToRow } from "@/index.js";

interface Row {
fileWithContent: FileWithDuplicateContent;
tableIndex: number;
}

export const generateRow = ({ fileWithContent, tableIndex }: Row): string => {
let rows = "";
if (!fileWithContent) return "";

const row = Object.entries(fileWithContent).map(([fileName, contentWithOption]) => {
let allErrors = 0;
let contentToRow = "";
for (const name of Object.values(DuplicatedSearchName)) {
const data = generateContentToRow({ contentWithOption, name });
if (data) {
allErrors += data.numberOfErrors;
contentToRow += data.content;
}
}

return `
<table>
<thead>
<tr>
<th colspan="2">
${fileName} | Number of errors: (${allErrors})
<span class="toggle-button" id="toggle-button-${tableIndex}">▼</span>
</th>
</tr>
</thead>
<tbody id="toggle-body-${tableIndex}" class="hidden">
${contentToRow}
</tbody>
<script>
document.addEventListener("DOMContentLoaded", () => {
const toggleButton = document.getElementById("toggle-button-${tableIndex}");
const toggleBody = document.getElementById("toggle-body-${tableIndex}");
toggleButton &&
toggleBody &&
toggleButton.addEventListener("click", () => {
if (toggleBody.classList.contains("hidden")) {
toggleBody.classList.remove("hidden");
toggleButton.textContent = "▲";
} else {
toggleBody.classList.add("hidden");
toggleButton.textContent = "▼";
}
});
})
</script>
</table>
`;
});
rows = rows + row.join("");
return rows;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FileWithDuplicateContent, generateRow } from "@/index.js";

export const generateRows = (dataArray: FileWithDuplicateContent[]) => {
let rows = "";
dataArray
.map((fileWithContent, tableIndex) => {
rows = rows + generateRow({ fileWithContent, tableIndex });
})
.join("");

return rows;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./generateContentToRow.js";
export * from "./generateRow.js";
export * from "./generateRows.js";
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./functions/index.js";
export * from "./findAndStoreDuplicates.js";
export * from "./generateHtmlContent/index.js";
export * from "./getContent.js";
export * from "./prepareDuplicatedHtml.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { FileWithDuplicateContent, generateRows } from "@/index.js";

interface Props {
dataArray: FileWithDuplicateContent[];
domain: string;
}

export const prepareDuplicatedHtml = ({ dataArray, domain }: Props): string => {
const rows = generateRows(dataArray);

return `
<!DOCTYPE html>
<html>
<head>
<title>SEO analyze</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f9f9f9;
}
h1 {
margin-bottom: 20px;
}
h4 {
word-break: break-word;
margin-bottom: 12px;
}
table {
border-collapse: collapse;
width: 100%;
margin: 30px 0px 60px;
}
th,
td {
border: 1px solid black;
padding: 8px;
text-align: left;
justify-content: space-between;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.center {
text-align: center;
}
.toggle-button {
cursor: pointer;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<h1>Duplicated pages for ${domain}</h1>
<table>
${rows}
</table>
</body>
</html>
`;
};
4 changes: 2 additions & 2 deletions packages/landing-friend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@landing-friend/cli",
"version": "0.0.22",
"version": "0.0.23",
"description": "",
"type": "module",
"main": "lib/CLI.js",
Expand All @@ -16,7 +16,7 @@
"watch": "ttsc --watch"
},
"dependencies": {
"@landing-friend/core": "^0.0.22",
"@landing-friend/core": "^0.0.23",
"inquirer": "9.2.8",
"node-fetch": "3.3.0",
"yargs": "17.6.0"
Expand Down

0 comments on commit a02181d

Please sign in to comment.