-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
471f997
commit 9048be0
Showing
14 changed files
with
336 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/landing-friend-core/src/functions/analyzer/advancedAnalyzer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.