Skip to content

Commit

Permalink
feat: organize detected tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dipratap committed Sep 12, 2024
1 parent 7a36bcf commit b59c457
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/metatags/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default async function auditMetaTags(message, context) {
for (const [pageUrl, pageTags] of Object.entries(extractedTags)) {
seoChecks.performChecks(pageUrl, pageTags);
}
seoChecks.organizeDetectedTags();
const detectedTags = seoChecks.getDetectedTags();
// Prepare Audit result
const auditResult = {
Expand Down
21 changes: 21 additions & 0 deletions src/metatags/seo-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class SeoChecks {
};
}

/**
* Sorts Non Unique H1 tags in descending order of their occurrence count
*/
sortNonUniqueH1Tags() {
if (!this.detectedTags[H1][0] || !this.detectedTags[H1][0][NON_UNIQUE]) {
return;
}
// Convert the non-unique H1 tags object to an array of [key, value] entries
const sortedEntries = Object.entries(this.detectedTags[H1][0][NON_UNIQUE])
.sort(([, a], [, b]) => b.count - a.count); // Sort by `count` in descending order

this.detectedTags[H1][0][NON_UNIQUE] = Object.fromEntries(sortedEntries);
}

/**
* Adds an entry to the detected tags array.
* @param {string} pageUrl - The URL of the page.
Expand Down Expand Up @@ -195,6 +209,13 @@ class SeoChecks {
getDetectedTags() {
return this.detectedTags;
}

/**
* Processes detected tags, including sorting non-unique H1 tags.
*/
organizeDetectedTags() {
this.sortNonUniqueH1Tags();
}
}

export default SeoChecks;
24 changes: 24 additions & 0 deletions test/audits/metatags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
H1,
HIGH,
MODERATE,
NON_UNIQUE,
} from '../../src/metatags/constants.js';
import SeoChecks from '../../src/metatags/seo-checks.js';
import auditMetaTags from '../../src/metatags/handler.js';
Expand Down Expand Up @@ -157,6 +158,29 @@ describe('Meta Tags', () => {
});
});
});

describe('Organize Detected Tags', () => {
it('should sort non-unique H1 tags by count in descending order', () => {
seoChecks.detectedTags = {
h1: [
{
[NON_UNIQUE]: {
'Tag A': { count: 3, urls: ['/url1', '/url2'] },
'Tag B': { count: 5, urls: ['/url3'] },
'Tag C': { count: 1, urls: ['/url4'] },
},
},
],
};
seoChecks.sortNonUniqueH1Tags();
const expected = {
'Tag B': { count: 5, urls: ['/url3'] },
'Tag A': { count: 3, urls: ['/url1', '/url2'] },
'Tag C': { count: 1, urls: ['/url4'] },
};
expect(seoChecks.detectedTags[H1][0][NON_UNIQUE]).to.deep.equal(expected);
});
});
});

describe('handler method', () => {
Expand Down

0 comments on commit b59c457

Please sign in to comment.