generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 208
[MWPW-179699] [PREFLIGHT] Added a11y links localization check #5169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4c348fe
added a11y links localization check
robert-bogos 24c3354
Merge branch 'stage' into mwpw-179699
robert-bogos 48d5eea
moved localization checks to the general panel
robert-bogos f36d20b
addressing feedback
robert-bogos dc2c42f
Merge branch 'stage' of https://github.com/adobecom/milo into mwpw-17…
robert-bogos 3e40708
hotfix
robert-bogos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,82 @@ | ||
| import { STATUS } from './constants.js'; | ||
| import { getConfig, getLocale } from '../../../utils/utils.js'; | ||
|
|
||
| async function getStatus(url) { | ||
| const tryHead = await fetch(url, { method: 'HEAD', cache: 'no-store' }).catch(() => null); | ||
| if (tryHead?.ok || tryHead?.status === 404) return tryHead.status; | ||
| const res = await fetch(url, { method: 'GET', cache: 'no-store' }).catch(() => null); | ||
| return res?.status || 0; | ||
| } | ||
|
|
||
| function removeLocale(pathname, locales) { | ||
| const { prefix } = getLocale(locales, pathname); | ||
| if (!prefix) return pathname; | ||
| if (pathname === prefix || pathname === `${prefix}/`) return '/'; | ||
| return pathname.startsWith(`${prefix}/`) ? pathname.slice(prefix.length) : pathname; | ||
| } | ||
|
|
||
| function addLocale(basePath, prefix) { | ||
| const cleanBase = basePath.startsWith('/') ? basePath : `/${basePath}`; | ||
| if (!prefix) return cleanBase; | ||
| const cleanPrefix = prefix.startsWith('/') ? prefix : `/${prefix}`; | ||
| return `${cleanPrefix}${cleanBase === '/' ? '' : cleanBase}`; | ||
| } | ||
|
|
||
| function normalizePath(path) { | ||
| if (!path) return '/'; | ||
| if (path === '/') return path; | ||
| return path.replace(/\/+$/, ''); | ||
| } | ||
|
|
||
| export async function runChecks({ area = document } = {}) { | ||
| const { locales } = getConfig(); | ||
| const locale = getLocale(locales, window.location.pathname); | ||
| const links = Array.from(area.querySelectorAll('a[href]')); | ||
| const seen = new Set(); | ||
| const violations = (await Promise.all(links.map(async (linkEl) => { | ||
| const href = linkEl.getAttribute('href'); | ||
| if (!href || href.startsWith('#')) return null; | ||
| const url = new URL(href, window.location.origin); | ||
robert-bogos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const basePath = removeLocale(url.pathname, locales); | ||
| if (url.hash && normalizePath(basePath) === '/') return null; | ||
| const key = `${url.origin}${normalizePath(url.pathname)}`; | ||
| if (seen.has(key)) return null; | ||
robert-bogos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| seen.add(key); | ||
|
|
||
| const currentLocalePath = addLocale(basePath, locale.prefix); | ||
| const isCurrentLocaleLink = normalizePath(url.pathname) | ||
| === normalizePath(currentLocalePath); | ||
|
|
||
| const [localizedStatus, usStatus] = await Promise.all([ | ||
| getStatus(`${url.origin}${currentLocalePath}`), | ||
| getStatus(`${url.origin}${basePath}`), | ||
| ]); | ||
|
|
||
| const shouldFlag = (!isCurrentLocaleLink && [200, 404].includes(localizedStatus)) | ||
robert-bogos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| || (isCurrentLocaleLink && localizedStatus === 404); | ||
|
|
||
| if (shouldFlag) { | ||
| return { | ||
| url: url.href, | ||
| isLocalized: isCurrentLocaleLink, | ||
| usStatus, | ||
| localizedStatus, | ||
| }; | ||
| } | ||
|
|
||
| return null; | ||
robert-bogos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }))).filter(Boolean); | ||
|
|
||
| const violationsCount = violations.length; | ||
| return [{ | ||
| id: 'link-localization', | ||
| title: 'Links', | ||
| status: violationsCount === 0 ? STATUS.PASS : STATUS.FAIL, | ||
| description: violationsCount === 0 | ||
| ? 'All links are localized or valid.' | ||
| : `${violationsCount} link${violationsCount > 1 ? 's' : ''} potentially not localized or invalid.`, | ||
| details: { violations }, | ||
| }]; | ||
| } | ||
|
|
||
| export default { runChecks }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.