Skip to content

Commit 90cfe31

Browse files
committed
Merge branch 'develop' into test-fixes
2 parents a4336db + 7ab71aa commit 90cfe31

29 files changed

+7213
-4023
lines changed

build/__test-utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const outdent = require('outdent').default
2+
const parsePage = require('../utils/parse-page')
3+
4+
function createGlossary(dfnObj) {
5+
const dfnEntries = Object.entries(dfnObj)
6+
const dfnStrings = dfnEntries.map(dfnFileContent)
7+
return dfnStrings.map(parsePage)
8+
}
9+
10+
function dfnFileContent([term, content]) {
11+
const title = term[0].toUpperCase() + term.substr(1)
12+
return outdent`
13+
---
14+
title: ${title}
15+
key: ${term}
16+
---
17+
${content}
18+
`
19+
}
20+
21+
module.exports = {
22+
dfnFileContent,
23+
createGlossary,
24+
}

build/taskforce-markdown.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const path = require('path')
2+
const getMarkdownData = require('../utils/get-markdown-data')
3+
const createFile = require('../utils/create-file')
4+
const getRuleContent = require('./taskforce-rule-page/get-rule-content')
5+
const getDefinitionContent = require('./taskforce-rule-page/get-definition-content')
6+
const program = require('commander')
7+
8+
const rulesDirDefault = path.resolve(__dirname, '../node_modules/act-rules-community/_rules')
9+
const glossaryDirDefault = path.resolve(__dirname, '../node_modules/act-rules-community/pages/glossary')
10+
11+
program
12+
.option('-i, --ruleIds <id_list>', 'comma separated list of IDs', val => val.split(','))
13+
.option('-o, --outDir <dirname>', 'Path to output dir')
14+
.option('-r, --rulesDir <dirname>', 'Path to _rules directory')
15+
.option('-g, --glossaryDir <dirname>', 'Path to glossary directory')
16+
.parse(process.argv)
17+
18+
taskforceMarkdown(program)
19+
.then(() => {
20+
console.log('Created taskforce markdown files')
21+
process.exit()
22+
})
23+
.catch(e => {
24+
console.error(e)
25+
process.exit(1)
26+
})
27+
28+
async function taskforceMarkdown({
29+
rulesDir = rulesDirDefault,
30+
glossaryDir = glossaryDirDefault,
31+
ruleIds = [],
32+
outDir = './content/',
33+
}) {
34+
const rulesData = getMarkdownData(rulesDir)
35+
const glossary = getMarkdownData(glossaryDir)
36+
const glossaryFiles = new Set()
37+
38+
for (const ruleData of rulesData) {
39+
if (ruleIds.length && !ruleIds.includes(ruleData.frontmatter.id)) {
40+
continue
41+
}
42+
const { filepath, content } = buildTfRuleFile(ruleData, glossary)
43+
await createFile(path.resolve(outDir, filepath), content)
44+
45+
const definitions = parseDefinitions(content)
46+
definitions.forEach(dfn => glossaryFiles.add(dfn))
47+
}
48+
49+
for (const definition of glossaryFiles) {
50+
const { filepath, content } = buildTfDefinitionFile(definition, glossary)
51+
await createFile(path.resolve(outDir, filepath), content)
52+
}
53+
}
54+
55+
function buildTfRuleFile(ruleData, glossary) {
56+
return {
57+
filepath: ruleData.filename,
58+
content: getRuleContent(ruleData, glossary),
59+
}
60+
}
61+
62+
function buildTfDefinitionFile(definitionKey, glossary) {
63+
return {
64+
filepath: `glossary/${definitionKey}.md`,
65+
content: getDefinitionContent(definitionKey, glossary),
66+
}
67+
}
68+
69+
function parseDefinitions(content) {
70+
const definitionKeys = []
71+
const matches = content.match(/{%[^%]*%}/g)
72+
matches.forEach(str => {
73+
const match = str.match(/{%\s+include_relative\s+glossary\/([^.]+).md\s+%}/i)
74+
if (match) {
75+
definitionKeys.push(match[1])
76+
}
77+
})
78+
return definitionKeys
79+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const outdent = require('outdent').default
2+
const getAcknowledgements = require('../get-acknowledgements')
3+
4+
describe('taskforce-markdown', () => {
5+
describe('get-acknowledgements', () => {
6+
const acknowledgements = {
7+
beans: ['great'],
8+
Assets: ['Some text', 'Some other text'],
9+
previous_authors: ['Audrey Maniez', 'Random Person'],
10+
authors: ['Wilco Fiers'],
11+
}
12+
13+
it('returns a string', () => {
14+
const frontmatter = { acknowledgements }
15+
const ackn = getAcknowledgements({ frontmatter })
16+
expect(ackn).toBe(outdent`
17+
## Acknowledgements
18+
19+
This rule was written in the [ACT Rules community group](https://w3.org/community/act-r/),
20+
with the support of the EU-funded [WAI-Tools Project](https://www.w3.org/WAI/about/projects/wai-tools/).
21+
22+
### Authors
23+
24+
- [Wilco Fiers](https://github.com/wilcofiers)
25+
26+
### Previous Authors
27+
28+
- [Audrey Maniez](https://github.com/audreymaniez)
29+
- Random Person
30+
31+
### Assets
32+
33+
- Some text
34+
- Some other text
35+
36+
### Beans
37+
38+
- great
39+
`)
40+
})
41+
})
42+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const outdent = require('outdent').default
2+
const getChangelog = require('../get-changelog')
3+
4+
describe('taskforce-markdown', () => {
5+
describe('get-changelog', () => {
6+
it('returns a static changelog', () => {
7+
const log = getChangelog()
8+
expect(log).toBe(outdent`
9+
## Changelog
10+
11+
This is the first version of this ACT rule.
12+
`)
13+
})
14+
})
15+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const outdent = require('outdent').default
2+
const { createGlossary } = require('../../__test-utils')
3+
const getDefinitionContent = require('../get-definition-content')
4+
5+
describe('getDefinitionContent', () => {
6+
const glossaryBase = {
7+
hello: outdent`
8+
Hello [world][]
9+
10+
[world]: #world
11+
[w3c]: //w3.org
12+
`,
13+
world: outdent`
14+
World of the [ACT-rules community]
15+
16+
[act-rules community]: //act-rules.github.io
17+
`,
18+
outcome: `All good.`,
19+
}
20+
const glossary = createGlossary(glossaryBase)
21+
22+
it('runs', () => {
23+
const taskforceMarkdown = getDefinitionContent('hello', glossary)
24+
25+
expect(taskforceMarkdown).toBe(outdent`
26+
### Hello {#hello}
27+
Hello [world][]
28+
29+
[world]: #world
30+
[w3c]: //w3.org
31+
`)
32+
})
33+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const getFrontmatter = require('../get-frontmatter')
2+
const yaml = require('js-yaml')
3+
4+
function stripDashes(str) {
5+
return str.replace(/---/g, '')
6+
}
7+
8+
describe('taskforce-markdown', () => {
9+
const filenameNoExt = 'hello-world-198j8j'
10+
const ruleData = {
11+
filename: `${filenameNoExt}.md`,
12+
frontmatter: {
13+
name: 'hello world',
14+
},
15+
}
16+
17+
describe('get-frontmatter', () => {
18+
it('starts and ends with a line of "---"', () => {
19+
const lines = getFrontmatter(ruleData).split('\n')
20+
expect(lines[0]).toBe('---')
21+
expect(lines[lines.length - 1]).toBe('---')
22+
})
23+
24+
it('returns valid yaml between the "---"s', () => {
25+
const frontmatter = getFrontmatter(ruleData)
26+
const frontmatterData = stripDashes(frontmatter)
27+
expect(() => {
28+
yaml.safeLoad(frontmatterData)
29+
}).not.toThrow()
30+
})
31+
32+
it('has the appropriate data in the yaml', () => {
33+
const frontmatter = getFrontmatter(ruleData)
34+
const frontmatterData = stripDashes(frontmatter)
35+
const data = yaml.safeLoad(frontmatterData)
36+
37+
expect(data).toEqual({
38+
title: ruleData.frontmatter.name,
39+
permalink: `/standards-guidelines/act/rules/${filenameNoExt}/`,
40+
ref: `/standards-guidelines/act/rules/${filenameNoExt}/`,
41+
lang: 'en',
42+
github: {
43+
repository: `w3c/wcag-act-rules`,
44+
path: `content/${ruleData.filename}`,
45+
},
46+
})
47+
})
48+
49+
it('does not include markdown in the title', () => {
50+
const frontmatter = getFrontmatter({
51+
filename: `${filenameNoExt}.md`,
52+
frontmatter: {
53+
name: '`*Hello*` **world, welcome** to _ACT_taskforce_ **',
54+
},
55+
})
56+
const frontmatterData = stripDashes(frontmatter)
57+
const data = yaml.safeLoad(frontmatterData)
58+
59+
expect(data).toHaveProperty('title', '*Hello* world, welcome to ACT_taskforce **')
60+
})
61+
})
62+
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const parsePage = require('../../../utils/parse-page')
2+
const getGlossary = require('../get-glossary')
3+
const { createGlossary } = require('../../__test-utils')
4+
5+
describe('taskforce-markdown', () => {
6+
describe('get-glossary', () => {
7+
const glossaryBase = {
8+
hello: 'Hello [world](#world).',
9+
world: 'World!',
10+
outcome: 'All good.',
11+
}
12+
const glossary = createGlossary(glossaryBase)
13+
14+
it('includes the outcome', () => {
15+
const rulePage = parsePage('Without definitions [w3](//w3.org)')
16+
const ruleGlossary = getGlossary(rulePage, glossary)
17+
18+
expect(ruleGlossary).toBe('## Glossary\n\n{% include_relative glossary/outcome.md %}')
19+
})
20+
21+
it('sorts definitions in alphabetic order', () => {
22+
const rulePage = parsePage(`[hello](#hello), [world](#world)`)
23+
const ruleGlossary = getGlossary(rulePage, glossary)
24+
expect(ruleGlossary).toBe(
25+
[
26+
'## Glossary\n',
27+
'{% include_relative glossary/hello.md %}',
28+
'{% include_relative glossary/outcome.md %}',
29+
'{% include_relative glossary/world.md %}',
30+
].join('\n')
31+
)
32+
})
33+
34+
it('includes nested definitions', () => {
35+
const rulePage = parsePage('[hello](#hello)')
36+
const ruleGlossary = getGlossary(rulePage, glossary)
37+
expect(ruleGlossary).toBe(
38+
[
39+
'## Glossary\n',
40+
'{% include_relative glossary/hello.md %}',
41+
'{% include_relative glossary/outcome.md %}',
42+
'{% include_relative glossary/world.md %}',
43+
].join('\n')
44+
)
45+
})
46+
})
47+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const outdent = require('outdent').default
2+
const parsePage = require('../../../utils/parse-page')
3+
const getReferenceLinks = require('../get-reference-links')
4+
5+
describe('taskforce-markdown', () => {
6+
describe('get-reference-links', () => {
7+
it('returns a string', () => {
8+
const rulePage = parsePage(outdent`
9+
[hello][], [w3c][]
10+
11+
[hello]: #hello
12+
[w3c]: //w3.org 'W3C website'
13+
`)
14+
15+
const referenceLinks = getReferenceLinks(rulePage, [])
16+
expect(referenceLinks).toBe(outdent`
17+
[hello]: #hello
18+
[w3c]: //w3.org 'W3C website'
19+
`)
20+
})
21+
})
22+
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const outdent = require('outdent').default
2+
const parseMarkdown = require('../../../utils/parse-markdown')
3+
const getRuleBody = require('../get-rule-body')
4+
5+
describe('taskforce-markdown', () => {
6+
describe('get-rule-body', () => {
7+
it('returns the trimmed body if there are no reference links', () => {
8+
const body = outdent`
9+
## Hello world
10+
11+
` // indent is intentional
12+
const markdownAST = parseMarkdown(body)
13+
const stripped = getRuleBody({ body, markdownAST })
14+
15+
expect(stripped).toBe(body.trim())
16+
})
17+
18+
it('returns content without reference links', () => {
19+
const content = outdent`
20+
## Hello
21+
Welcome to the [party][] [time][] in the [ACT Taskforce][]!
22+
23+
## Shopping list
24+
- cake
25+
- chips
26+
- party hats
27+
28+
`
29+
const references = outdent`
30+
[party]: http://w3.org
31+
[time]: <http://w3.org> 'hello world'
32+
33+
[act-taskforce]: https://act-rules.github.io/
34+
35+
` // blank line is intentional
36+
const body = content + '\n' + references
37+
const markdownAST = parseMarkdown(body)
38+
39+
const stripped = getRuleBody({ body, markdownAST })
40+
expect(stripped).toBe(content.trim())
41+
})
42+
})
43+
})

0 commit comments

Comments
 (0)