-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbylawlinksplugin.js
197 lines (180 loc) · 6.44 KB
/
bylawlinksplugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const map = require('unist-util-map');
const visit = require('unist-util-visit');
const is = require('unist-util-is');
const position = require('unist-util-position');
const fs = require('fs');
const powersStateCouncilMustNotDelegate = fs.readFileSync('docs/constitution/schedule-04-powers-state-council-must-not-delegate.md', 'utf-8').match(/<td>.*?<\/td>/g).map((cell) => {
const clean = cell.replace('<td>', '').replace('</td>', '');
if (clean.startsWith('Clause')) {
if (clean.indexOf('and') !== -1) {
const nums = clean.split(' and ');
const clauseNum1 = nums[0].split(' ')[1].replace('(', '.').replace(')', '');
const clauseNum2 = clauseNum1.split('.')[0] + nums[1].replace('(', '.').replace(')', '');
return [clauseNum1, clauseNum2]
} else {
const clauseNum = clean.split(' ')[1].replace('(', '.').replace(')', '');
return [clauseNum]
}
}
}).flat().filter(v => v);
const clauseDates = {
"40.1": "immediately",
"4.1": "on 1 February 2022",
"4.2": "on 1 February 2022",
"4.3": "on 1 February 2022",
"4.4": "on 1 February 2022",
"35.1": "on 1 February 2022 as it applies to the New State Council",
"35.2": "on 1 February 2022 as it applies to the New State Council",
"35.3": "on 1 February 2022 as it applies to the New State Council",
"35.4": "on 1 February 2022 as it applies to the New State Council",
"36.1": "on 1 February 2022 as it applies to the New State Council",
"36.2": "on 1 February 2022 as it applies to the New State Council",
"36.3": "on 1 February 2022 as it applies to the New State Council",
"36.4": "on 1 February 2022 as it applies to the New State Council",
"21.2": "on 1 January 2023",
};
const clausesMentioningDays = ["12.2", "12.3", "23.2.1", "23.2.2", "28.3.b.1", "28.3.b.2", "35.3.d", "39.1.b", "39.1.b.1", "39.1.b.2", "39.3", "39.5"];
const lettersOfTheAlphabet = 'abcdefghijklmnopqrstuvwxyz';
function generateSlug(headline, links = []) {
let slug = headline;
// count if there are any duplicates on the page
const dupeCount = links.reduce((m, i) => {
if (slug === i) m++
return m
}, 0)
links.push(slug)
// append the count to the end of the slug if necessary
if (dupeCount > 0) {
slug = `${slug}-${dupeCount}`
}
return slug
}
module.exports = function bylawLinksPlugin({
} = {}) {
return function transformer(tree, file) {
const value = String(file)
const isSchedule = (file.history[0].indexOf('constitution/schedule') !== -1);
// this array keeps track of existing slugs to prevent duplicates per-page
const links = []
const processed = [];
function enumerateChild(prefix, letters, child, i) {
if (processed.includes(child)) { return }
let marker = Number(
value
.slice(
position.start(child).offset,
position.start(child.children[0]).offset
)
.replace(/[\s.)]/g, '')
.replace(/\[[x ]?]\s*$/i, '')
)
if (letters) {
marker = lettersOfTheAlphabet[marker-1];
}
var idx = i + 1;
child.id = prefix ? prefix + '.' + marker : marker.toString();
let childHasLetters = false;
let heading = null;
child.children.filter(function(child) {
if (child.type == 'jsx' && child.value == '<subclause-letters>') {
childHasLetters = true;
}
if (child.type == 'heading') {
heading = child.data.id;
}
return child.type === 'list';
}).forEach(function(list) {
list.children.forEach(enumerateChild.bind(null, child.id, childHasLetters));
});
if (heading) {
return;
}
const slug = generateSlug(child.id, links);
let clauseAnnotations = [];
if (file.history[0].indexOf('constitution') !== -1) {
let mustnotDelegate = powersStateCouncilMustNotDelegate.includes(slug);
if (mustnotDelegate && !isSchedule) {
clauseAnnotations.push('nodelegate');
}
if (clauseDates[slug] && !isSchedule) {
clauseAnnotations.push(`effectivefrom="${clauseDates[slug]}"`);
}
if (clausesMentioningDays.includes(slug) && !isSchedule) {
clauseAnnotations.push('days');
}
}
if (child.children && is(child.children[0], 'paragraph')) {
if (clauseAnnotations.length > 0) {
child.children[0].children.unshift({
type: 'jsx',
value: `<ClauseAnnotation ${clauseAnnotations.join(" ")} />`
});
}
child.children[0].children.unshift({
type: 'html',
value: `<a aria-hidden="true" tabindex="-1" class="anchor anchor-ref" id="${slug}"></a>`,
})
child.children[0].children.push({
type: 'html',
value: `<a class="hash-link" href="#${slug}" title="Direct link"></a>`
})
} else {
if (clauseAnnotations.length > 0) {
child.children.unshift({
type: 'jsx',
value: `<ClauseAnnotation ${clauseAnnotations.join(" ")} />`
});
}
child.children.unshift({
type: 'html',
value: `<a aria-hidden="true" tabindex="-1" class="anchor anchor-ref" id="${slug}"></a>`,
})
child.children.push({
type: 'html',
value: `<a class="hash-link" href="#${slug}" title="Direct link"></a>`
})
}
processed.push(child);
}
visit(tree, 'list', function (node) {
node.children.forEach(enumerateChild.bind(null, '', false));
return true;
});
// visit(tree, 'listItem', function (node) {
// var id = node.id;
// visit(node, 'html', function (node) {
// var match = node.value.match(/<a\s*name='([^']+)'>/);
// if (match) {
// anchors[match[1]] = id;
// }
// });
// });
// return map(tree, (node) => {
// if (!is(node, 'listItem')) return node
// if (node.children && is(node.children[0], 'paragraph')) {
// node.children[0] = processListItem(node.children[0], links);
// return node;
// }
// return processListItem(
// node,
// links
// )
// })
}
}
function processListItem(
node,
id,
links
) {
const slug = generateSlug(id, links)
node.children.unshift({
type: 'html',
value: `<a aria-hidden="true" tabindex="-1" class="anchor anchor-ref" id="${slug}"></a>`,
})
node.children.push({
type: 'html',
value: `<a class="hash-link" href="#${slug}" title="Direct link"></a>`
})
return node
}