Skip to content

Commit

Permalink
make synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
nedredmond committed Jan 14, 2024
1 parent 17fb403 commit b9c1559
Show file tree
Hide file tree
Showing 11 changed files with 575 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
uses: actions/upload-pages-artifact@v3
with:
# Upload entire repository
path: '.'
path: "."
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
4 changes: 4 additions & 0 deletions src/contexts/wcag-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from "react";
import wcag from "../w3c/wcag/wcag22.json";

export const WcagContext = React.createContext(wcag);
11 changes: 0 additions & 11 deletions src/loader.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/loaders/wcag-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { redirect } from "react-router-dom";
import type { WcagItem } from "../types";
import { parse } from "../utils";

const baseRedirectUrl = "https://www.w3.org/TR/WCAG22/" as const;

export default async (item?: WcagItem) =>
item
? redirect(`${baseRedirectUrl}#${parse(item.id)}`)
: redirect(baseRedirectUrl);
5 changes: 1 addition & 4 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import Router from "./router";
import { WcagContextProvider } from "./wcag/wcagContext";

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<WcagContextProvider>
<Router />
</WcagContextProvider>
<Router />
</React.StrictMode>,
);
120 changes: 74 additions & 46 deletions src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,88 @@ import type { RouteObject } from "react-router-dom";
import { RouterProvider, createHashRouter } from "react-router-dom";
import type { WcagResponseData } from "./types";
import { pathAliases } from "./types";
import { WcagContext } from "./wcag/wcagContext";
import loader from "./loader";
import { WcagContext } from "./contexts/wcag-context";
import loader from "./loaders/wcag-loader";
import { parse } from "./utils";
import { Text } from "./components/text";

const wcagRoutes = (wcag: WcagResponseData) =>
wcag.principles
.map((principle): RouteObject[] =>
pathAliases.map((pKey) => ({
path: `wcag/${parse(principle[pKey])}`,
loader: () => loader(principle),
children: principle.guidelines
.map((guideline): RouteObject[] =>
pathAliases.map((gKey) => ({
path: `wcag/${parse(principle[pKey])}/${parse(guideline[gKey])}`,
loader: () => loader(guideline),
children: guideline.successcriteria
.map((successcriterion): RouteObject[] =>
pathAliases.map((scKey) => ({
path: `wcag/${parse(principle[pKey])}/${parse(
guideline[gKey],
)}/${parse(successcriterion[scKey])}`,
loader: () => loader(successcriterion),
})),
)
.flat(),
})),
)
.flat(),
})),
)
.flat();
// const wcagRoutes = (wcag: WcagResponseData): RouteObject[] => [
// {
// path: "wcag2",
// loader: () => loader(),
// },
// ...wcag.principles
// .map((principle): RouteObject[] =>
// pathAliases.map((pKey) => ({
// path: `wcag2/${parse(principle[pKey])}`,
// loader: () => loader(principle),
// children: principle.guidelines
// .map((guideline): RouteObject[] =>
// pathAliases.map((gKey) => ({
// path: `wcag2/${parse(principle[pKey])}/${parse(guideline[gKey])}`,
// loader: () => loader(guideline),
// children: guideline.successcriteria
// .map((successcriterion): RouteObject[] =>
// pathAliases.map((scKey) => ({
// path: `wcag2/${parse(principle[pKey])}/${parse(
// guideline[gKey],
// )}/${parse(successcriterion[scKey])}`,
// loader: () => loader(successcriterion),
// })),
// )
// .flat(),
// })),
// )
// .flat(),
// })),
// )
// .flat(),
// ];

const wcagRoutes = (wcag: WcagResponseData): RouteObject[] => [
{
path: "wcag2",
loader: () => loader(),
children: [
...wcag.principles
.map((principle): RouteObject[] =>
pathAliases.map((pKey) => ({
path: `${parse(principle[pKey])}`,
loader: () => loader(principle),
children: principle.guidelines
.map((guideline): RouteObject[] =>
pathAliases.map((gKey) => ({
path: `${parse(guideline[gKey])}`,
loader: () => loader(guideline),
children: guideline.successcriteria
.map((successcriterion): RouteObject[] =>
pathAliases.map((scKey) => ({
path: `${parse(successcriterion[scKey])}`,
loader: () => loader(successcriterion),
})),
)
.flat(),
})),
)
.flat(),
})),
)
.flat(),
],
},
];

const router = (data: WcagResponseData) =>
createHashRouter([
{
path: "/",
element: (
<Text>
Add the spec to the URL like so: <code>/#/wcag/1</code> to go to the
first principal, or <code>/#/wcag/1/1/1</code> to go to the first
Add the spec to the URL like so: <code>/#/wcag2/1</code> to go to the
first principal, or <code>/#/wcag2/1/1/1</code> to go to the first
success criterion. <br /> You can also use the names of principles,
guidelines, and criteria, like so:
<code> /#/wcag/robust/compatible/name-role-value</code>
<code> /#/wcag2/robust/compatible/name-role-value</code>
</Text>
),
},
Expand All @@ -57,19 +95,9 @@ const router = (data: WcagResponseData) =>
},
]);

const Router = () => (
<WcagContext.Consumer>
{({ loading, data }) => {
if (loading) {
return <Text>Loading WCAG spec...</Text>;
}
if (!data) {
return <Text>Something went wrong!</Text>;
}
console.log("router", router(data).routes);
return <RouterProvider router={router(data)} />;
}}
</WcagContext.Consumer>
);
const Router = () => {
const data = React.useContext(WcagContext);
return <RouterProvider router={router(data)} />;
};

export default Router;
6 changes: 1 addition & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
export const parseId = (handle: string) => handle.split(":")[1];

export const parseNum = (num: string) => num.split(".").pop()!;

export const parse = (segment: string) => {
if (segment.startsWith("WCAG2")) {
return parseId(segment);
}
if (segment.includes(".") || !isNaN(Number(segment))) {
return parseNum(segment);
}
return segment;
};
30 changes: 30 additions & 0 deletions src/w3c/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
This work is being provided by the copyright holders under the following license.

License

By obtaining and/or copying this work, you (the licensee) agree that you have read, understood, and will comply with the following terms and conditions.

Permission to copy, modify, and distribute this work, with or without modification, for any purpose and without fee or royalty is hereby granted, provided that you include the following on ALL copies of the work or portions thereof, including modifications:

- The full text of this NOTICE in a location viewable to users of the redistributed or derivative work.
- Any pre-existing intellectual property disclaimers, notices, or terms and conditions. If none exist, the W3C software and document short notice should be included.
- Notice of any changes or modifications, through a copyright statement on the new code or document such as "This software or document includes material copied from or derived from [title and URI of the W3C document]. Copyright © [$year-of-document] World Wide Web Consortium. https://www.w3.org/copyright/software-license-2023/"

Disclaimers

THIS WORK IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR DOCUMENT WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.

COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENT.

The name and trademarks of copyright holders may NOT be used in advertising or publicity pertaining to the work without specific, written prior permission. Title to copyright in this work will at all times remain with copyright holders.

Versions

This version: https://www.w3.org/copyright/software-license-2023/
Latest version: https://www.w3.org/copyright/software-license/
Previous version: https://www.w3.org/copyright/software-license-2015/

Changes since the previous document

The 2023 version updates identification of the copyright holder.

Loading

0 comments on commit b9c1559

Please sign in to comment.