-
Notifications
You must be signed in to change notification settings - Fork 5
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
94bc45b
commit 7557e0f
Showing
8 changed files
with
9,435 additions
and
491 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: [["@babel/preset-env", { targets: { node: "current" } }]], | ||
}; |
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
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,108 @@ | ||
import { | ||
LEVEL_1_HEADING_REGEX, | ||
LINK_REGEX, | ||
INNER_LINK_REGEX, | ||
disableButton, | ||
enableButton, | ||
invalidLinks, | ||
} from "../utils"; | ||
|
||
describe("Regex", () => { | ||
test("LEVEL_1_HEADING_REGEX", () => { | ||
const testCase1 = "# h1".match(LEVEL_1_HEADING_REGEX); | ||
expect(testCase1).toEqual(["# "]); | ||
|
||
const testCase2 = "## h2".match(LEVEL_1_HEADING_REGEX); | ||
expect(testCase2).toEqual(null); | ||
|
||
const testCase3 = "#something thats not a heading".match( | ||
LEVEL_1_HEADING_REGEX | ||
); | ||
expect(testCase3).toEqual(null); | ||
|
||
const testCase4 = " # space in front".match(LEVEL_1_HEADING_REGEX); | ||
expect(testCase4).toEqual([" # "]); | ||
|
||
const testCase5 = "something else thats not a heading#lalal".match( | ||
LEVEL_1_HEADING_REGEX | ||
); | ||
expect(testCase5).toEqual(null); | ||
}); | ||
|
||
test("LINK_REGEX", () => { | ||
const testCase1 = "<a>A link</a>".match(LINK_REGEX); | ||
expect(testCase1).toEqual(["<a>A link</a>"]); | ||
|
||
const testCase2 = "<div>not a link</div>".match(LINK_REGEX); | ||
expect(testCase2).toEqual(null); | ||
|
||
const testCase3 = "<a class='link' href='google.com'>A link</a>".match( | ||
LINK_REGEX | ||
); | ||
expect(testCase3).toEqual(["<a class='link' href='google.com'>A link</a>"]); | ||
|
||
const testCase4 = "<a class='link'>A link</a>".match(LINK_REGEX); | ||
expect(testCase4).toEqual(["<a class='link'>A link</a>"]); | ||
|
||
const testCase5 = "<a class='link'>A link</a".match(LINK_REGEX); | ||
expect(testCase5).toEqual(null); | ||
}); | ||
|
||
test("INNER_LINK_REGEX", () => { | ||
const testCase1 = "<a>A link</a>".match(INNER_LINK_REGEX); | ||
expect(testCase1).toEqual(["A link"]); | ||
|
||
const testCase2 = "<div>not a link</div>".match(INNER_LINK_REGEX); | ||
expect(testCase2).toEqual(null); | ||
|
||
const testCase3 = | ||
"<a class='link' href='google.com'>descriptive link</a>".match( | ||
INNER_LINK_REGEX | ||
); | ||
expect(testCase3).toEqual(["descriptive link"]); | ||
|
||
const testCase4 = "<a class='link'>a link</a>".match(INNER_LINK_REGEX); | ||
expect(testCase4).toEqual(["a link"]); | ||
}); | ||
}); | ||
|
||
describe("Button Helpers", () => { | ||
test("disableButton", () => { | ||
const button = { ariaDisabled: false, disabled: false }; | ||
disableButton(button); | ||
|
||
expect(button.ariaDisabled).toBe(true); | ||
expect(button.disabled).toBe(true); | ||
}); | ||
|
||
test("enableButton", () => { | ||
const button = { ariaDisabled: true, disabled: true }; | ||
enableButton(button); | ||
|
||
expect(button.ariaDisabled).toBe(false); | ||
expect(button.disabled).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("invalidLinks", () => { | ||
test("all links are valid", () => { | ||
const links = ["<a>A very descriptive link</a>"]; | ||
const results = invalidLinks(links); | ||
|
||
expect(results).toStrictEqual([]); | ||
}); | ||
|
||
test("Upper case strings are converted to lower case", () => { | ||
const links = ["<a>Learn more</a>"]; | ||
const results = invalidLinks(links); | ||
|
||
expect(results).toStrictEqual(["<a>Learn more</a>"]); | ||
}); | ||
|
||
test("all links are invalid", () => { | ||
const links = ["<a>Learn more</a>", "<a>learn more</a>"]; | ||
const results = invalidLinks(links); | ||
|
||
expect(results).toStrictEqual(["<a>Learn more</a>", "<a>learn more</a>"]); | ||
}); | ||
}); |
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,75 @@ | ||
export const LEVEL_1_HEADING_REGEX = /(\n|\s|^)\#\s/g; | ||
export const LINK_REGEX = /<a(.*?)>(.*?)<\/a>/g; | ||
export const INNER_LINK_REGEX = /(?<=>).+?(?=<\/a)/g; | ||
|
||
// TODO: internationalization | ||
const NON_DESCRIPTIVE_LINK_TEXT = [ | ||
"click here", | ||
"click this", | ||
"go", | ||
"here", | ||
"this", | ||
"start", | ||
"right here", | ||
"more", | ||
"learn more", | ||
]; | ||
|
||
export const HELPER_LINKS = { | ||
headings: "https://www.markdownguide.org/basic-syntax/#headings", | ||
linkText: "https://webaim.org/techniques/hypertext/link_text", | ||
}; | ||
|
||
export const disableButton = (button) => { | ||
button.ariaDisabled = true; | ||
button.disabled = true; | ||
}; | ||
|
||
export const enableButton = (button) => { | ||
button.ariaDisabled = false; | ||
button.disabled = false; | ||
}; | ||
|
||
/** Given a list of links check if the link text is descriptive enough */ | ||
export const invalidLinks = (links) => { | ||
return links.filter((link) => { | ||
const description = link.match(INNER_LINK_REGEX) || []; | ||
return NON_DESCRIPTIVE_LINK_TEXT.includes(description[0].toLowerCase()); | ||
}); | ||
}; | ||
|
||
/** Create an error message <li> */ | ||
export const createErrorListItem = (title, message) => { | ||
const listItem = document.createElement("li"); | ||
const heading = document.createElement("h3"); | ||
const paragraph = document.createElement("p"); | ||
|
||
heading.innerHTML = title; | ||
paragraph.innerHTML = message; | ||
|
||
listItem.append(heading); | ||
listItem.append(paragraph); | ||
|
||
return listItem; | ||
}; | ||
|
||
/** Create a <li> for each textArea */ | ||
export const createFeedbackGroup = (id, highlightEvent) => { | ||
const listItem = document.createElement("li"); | ||
const heading = document.createElement("h2"); | ||
const divider = document.createElement("hr"); | ||
const highlightButton = document.createElement("button"); | ||
|
||
listItem.classList.add("feedbackGroup"); | ||
divider.classList.add("divider"); | ||
heading.innerText = `Markdown textArea with id: "${id}"`; | ||
highlightButton.innerText = "Find TextArea"; | ||
highlightButton.addEventListener("click", highlightEvent); | ||
|
||
listItem.append(heading); | ||
listItem.append(divider); | ||
listItem.append(feedback); | ||
listItem.append(highlightButton); | ||
|
||
return listItem; | ||
}; |
Oops, something went wrong.