@bartificer/linkify 2.4.0 → 2.4.1
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 +135 -35
- package/dist/index.js +1 -1
- package/docs/LinkData.class.mjs.html +28 -53
- package/docs/LinkTemplate.class.mjs.html +29 -33
- package/docs/Linkifier.class.mjs.html +46 -59
- package/docs/PageData.class.mjs.html +54 -69
- package/docs/defaults.mjs.html +22 -2
- package/docs/docdash-overrides.css +9 -3
- package/docs/externals.jsdoc.html +6 -2
- package/docs/global.html +1276 -0
- package/docs/index.html +114 -45
- package/docs/index.js.html +7 -3
- package/docs/mermaid-init.js +21 -0
- package/docs/module-cheerio.html +6 -2
- package/docs/module-defaults.html +100 -5
- package/docs/module-link-data.LinkData.html +81 -212
- package/docs/module-link-data.html +8 -4
- package/docs/module-link-template.LinkTemplate.html +71 -96
- package/docs/module-link-template.html +8 -4
- package/docs/module-linkifier.Linkifier.html +103 -376
- package/docs/module-linkifier.html +6 -2
- package/docs/module-linkify.html +7 -3
- package/docs/module-mustache.html +6 -2
- package/docs/module-node-fetch.html +6 -2
- package/docs/module-page-data.PageData.html +224 -202
- package/docs/module-page-data.html +8 -4
- package/docs/module-title-case.html +6 -2
- package/docs/module-urijs.html +6 -2
- package/docs/module-url-slug.html +6 -2
- package/docs/module-utilities.html +74 -24
- package/docs/typedefs.jsdoc.html +145 -0
- package/docs/utilities.mjs.html +36 -23
- package/package.json +3 -2
- package/src/LinkData.class.mjs +22 -51
- package/src/LinkTemplate.class.mjs +23 -31
- package/src/Linkifier.class.mjs +40 -57
- package/src/PageData.class.mjs +48 -67
- package/src/defaults.mjs +16 -0
- package/src/index.js +1 -1
- package/src/typedefs.jsdoc +52 -0
- package/src/utilities.mjs +30 -21
package/src/Linkifier.class.mjs
CHANGED
|
@@ -28,28 +28,26 @@ import Mustache from 'mustache';
|
|
|
28
28
|
* The class providing the link rendering functionality. Instances of this class capture the settings for generating links, and, generate links using these settings.
|
|
29
29
|
*/
|
|
30
30
|
export class Linkifier {
|
|
31
|
+
/**
|
|
32
|
+
* Builds a Linkifier instance ready for use rendering links using the default configration.
|
|
33
|
+
* @see {@link module:defaults} for the default configuration settings.
|
|
34
|
+
*/
|
|
31
35
|
constructor(){
|
|
32
36
|
/**
|
|
33
|
-
* A mapping of domain names to data transformation functions.
|
|
37
|
+
* A mapping of fully qualified domain names to data transformation functions.
|
|
34
38
|
*
|
|
35
39
|
* @private
|
|
36
|
-
* @type {Object.<
|
|
40
|
+
* @type {Object.<string, dataTransformer>}
|
|
37
41
|
*/
|
|
38
42
|
this._pageDataToLinkDataTransmformers = {
|
|
39
|
-
'.' :
|
|
40
|
-
let text = pData.title;
|
|
41
|
-
if(pData.h1s.length === 1){
|
|
42
|
-
text = pData.mainHeading;
|
|
43
|
-
}
|
|
44
|
-
return new LinkData(pData.url, text);
|
|
45
|
-
}
|
|
43
|
+
'.' : defaults.dataTransformer
|
|
46
44
|
};
|
|
47
45
|
|
|
48
46
|
/**
|
|
49
|
-
* A mapping of
|
|
47
|
+
* A mapping of fully qualified domain names to default template names.
|
|
50
48
|
*
|
|
51
49
|
* @private
|
|
52
|
-
* @type {Object.<
|
|
50
|
+
* @type {Object.<string, string>}
|
|
53
51
|
*/
|
|
54
52
|
this._pageDataToLinkTemplateName = {
|
|
55
53
|
'.' : 'html' // default to the 'html' template for all domains unless otherwise specified
|
|
@@ -59,7 +57,7 @@ export class Linkifier {
|
|
|
59
57
|
* The registered link templates.
|
|
60
58
|
*
|
|
61
59
|
* @private
|
|
62
|
-
* @type {Object.<
|
|
60
|
+
* @type {Object.<string, module:link-template.LinkTemplate>}
|
|
63
61
|
*/
|
|
64
62
|
this._linkTemplates = {};
|
|
65
63
|
|
|
@@ -90,30 +88,30 @@ export class Linkifier {
|
|
|
90
88
|
|
|
91
89
|
/**
|
|
92
90
|
* @type {Object.<string, Function>}
|
|
91
|
+
* @readonly
|
|
92
|
+
* @see {@link module:utilities} for the utility functions available in this collection.
|
|
93
93
|
*/
|
|
94
94
|
get utilities() {
|
|
95
95
|
return this._utilities;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
99
|
-
*
|
|
99
|
+
* Shorthand property for `.utilities`.
|
|
100
|
+
* @see {@link module:linkifier.Linkifier#utilities}
|
|
100
101
|
*/
|
|
101
102
|
get util(){
|
|
102
103
|
return this._utilities;
|
|
103
104
|
}
|
|
104
105
|
|
|
105
106
|
/**
|
|
106
|
-
*
|
|
107
|
+
* The list of known words with special capitalisations. The words should be capitalised in the descired manner.
|
|
108
|
+
* @type {string[]}
|
|
107
109
|
*/
|
|
108
110
|
get speciallyCapitalisedWords(){
|
|
109
111
|
const ans = [];
|
|
110
112
|
this._speciallyCapitalisedWords.map(word => ans.push(word));
|
|
111
113
|
return ans;
|
|
112
114
|
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* @param {string[]} words - a list of words with special capitalisations
|
|
116
|
-
*/
|
|
117
115
|
set speciallyCapitalisedWords(words){
|
|
118
116
|
// TO DO - add validation
|
|
119
117
|
|
|
@@ -121,13 +119,11 @@ export class Linkifier {
|
|
|
121
119
|
}
|
|
122
120
|
|
|
123
121
|
/**
|
|
124
|
-
* Register a data transformer function
|
|
122
|
+
* Register a data transformer function to a domain name.
|
|
125
123
|
*
|
|
126
|
-
* @param {
|
|
124
|
+
* @param {string} domain - The fully qualified domain for which this transformer should be
|
|
127
125
|
* used.
|
|
128
126
|
* @param {dataTransformer} transformerFn - The data transformer callback.
|
|
129
|
-
* @throws {ValidationError} A validation error is thrown if either parameter
|
|
130
|
-
* is missing or invalid.
|
|
131
127
|
*/
|
|
132
128
|
registerTransformer(domain, transformerFn){
|
|
133
129
|
// TO DO - add validation
|
|
@@ -143,16 +139,14 @@ export class Linkifier {
|
|
|
143
139
|
* Get the data transformer function for a given domain.
|
|
144
140
|
*
|
|
145
141
|
* Note that domains are searched from the subdomain up. For example, if passed
|
|
146
|
-
* the domain `www.bartificer.
|
|
147
|
-
* transformer for the domain `www.bartificer.
|
|
142
|
+
* the domain `www.bartificer.ie` the function will first look for a
|
|
143
|
+
* transformer for the domain `www.bartificer.ie`, if there's no transformer
|
|
148
144
|
* registered for that domain it will look for a transformer for the domain
|
|
149
|
-
* `bartificer.
|
|
145
|
+
* `bartificer.ie`, if there's no transformer for that domain either it will
|
|
150
146
|
* return the default transformer.
|
|
151
147
|
*
|
|
152
|
-
* @param {
|
|
148
|
+
* @param {string} domain - The fully qualified domain for which to get the data transformer.
|
|
153
149
|
* @returns {dataTransformer}
|
|
154
|
-
* @throws {ValidationError} A validation error is thrown unless a valid domain
|
|
155
|
-
* name is passed.
|
|
156
150
|
*/
|
|
157
151
|
getTransformerForDomain(domain){
|
|
158
152
|
// TO DO - add validation
|
|
@@ -178,22 +172,20 @@ export class Linkifier {
|
|
|
178
172
|
/**
|
|
179
173
|
* A list of the names of the registered link templates.
|
|
180
174
|
* @type {string[]}
|
|
175
|
+
* @readonly
|
|
181
176
|
*/
|
|
182
177
|
get templateNames() {
|
|
183
178
|
return Object.keys(this._linkTemplates);
|
|
184
179
|
}
|
|
185
180
|
|
|
186
181
|
/**
|
|
187
|
-
*
|
|
182
|
+
* The name of the default template used when rendering links.
|
|
183
|
+
* @type {string}
|
|
184
|
+
* @throws {ValidationError} A validation error is thrown if the template name is missing, invalid, or doesn't correspond to a registered template.
|
|
188
185
|
*/
|
|
189
186
|
get defaultTemplateName(){
|
|
190
187
|
return this._pageDataToLinkTemplateName['.'];
|
|
191
188
|
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* @param {string} templateName - The name of the default template to use.
|
|
195
|
-
* @throws {ValidationError} A validation error is thrown if the template name is missing, invalid, or doesn't correspond to a registered template.
|
|
196
|
-
*/
|
|
197
189
|
set defaultTemplateName(templateName){
|
|
198
190
|
const tplName = String(templateName);
|
|
199
191
|
if(!this._linkTemplates[tplName]){
|
|
@@ -204,7 +196,8 @@ export class Linkifier {
|
|
|
204
196
|
|
|
205
197
|
/**
|
|
206
198
|
* The default link template.
|
|
207
|
-
* @type {module:
|
|
199
|
+
* @type {module:link-template.LinkTemplate}
|
|
200
|
+
* @readonly
|
|
208
201
|
*/
|
|
209
202
|
get defaultTemplate(){
|
|
210
203
|
return this._linkTemplates[this._pageDataToLinkTemplateName['.']];
|
|
@@ -214,9 +207,7 @@ export class Linkifier {
|
|
|
214
207
|
* Register a link template.
|
|
215
208
|
*
|
|
216
209
|
* @param {string} name
|
|
217
|
-
* @param {LinkTemplate} template
|
|
218
|
-
* @throws {ValidationError} A validation error is thrown unless both a valid
|
|
219
|
-
* name and template object are passed.
|
|
210
|
+
* @param {module:link-template.LinkTemplate} template
|
|
220
211
|
*/
|
|
221
212
|
registerTemplate(name, template){
|
|
222
213
|
// TO DO - add validation
|
|
@@ -229,7 +220,7 @@ export class Linkifier {
|
|
|
229
220
|
* Get a registered link template by name.
|
|
230
221
|
*
|
|
231
222
|
* @param {string} templateName
|
|
232
|
-
* @returns {LinkTemplate}
|
|
223
|
+
* @returns {module:link-template.LinkTemplate}
|
|
233
224
|
* @throws {ValidationError} A validation error is thrown unless a valid name is passed and corresponds to a registered template.
|
|
234
225
|
*/
|
|
235
226
|
getTemplate(templateName){
|
|
@@ -245,10 +236,8 @@ export class Linkifier {
|
|
|
245
236
|
* Register a default template for use with a given domain. This template will
|
|
246
237
|
* override the overall default for this domain and all its subdomains.
|
|
247
238
|
*
|
|
248
|
-
* @param {
|
|
249
|
-
* @param {
|
|
250
|
-
* @throws {ValidationError} A validation error is thrown if either parameter
|
|
251
|
-
* is missing or invalid.
|
|
239
|
+
* @param {string} domain - The fully qualified domain name for which this template should be used by default.
|
|
240
|
+
* @param {string} templateName - The name of the template to use.
|
|
252
241
|
*/
|
|
253
242
|
registerDefaultTemplateMapping(domain, templateName){
|
|
254
243
|
// TO DO - add validation
|
|
@@ -264,16 +253,14 @@ export class Linkifier {
|
|
|
264
253
|
* Get the data transformer function for a given domain.
|
|
265
254
|
*
|
|
266
255
|
* Note that domains are searched from the subdomain up. For example, if passed
|
|
267
|
-
* the domain `www.bartificer.
|
|
268
|
-
* transformer for the domain `www.bartificer.
|
|
256
|
+
* the domain `www.bartificer.ie` the function will first look for a
|
|
257
|
+
* transformer for the domain `www.bartificer.ie`, if there's no transformer
|
|
269
258
|
* registered for that domain it will look for a transformer for the domain
|
|
270
|
-
* `bartificer.
|
|
259
|
+
* `bartificer.ie`, if there's no transformer for that domain either it will
|
|
271
260
|
* return the default transformer.
|
|
272
261
|
*
|
|
273
|
-
* @param {
|
|
262
|
+
* @param {string} domain - The fully qualified domain name to get the data transformer for.
|
|
274
263
|
* @returns {dataTransformer}
|
|
275
|
-
* @throws {ValidationError} A validation error is thrown unless a valid domain
|
|
276
|
-
* name is passed.
|
|
277
264
|
*/
|
|
278
265
|
getTemplateNameForDomain(domain){
|
|
279
266
|
// TO DO - add validation
|
|
@@ -308,10 +295,8 @@ export class Linkifier {
|
|
|
308
295
|
* Fetch the page data for a given URL.
|
|
309
296
|
*
|
|
310
297
|
* @async
|
|
311
|
-
* @param {
|
|
312
|
-
* @returns {PageData}
|
|
313
|
-
* @throws {ValidationError} A validation error is thrown unless a valid URL is
|
|
314
|
-
* passed.
|
|
298
|
+
* @param {string} url
|
|
299
|
+
* @returns {module:page-data.PageData}
|
|
315
300
|
*/
|
|
316
301
|
async fetchPageData(url){
|
|
317
302
|
// TO DO - add validation
|
|
@@ -351,11 +336,9 @@ export class Linkifier {
|
|
|
351
336
|
* default will be used (`html`).
|
|
352
337
|
*
|
|
353
338
|
* @async
|
|
354
|
-
* @param {
|
|
355
|
-
* @param {
|
|
339
|
+
* @param {string} url
|
|
340
|
+
* @param {string} [templateName='html']
|
|
356
341
|
* @returns {string}
|
|
357
|
-
* @throws {ValidationError} A validation error is thrown unless a valid URL is
|
|
358
|
-
* passed.
|
|
359
342
|
*/
|
|
360
343
|
async generateLink(url, templateName){
|
|
361
344
|
// TO DO - add validation
|
package/src/PageData.class.mjs
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @file
|
|
2
|
+
* @file Data model for web page information.
|
|
3
3
|
* @author Bart Busschots <opensource@bartificer.ie>
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* This module provides
|
|
8
|
+
* This module provides the class for representing the information that is extracted from web pages.
|
|
9
9
|
* @module page-data
|
|
10
10
|
* @requires module:urijs
|
|
11
11
|
*/
|
|
12
12
|
import {default as URI} from 'urijs';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* The information extracted from web pages that can be used to render a link.
|
|
16
|
+
*
|
|
17
|
+
* Instances of this class are created from the information extracted from web pages and converted to link information by data transformers before being rendered to links via templates.
|
|
18
|
+
* @see {@link dataTransformer} for details of how instances of this class are used in the link generation process.
|
|
16
19
|
*/
|
|
17
20
|
export class PageData {
|
|
18
21
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @param {URL} url - The page's full URL.
|
|
22
|
-
* @throws {ValidationError} A validation error is thrown if an invalid URL
|
|
23
|
-
* is passed.
|
|
22
|
+
* @param {string} url - The page's full URL.
|
|
24
23
|
*/
|
|
25
24
|
constructor(url){
|
|
26
25
|
// TO DO - add validation
|
|
@@ -29,7 +28,7 @@ export class PageData {
|
|
|
29
28
|
* The page's URL as a URI object.
|
|
30
29
|
*
|
|
31
30
|
* @private
|
|
32
|
-
* @type {
|
|
31
|
+
* @type {module:urijs}
|
|
33
32
|
*/
|
|
34
33
|
this._uri = URI();
|
|
35
34
|
|
|
@@ -46,74 +45,73 @@ export class PageData {
|
|
|
46
45
|
* `h1` and `h2`.
|
|
47
46
|
*
|
|
48
47
|
* @private
|
|
49
|
-
* @type {
|
|
48
|
+
* @type {Object}
|
|
49
|
+
* @property {string[]} h1 - The page's top-level headings (`h1` tags).
|
|
50
|
+
* @property {string[]} h2 - The page's secondary headings (`h2` tags).
|
|
50
51
|
*/
|
|
51
52
|
this._headings = {
|
|
52
53
|
h1: [],
|
|
53
54
|
h2: []
|
|
54
55
|
};
|
|
55
56
|
|
|
56
|
-
// store the URL
|
|
57
|
+
// store the URL using the public setter to ensure it's stored as a URI object
|
|
57
58
|
this.url = url;
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
/**
|
|
61
|
-
* @
|
|
62
|
+
* @type {string}
|
|
63
|
+
* @throws {TypeError} on invalid URLs.
|
|
62
64
|
*/
|
|
63
65
|
get url(){
|
|
64
66
|
return this._uri.toString();
|
|
65
67
|
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @param {string} url - A URL as a string.
|
|
69
|
-
* @throws {ValidationError} A validation error is thrown if an argument
|
|
70
|
-
* is passed that's not a valid URL string.
|
|
71
|
-
*/
|
|
72
68
|
set url(url){
|
|
73
69
|
this._uri = URI(url).normalize();
|
|
74
70
|
}
|
|
75
71
|
|
|
76
72
|
/**
|
|
77
|
-
* @
|
|
73
|
+
* @type {module:urijs}
|
|
74
|
+
* @readonly
|
|
78
75
|
*/
|
|
79
76
|
get uri(){
|
|
80
77
|
return this._uri.clone();
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* @
|
|
81
|
+
* The domain-part of the URL.
|
|
82
|
+
* @type {string}
|
|
83
|
+
* @readonly
|
|
87
84
|
*/
|
|
88
85
|
get domain(){
|
|
89
86
|
return this._uri.hostname();
|
|
90
87
|
}
|
|
91
88
|
|
|
92
89
|
/**
|
|
93
|
-
*
|
|
90
|
+
* The path-part of the URL.
|
|
91
|
+
* @type {string}
|
|
92
|
+
* @readonly
|
|
94
93
|
*/
|
|
95
94
|
get path(){
|
|
96
95
|
return this._uri.path();
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
/**
|
|
100
|
-
*
|
|
99
|
+
* The page's title. Values are coerced to strings with `String(title)`.
|
|
100
|
+
* @type {string}
|
|
101
101
|
*/
|
|
102
102
|
get title(){
|
|
103
103
|
return this._title;
|
|
104
104
|
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* @param {string} title - the page's title as a string. Values passed will be coerced to strings.
|
|
108
|
-
*/
|
|
109
105
|
set title(title){
|
|
110
106
|
this._title = String(title);
|
|
111
107
|
}
|
|
112
108
|
|
|
113
109
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* @
|
|
110
|
+
* The page's primary and secondary headings.
|
|
111
|
+
* @type {Object}
|
|
112
|
+
* @property {string[]} h1 - The page's top-level headings (`h1` tags).
|
|
113
|
+
* @property {string[]} h2 - The page's secondary headings (`h2` tags).
|
|
114
|
+
* @readonly
|
|
117
115
|
*/
|
|
118
116
|
get headings(){
|
|
119
117
|
let ans = {
|
|
@@ -131,8 +129,8 @@ export class PageData {
|
|
|
131
129
|
|
|
132
130
|
/**
|
|
133
131
|
* The page's top-level headings (`h1` tags).
|
|
134
|
-
*
|
|
135
|
-
* @
|
|
132
|
+
* @type {string[]}
|
|
133
|
+
* @readonly
|
|
136
134
|
*/
|
|
137
135
|
get topLevelHeadings(){
|
|
138
136
|
var ans = [];
|
|
@@ -144,7 +142,8 @@ export class PageData {
|
|
|
144
142
|
|
|
145
143
|
/**
|
|
146
144
|
* An alias for `.topLevelHeadings`.
|
|
147
|
-
* @
|
|
145
|
+
* @readonly
|
|
146
|
+
* @see {@link module:page-data.PageData#topLevelHeadings}
|
|
148
147
|
*/
|
|
149
148
|
get h1s(){
|
|
150
149
|
return this.topLevelHeadings;
|
|
@@ -152,8 +151,8 @@ export class PageData {
|
|
|
152
151
|
|
|
153
152
|
/**
|
|
154
153
|
* The page's secondary headings (`h2` tags).
|
|
155
|
-
*
|
|
156
|
-
* @
|
|
154
|
+
* @type {string[]}
|
|
155
|
+
* @readonly
|
|
157
156
|
*/
|
|
158
157
|
get secondaryHeadings(){
|
|
159
158
|
var ans = [];
|
|
@@ -165,8 +164,9 @@ export class PageData {
|
|
|
165
164
|
|
|
166
165
|
/**
|
|
167
166
|
* An alias for `.secondaryHeadings`.
|
|
168
|
-
* @
|
|
169
|
-
|
|
167
|
+
* @readonly
|
|
168
|
+
* @see {@link module:page-data.PageData#secondaryHeadings}
|
|
169
|
+
*/
|
|
170
170
|
get h2s(){
|
|
171
171
|
return this.secondaryHeadings;
|
|
172
172
|
}
|
|
@@ -176,8 +176,8 @@ export class PageData {
|
|
|
176
176
|
* has `h1` tags, the first one will be used, if not, the first `h2` tag
|
|
177
177
|
* will be used, and if there's none of those either, an empty string will
|
|
178
178
|
* be returned.
|
|
179
|
-
*
|
|
180
|
-
* @
|
|
179
|
+
* @type {string}
|
|
180
|
+
* @readonly
|
|
181
181
|
*/
|
|
182
182
|
get mainHeading(){
|
|
183
183
|
if(this._headings.h1.length > 0){
|
|
@@ -193,7 +193,7 @@ export class PageData {
|
|
|
193
193
|
* Add a top-level heading.
|
|
194
194
|
*
|
|
195
195
|
* @param {string} h1Text
|
|
196
|
-
* @returns {PageData} A reference to self to
|
|
196
|
+
* @returns {module:page-data.PageData} A reference to self to
|
|
197
197
|
* facilitate function chaning.
|
|
198
198
|
*/
|
|
199
199
|
addTopLevelHeading(h1Text){
|
|
@@ -206,7 +206,7 @@ export class PageData {
|
|
|
206
206
|
* Add a seconary heading.
|
|
207
207
|
*
|
|
208
208
|
* @param {string} h2Text
|
|
209
|
-
* @returns {PageData} A reference to self to
|
|
209
|
+
* @returns {module:page-data.PageData} A reference to self to
|
|
210
210
|
* facilitate function chaning.
|
|
211
211
|
*/
|
|
212
212
|
addSecondaryHeading(h2Text){
|
|
@@ -216,27 +216,8 @@ export class PageData {
|
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
/**
|
|
219
|
-
* Get the page data as a plain object
|
|
220
|
-
*
|
|
221
|
-
* {
|
|
222
|
-
* url: 'http://www.bartificer.net/',
|
|
223
|
-
* title: 'the page title',
|
|
224
|
-
* topLevelHeadings: [ 'first h1', 'second h1' ],
|
|
225
|
-
* secondaryHeadings: [ 'first h2', 'second h2' ],
|
|
226
|
-
* mainHeading: 'first h1',
|
|
227
|
-
* uri: {
|
|
228
|
-
* hostname: 'www.bartificer.net',
|
|
229
|
-
* path: '/',
|
|
230
|
-
* hasPath: false
|
|
231
|
-
* }
|
|
232
|
-
* }
|
|
233
|
-
* ```
|
|
234
|
-
*
|
|
235
|
-
* Note that the `uri` could contain more fields - it's initialised with
|
|
236
|
-
* output from the `URI.parse()` function from the `URI` module.
|
|
237
|
-
*
|
|
238
|
-
* @returns {Object} A plain object containing the page data.
|
|
239
|
-
* @see {@link https://medialize.github.io/URI.js/docs.html#static-parse}
|
|
219
|
+
* Get the page data as a plain object.
|
|
220
|
+
* @returns {plainPageInformationObject}
|
|
240
221
|
*/
|
|
241
222
|
asPlainObject(){
|
|
242
223
|
let ans = {
|
|
@@ -254,18 +235,18 @@ export class PageData {
|
|
|
254
235
|
|
|
255
236
|
/**
|
|
256
237
|
* A shortcut for `.addTopLevelHeading()`.
|
|
257
|
-
*
|
|
238
|
+
* @name module:page-data.PageData#h1
|
|
258
239
|
* @function
|
|
259
|
-
* @see PageData#addTopLevelHeading
|
|
240
|
+
* @see {@link module:page-data.PageData#addTopLevelHeading}
|
|
260
241
|
*
|
|
261
242
|
*/
|
|
262
243
|
PageData.prototype.h1 = PageData.prototype.addTopLevelHeading;
|
|
263
244
|
|
|
264
245
|
/**
|
|
265
246
|
* A shortcut for `.addSecondaryHeading()`.
|
|
266
|
-
*
|
|
247
|
+
* @name module:page-data.PageData#h2
|
|
267
248
|
* @function
|
|
268
|
-
* @see PageData#addSecondaryHeading
|
|
249
|
+
* @see {@link module:page-data.PageData#addSecondaryHeading}
|
|
269
250
|
*
|
|
270
251
|
*/
|
|
271
252
|
PageData.prototype.h2 = PageData.prototype.addSecondaryHeading;
|
package/src/defaults.mjs
CHANGED
|
@@ -15,6 +15,22 @@
|
|
|
15
15
|
import { LinkTemplate } from './LinkTemplate.class.mjs';
|
|
16
16
|
import * as utilities from "./utilities.mjs";
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* The default link transformer. The Linkifier constructor assigns this data transformer to the root DNS name `.`, ensuring it acts as the fallback when no other domains are matched.
|
|
20
|
+
*
|
|
21
|
+
* If the page has exactly one top-level heading, this heading is used as a the link text, otherwise the page title is used.
|
|
22
|
+
*
|
|
23
|
+
* The description field is not explicitly set, so will default to the link text.
|
|
24
|
+
* @type {dataTransformer}
|
|
25
|
+
*/
|
|
26
|
+
export function dataTransformer(pData){
|
|
27
|
+
let text = pData.title;
|
|
28
|
+
if(pData.h1s.length === 1){
|
|
29
|
+
text = pData.mainHeading;
|
|
30
|
+
}
|
|
31
|
+
return new LinkData(pData.url, text);
|
|
32
|
+
}
|
|
33
|
+
|
|
18
34
|
/**
|
|
19
35
|
* The collection of named link templates loaded by the Linkifier constructor. All templates strip UTM parameters from the URL, and regularise white space in the text and descriptions.
|
|
20
36
|
* @type {Object.<string, module:link-template.LinkTemplate>}
|
package/src/index.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* @requires link-data
|
|
15
15
|
* @requires link-template
|
|
16
16
|
* @requires page-data
|
|
17
|
-
* @see {@link module:linkifier.Linkifier}
|
|
17
|
+
* @see {@link module:linkifier.Linkifier} for the entry point into the link generation functionality.
|
|
18
18
|
* @example <caption>Basic Usage</caption>
|
|
19
19
|
* import { linkify } from '@bartificer/linkify';
|
|
20
20
|
*
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functions for converting information extracted from a web page into the information used to render links.
|
|
3
|
+
* @callback dataTransformer
|
|
4
|
+
* @param {module:page-data.PageData} pData - The web page data to be transrormed to link data.
|
|
5
|
+
* @returns {module:link-data.LinkData} The link information derived from the page data.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Functions for filtering template field values before their use to render links. Arbitrarily many of these functions can be added to a template in order to filter individual fields, or all fields.
|
|
10
|
+
* @callback templateFieldFilterFunction
|
|
11
|
+
* @param {string} originalString - the original value for the field to be filtered.
|
|
12
|
+
* @returns {string} the filtered version of the original string.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A tupple (array of length exactly two) for assigning a field filter to a template.
|
|
17
|
+
* @typedef {Array} templateFieldFilterTuple
|
|
18
|
+
* @property {"all"|"url"|"text"|"description"} 0 - the property the filter should be applied to.
|
|
19
|
+
* @property {templateFieldFilterFunction} 1 - the filter function to apply.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A plain object represeting the information about about link that can get utilised in a template.
|
|
24
|
+
*
|
|
25
|
+
* Note that the `uri` could contain more fields - it's initialised with output from the `URI.parse()` function from the `URI` module.
|
|
26
|
+
* @typedef {Object} plainLinkInformationObject
|
|
27
|
+
* @property {string} url - the URL for the link.
|
|
28
|
+
* @property {string} text - the text for the link.
|
|
29
|
+
* @property {string} description - a description for the link.
|
|
30
|
+
* @property {Object} uri - the URL's components
|
|
31
|
+
* @property {string} url.hostname - the hostname part of the URL.
|
|
32
|
+
* @property {string} url.path - the path part of the UL, `/` for an empty path.
|
|
33
|
+
* @property {boolean} url.hasPath - whether or not the URL has a path, note that `/` is considered no path.
|
|
34
|
+
* @see {@link https://medialize.github.io/URI.js/docs.html#static-parse}
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A plain object representing the infomration extracted from a web page.
|
|
39
|
+
*
|
|
40
|
+
* Note that the `uri` could contain more fields - it's initialised with output from the `URI.parse()` function from the `URI` module.
|
|
41
|
+
* @typedef {Object} plainPageInformationObject
|
|
42
|
+
* @property {string} url - the page's URL.
|
|
43
|
+
* @property {string} title - the page's title.
|
|
44
|
+
* @property {string[]} topLevelHeadings - the text from the page's `h1` tags.
|
|
45
|
+
* @property {string[]} secondaryHeadings - text from the page's `h2` tags.
|
|
46
|
+
* @property {string} mainHeading - the text from the most semantically important heading that exists in the page.
|
|
47
|
+
* @property {Object} uri - the URL's components
|
|
48
|
+
* @property {string} url.hostname - the hostname part of the URL.
|
|
49
|
+
* @property {string} url.path - the path part of the UL, `/` for an empty path.
|
|
50
|
+
* @property {boolean} url.hasPath - whether or not the URL has a path, note that `/` is considered no path.
|
|
51
|
+
* @see {@link https://medialize.github.io/URI.js/docs.html#static-parse}
|
|
52
|
+
*/
|