@neo4j-antora/mark-terms 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -0
- package/mark-terms.js +101 -0
- package/package.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# mark-terms
|
|
2
|
+
|
|
3
|
+
Marks the first usage of a term or terms on a page by appending asciidoc after the term. Typically, this is used to add a registered trademark.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Add the extension to a playbook:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
asciidoc:
|
|
11
|
+
extensions:
|
|
12
|
+
- ./extensions/mark-terms/mark-terms.js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Add a comma-separated list of terms to be marked:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
asciidoc:
|
|
19
|
+
attributes:
|
|
20
|
+
page-terms-to-mark: Term1, Term2
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Optionally, add the asciidoc to be appended to the first instance of the term:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
asciidoc:
|
|
27
|
+
attributes:
|
|
28
|
+
page-terms-marker: ©
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
If you don't add `page-terms-marker`, the default value is used. The default value is `®`
|
|
32
|
+
|
|
33
|
+
## Limitationa and known issues
|
|
34
|
+
|
|
35
|
+
The extension reads the document blocks and matches
|
package/mark-terms.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
module.exports = function (registry) {
|
|
2
|
+
registry.treeProcessor(function () {
|
|
3
|
+
var self = this
|
|
4
|
+
self.process(function(doc) {
|
|
5
|
+
|
|
6
|
+
if (!doc.getAttribute('page-terms-to-mark')) return
|
|
7
|
+
|
|
8
|
+
let terms = doc.getAttribute('page-terms-to-mark').split(',').map(function (value) {
|
|
9
|
+
return value.trim();
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
let devMode = doc.getAttribute('page-terms-dev-mode')
|
|
13
|
+
|
|
14
|
+
if (!terms) return
|
|
15
|
+
|
|
16
|
+
let marker = doc.getAttribute('page-terms-marker') || '^®^'
|
|
17
|
+
|
|
18
|
+
let markAdded = []
|
|
19
|
+
|
|
20
|
+
terms.forEach(term => {
|
|
21
|
+
|
|
22
|
+
// experimental regex with negative lookbehind and lookahead DO NOT USE
|
|
23
|
+
// let re = new RegExp(`(?<!\w+:)${term}(?!\\[\.*\])`)
|
|
24
|
+
|
|
25
|
+
// terms should appear:
|
|
26
|
+
// - as a word at the beginning of a line
|
|
27
|
+
// - as a word after a space
|
|
28
|
+
// - after a [ if they are the start of the text output of an inline macro
|
|
29
|
+
|
|
30
|
+
// let re = new RegExp(`(^|\\[|\s)${term}`)
|
|
31
|
+
|
|
32
|
+
let re = new RegExp(`(^|\\[|\\s)${term}\\b`)
|
|
33
|
+
|
|
34
|
+
let reMarked = new RegExp(`${term} ${marker}`)
|
|
35
|
+
|
|
36
|
+
doc.findBy().forEach(block => {
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
// if we've already marked ths, don't mark it again
|
|
40
|
+
// unless testing in dev mode
|
|
41
|
+
if ( markAdded.includes(term) && !devMode) return
|
|
42
|
+
|
|
43
|
+
// ignore listing blocks (which includes source blocks) and literal blocks
|
|
44
|
+
if (block.getContext() === 'listing' || block.getContext() === 'literal') return
|
|
45
|
+
|
|
46
|
+
// heading?
|
|
47
|
+
if (block.getContext() === 'section') {
|
|
48
|
+
let reggedTitle = testLine(block.getName())
|
|
49
|
+
block.setTitle(reggedTitle)
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// tables aren't blocks with lines
|
|
54
|
+
// table cells can be checked for their text
|
|
55
|
+
if (block.getContext() === 'table_cell') {
|
|
56
|
+
let reggedText = testLine(block.text)
|
|
57
|
+
block.text = reggedText
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// if the block contains no lines, return
|
|
62
|
+
if ( !block.lines) return
|
|
63
|
+
|
|
64
|
+
// test each line
|
|
65
|
+
block.lines.forEach((line, i) => {
|
|
66
|
+
let reggedLine = testLine(line)
|
|
67
|
+
block.lines[i] = reggedLine
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// test a line of content from a block or a table cell
|
|
73
|
+
function testLine(line) {
|
|
74
|
+
|
|
75
|
+
// return if we've already marked this term
|
|
76
|
+
if (markAdded.includes(term) && !devMode) return line
|
|
77
|
+
|
|
78
|
+
// return if this term is already marked
|
|
79
|
+
if (reMarked.test(line)) {
|
|
80
|
+
markAdded.push(term)
|
|
81
|
+
return line
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// mark the first instance of the term if we find a match
|
|
85
|
+
if (re.test(line)) {
|
|
86
|
+
markAdded.push(term)
|
|
87
|
+
return line.replace(re, '$1' + term + marker)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// we checked but there was no match
|
|
91
|
+
return line
|
|
92
|
+
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@neo4j-antora/mark-terms",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Asciidoc extension to mark the first instance of a term on a page, typically with a copyright symbol",
|
|
5
|
+
"main": "mark-terms.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/neo4j/docs-asciidoc-extensions.git"
|
|
12
|
+
},
|
|
13
|
+
"author": "Neo4j Documentation <docs@neo4j,com>",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/neo4j/docs-asciidoc-extensions/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/neo4j/docs-asciidoc-extensions#readme"
|
|
19
|
+
}
|