Skip to content

Commit

Permalink
try catch for fetch page hide/show arrow to analize html
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil-aexol committed Nov 8, 2023
1 parent 980461e commit 648146b
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ landing/out
.next/
.env
out
out_test
landing-friend-config.ts
public
SEO
106 changes: 52 additions & 54 deletions packages/landing-friend-core/src/data/exclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,79 +106,77 @@ export const unicode = {
};

export const staticTags = [
"strong",
"em",
"span",
"a",
"abbr",
"acronym",
"address",
"audio",
"b",
"bdi",
"bdo",
"blockquote",
"br",
"style",
"p",
"canvas",
"caption",
"cite",
"code",
"col",
"colgroup",
"dd",
"del",
"dfn",
"div",
"svg",
"a",
"path",
"dl",
"dt",
"em",
"embed",
"figure",
"figcaption",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"ul",
"ol",
"li",
"dl",
"dt",
"dd",
"blockquote",
"q",
"cite",
"code",
"pre",
"abbr",
"acronym",
"address",
"b",
"big",
"del",
"dfn",
"iframe",
"ins",
"kbd",
"samp",
"sub",
"sup",
"var",
"small",
"li",
"mark",
"ruby",
"rt",
"rp",
"time",
"wbr",
"bdi",
"bdo",
"noscript",
"object",
"ol",
"p",
"param",
"embed",
"iframe",
"canvas",
"path",
"picture",
"pre",
"q",
"rp",
"rt",
"ruby",
"samp",
"script",
"noscript",
"caption",
"col",
"colgroup",
"small",
"span",
"strong",
"style",
"sub",
"sup",
"svg",
"table",
"thead",
"tbody",
"td",
"tfoot",
"tr",
"th",
"td",
"figure",
"figcaption",
"video",
"audio",
"source",
"thead",
"time",
"track",
"picture",
"tr",
"ul",
"var",
"video",
"wbr",
];

export const forbiddenCharacters = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ export const checkFileToAdvanceAnalyzer = async ({
char => content && content.includes(char)
);
if (content && content.includes("https")) {
const response = await fetch(content);
status = response.statusText;
try {
const response = await fetch(content);
status = response.statusText;
} catch (err) {
//
}
}

const metaObject: MetaNameWithProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const checkContent = (tagName: BasicTagsName, fileContent: string) => {
if (captureGroups) {
let content = captureGroups[1];
staticTags.forEach(staticTag => {
const staticTagRegex = new RegExp(`<${staticTag}.*?>|</${staticTag}>`, "g");
const staticTagRegex = new RegExp(
`<${staticTag}.*?>|</${staticTag}>|\\.css.*?}|@media.*?}|{|}`,
"g"
);
Object.entries(unicode).forEach(([unicode, replacement]) => {
const unicodeRegex = new RegExp(`${unicode}`, "g");
content = content.replace(unicodeRegex, replacement);
Expand Down
45 changes: 39 additions & 6 deletions packages/landing-friend-core/src/functions/analyzer/prepareHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ interface DefaultGenerate {
advancedAnalyzer: boolean;
}

type GenerateTable = DefaultGenerate & { combinedTagsPatterns: CombinedPatterns };
type GenerateTable = DefaultGenerate & {
combinedTagsPatterns: CombinedPatterns;
tableIndex: number;
};
type PrepareHtml = DefaultGenerate & { combinedTagsPatterns: CombinedPatterns[] };

const generateTableRows = ({
combinedTagsPatterns,
countKeywords,
countWordsInLast,
advancedAnalyzer,
tableIndex,
}: GenerateTable): string => {
return Object.entries(combinedTagsPatterns)
.map(([file, tagData]) => {
Expand Down Expand Up @@ -73,10 +77,13 @@ const generateTableRows = ({

return `<thead>
<tr>
<th colspan="2">${file}</th>
<th colspan="2">
<span>${file}</span>
<span class="toggle-button" id="toggle-button-${tableIndex}">▼</span>
</th>
</tr>
</thead>
<tbody>
<tbody id="toggle-body-${tableIndex}" class="hidden">
${mainRow}
${
countKeywords
Expand All @@ -94,8 +101,25 @@ const generateTableRows = ({
${metaRow}`
: ""
}
</tbody>
<tr class="empty-row"/>
</tbody>
<tr class="empty-row"/>
<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>
`;
})
.join("");
Expand All @@ -108,14 +132,15 @@ export const prepareHTMLWithTables = ({
advancedAnalyzer,
}: PrepareHtml): string => {
let brokenTagsTable: string = "";
combinedTagsPatterns.map(combinedTagsPattern => {
combinedTagsPatterns.map((combinedTagsPattern, idx) => {
brokenTagsTable =
brokenTagsTable +
generateTableRows({
combinedTagsPatterns: combinedTagsPattern,
countKeywords,
countWordsInLast,
advancedAnalyzer,
tableIndex: idx,
});
});

Expand All @@ -126,6 +151,12 @@ export const prepareHTMLWithTables = ({
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
.toggle-button {
cursor: pointer;
}
.hidden {
display: none;
}
body {
font-family: Arial, sans-serif;
margin: 20px;
Expand All @@ -148,6 +179,7 @@ export const prepareHTMLWithTables = ({
border: 1px solid black;
padding: 8px;
text-align: left;
justify-content: space-between;
}
th {
background-color: #f2f2f2;
Expand Down Expand Up @@ -209,6 +241,7 @@ export const prepareHTMLWithTables = ({
<table>
${brokenTagsTable}
</table>
</body>
</html>`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,30 @@ export const generateMissingKeywordsSection = ({
return "";
};

if (
missingTitleKeywords.length > 0 ||
missingDescriptionKeywords.length > 0 ||
missingLastSentenceKeywords.length > 0
) {
sections += missingKeywordsHeader;
sections += generateMissingKeywordsSection("Title", missingTitleKeywords);
sections += generateMissingKeywordsSection("Description", missingDescriptionKeywords);
sections += generateMissingKeywordsSection("Last Sentence", missingLastSentenceKeywords);
}
if (h1Keywords && h1Keywords.length > 0) {
if (
missingTitleKeywords.length > 0 ||
missingDescriptionKeywords.length > 0 ||
missingLastSentenceKeywords.length > 0
) {
sections += missingKeywordsHeader;
sections += generateMissingKeywordsSection("Title", missingTitleKeywords);
sections += generateMissingKeywordsSection("Description", missingDescriptionKeywords);
sections += generateMissingKeywordsSection("Last Sentence", missingLastSentenceKeywords);
}

if (
toMuchTitleKeywords.length > 0 ||
toMuchDescriptionKeywords.length > 0 ||
toMuchLastSentenceKeywords.length > 0
) {
sections += tooMuchKeywordsHeader;
sections += generateTooMuchKeywordsSection("Title", toMuchTitleKeywords);
sections += generateTooMuchKeywordsSection("Description", toMuchDescriptionKeywords);
sections += generateTooMuchKeywordsSection("Last Sentence", toMuchLastSentenceKeywords);
if (
toMuchTitleKeywords.length > 0 ||
toMuchDescriptionKeywords.length > 0 ||
toMuchLastSentenceKeywords.length > 0
) {
sections += tooMuchKeywordsHeader;
sections += generateTooMuchKeywordsSection("Title", toMuchTitleKeywords);
sections += generateTooMuchKeywordsSection("Description", toMuchDescriptionKeywords);
sections += generateTooMuchKeywordsSection("Last Sentence", toMuchLastSentenceKeywords);
}
} else {
sections = `<tr><td colspan="2"><strong style="color:red">Your h1 doesn't contain any keywords.</strong></td></tr>`;
}

return sections;
Expand Down

0 comments on commit 648146b

Please sign in to comment.