Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hat-ring-components",
"version": "4.4.4",
"version": "4.5.0",
"description": "Head App Template - RING components",
"license": "MIT",
"repository": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { context, blockData }: OrderedListBlockParams =
Astro.props as OrderedListBlockParams;
import { WidgetHelper_renderEmptyComponent } from "../../../../../helpers/WidgetHelper";
import {LinkReplacerHelper_replaceLinks} from '../../../../../helpers/LinkReplacerHelper';
import { SeoLinkWhitelistHelper_processLinks } from "../../../../../helpers/seo/SeoLinkWhitelistHelper";
import { AppContext } from "../../../../../types/types";

export interface OrderedListBlockParams {
Expand All @@ -15,14 +16,16 @@ export interface OrderedListBlockParams {
startValue: number;
};
}
let toRender: boolean | JSX.Element = false;
if (!blockData) {
let toRender: boolean | JSX.Element | string = false;
if (!blockData?.entries?.length) {
toRender = WidgetHelper_renderEmptyComponent("OrderedListBlock");
} else {
blockData.entries = await Promise.all(blockData.entries.map(async (entry) => {
let processed = await LinkReplacerHelper_replaceLinks(context, entry);
return SeoLinkWhitelistHelper_processLinks(context, processed);
}));
}

blockData.entries = await Promise.all(blockData.entries.map(async (entry) => {
return LinkReplacerHelper_replaceLinks(context, entry);
}));
---

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { context, blockData }: ParagraphBlockParams = Astro.props as ParagraphBlockParams;
import { WidgetHelper_renderEmptyComponent } from "../../../../../helpers/WidgetHelper";
import {LinkReplacerHelper_replaceLinks} from '../../../../../helpers/LinkReplacerHelper';
import { SeoLinkWhitelistHelper_processLinks } from "../../../../../helpers/seo/SeoLinkWhitelistHelper";
import { AppContext } from "../../../../../types/types";

export interface ParagraphBlockParams {
Expand All @@ -17,6 +18,7 @@ if (!blockData) {
}

blockData.text = await LinkReplacerHelper_replaceLinks(context, blockData.text);
blockData.text = await SeoLinkWhitelistHelper_processLinks(context, blockData.text);
---

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { context, blockData }: UnorderedListBlockParams =
Astro.props as UnorderedListBlockParams;
import { WidgetHelper_renderEmptyComponent } from "../../../../../helpers/WidgetHelper";
import {LinkReplacerHelper_replaceLinks} from '../../../../../helpers/LinkReplacerHelper';
import { SeoLinkWhitelistHelper_processLinks } from "../../../../../helpers/seo/SeoLinkWhitelistHelper";
import { AppContext } from "../../../../../types/types";

export interface UnorderedListBlockParams {
Expand All @@ -14,15 +15,16 @@ export interface UnorderedListBlockParams {
styleType: string;
};
}
let toRender: boolean | JSX.Element = false;
let toRender: boolean | JSX.Element | string = false;

if (!blockData) {
if (!blockData?.entries?.length) {
toRender = WidgetHelper_renderEmptyComponent("UnorderedListBlock");
} else {
blockData.entries = await Promise.all(blockData.entries.map(async (entry) => {
let processed = await LinkReplacerHelper_replaceLinks(context, entry);
return SeoLinkWhitelistHelper_processLinks(context, processed);
}));
}

blockData.entries = await Promise.all(blockData.entries.map(async (entry) => {
return LinkReplacerHelper_replaceLinks(context, entry);
}));
---

{
Expand Down
38 changes: 37 additions & 1 deletion src/configs/SEOWebsitesConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export let SEOWebsitesConfig
"fields": [
"metaData.customMetaTags"
]
},
"contentLinks": {
"type": "group",
"name": "Content Link Settings",
"fields": [
"contentLinks.enableDomainWhitelist",
"contentLinks.whitelistedDomains"
]
}
},
"keys": [
Expand All @@ -76,7 +84,8 @@ export let SEOWebsitesConfig
"seoLanguages",
"rssDefault",
"seoOpenGraph",
"metaData"
"metaData",
"contentLinks"
]
}
],
Expand Down Expand Up @@ -121,6 +130,10 @@ export let SEOWebsitesConfig
},
"metaData": {
"customMetaTags": []
},
"contentLinks": {
"enableDomainWhitelist": false,
"whitelistedDomains": []
}
},
"paramsDescription": {
Expand Down Expand Up @@ -338,6 +351,29 @@ export let SEOWebsitesConfig
}
]
}
},
"contentLinks": {
"enableDomainWhitelist": {
"name": "Enable domain whitelist for content links",
"type": "checkbox"
},
"whitelistedDomains": {
"type": "treeobject",
"name": "Whitelisted domains",
"description": "List of whitelisted domains. All links from content outside this list will have 'nofollow'.",
"properties": [
{
"name": "domain",
"description": "Domain name (e.g., example.com, example.xy.com)",
"type": "textfield"
},
{
"name": "relAttribute",
"description": "Rel attribute value for this domain (leave empty for default), e.g., noreferrer, noopener",
"type": "textfield"
}
]
}
}
}
}
5 changes: 5 additions & 0 deletions src/helpers/ConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,8 @@ export async function ConfigHelper_getMainCategoryUuid(context) {
const developerSettings = await ConfigHelper_getDeveloperSettingsConfig(context);
return developerSettings ? developerSettings.mainCategoryUuid : '';
}

export async function ConfigHelper_getContentLinksConfig(context) {
return ConfigHelper_getConfig(context, 'contentLinks');
}

98 changes: 98 additions & 0 deletions src/helpers/seo/SeoLinkWhitelistHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { AppContext } from "../../types/types";
import { ConfigHelper_getContentLinksConfig } from "../ConfigHelper";

interface WhitelistedDomain {
domain: string;
relAttribute?: string;
}

interface ContentLinksConfig {
enableDomainWhitelist?: boolean;
whitelistedDomains?: WhitelistedDomain[];
}

const LINK_REGEX = /<a\s+[^>]*?href=["'](https?:\/\/[^"']+)["'][^>]*?>/gi;
const REL_REGEX = /\s+rel=["']([^"']*)["']/i;

export async function SeoLinkWhitelistHelper_processLinks(context: AppContext, text: string): Promise<string> {
if (!text) {
return text;
}

try {
const contentLinksConfig = await ConfigHelper_getContentLinksConfig(context) as ContentLinksConfig;
const whitelistedDomainsList = contentLinksConfig?.whitelistedDomains;

if (!contentLinksConfig?.enableDomainWhitelist || !whitelistedDomainsList?.length) {
return text;
}

const whitelistedDomains = whitelistedDomainsList
.filter((item) => item.domain?.trim())
.map((item) => ({
...item,
domain: item.domain.trim().toLowerCase()
}));

if (whitelistedDomains.length === 0) {
return text;
}

return text.replace(LINK_REGEX, (matchedLink, href) => {
let domain: string;
try {
domain = new URL(href).hostname.toLowerCase();
} catch {
return matchedLink;
}

const matchWithWhitelist = whitelistedDomains.find((item) =>
domain === item.domain || domain.endsWith(`.${item.domain}`)
);

const existingRelMatch = matchedLink.match(REL_REGEX);
const currentRelAttributes = existingRelMatch?.[1]
? existingRelMatch[1].split(/\s+/).filter(Boolean)
: [];

if (matchWithWhitelist) {
// Remove nofollow for whitelisted domains
const filtered = currentRelAttributes.filter((attr) => attr !== "nofollow");

// Add configured rel attributes
if (matchWithWhitelist.relAttribute) {
const configAttrs = matchWithWhitelist.relAttribute.toLowerCase().split(/\s+/).filter(Boolean);
configAttrs.forEach((attr) => {
if (!filtered.includes(attr)) {
filtered.push(attr);
}
});
}
currentRelAttributes.length = 0;
currentRelAttributes.push(...filtered);
} else {
// Add nofollow for non-whitelisted domains
if (!currentRelAttributes.includes("nofollow")) {
currentRelAttributes.push("nofollow");
}
}

const newRelString = currentRelAttributes.length > 0
? ` rel="${currentRelAttributes.join(" ")}"`
: "";

if (existingRelMatch) {
return matchedLink.replace(REL_REGEX, newRelString);
}

if (newRelString) {
return matchedLink.slice(0, -1) + newRelString + ">";
}

return matchedLink;
});
} catch (error) {
console.error("SeoLinkWhitelistHelper: Error processing links", error);
return text;
}
}