@bartificer/linkify 2.3.5 → 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 +119 -56
- package/docs/LinkTemplate.class.mjs.html +120 -36
- package/docs/Linkifier.class.mjs.html +141 -66
- package/docs/PageData.class.mjs.html +145 -72
- package/docs/defaults.mjs.html +136 -12
- package/docs/docdash-overrides.css +11 -0
- package/docs/externals.jsdoc.html +95 -4
- package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
- package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
- package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
- package/docs/global.html +1276 -0
- package/docs/index.html +1018 -26
- package/docs/index.js.html +143 -17
- package/docs/mermaid-init.js +21 -0
- package/docs/module-cheerio.html +193 -3
- package/docs/module-defaults.html +680 -3
- package/docs/module-link-data.LinkData.html +813 -0
- package/docs/module-link-data.html +226 -0
- package/docs/module-link-template.LinkTemplate.html +792 -0
- package/docs/module-link-template.html +226 -0
- package/docs/module-linkifier.Linkifier.html +2050 -0
- package/docs/module-linkifier.html +236 -0
- package/docs/module-linkify.html +810 -3
- package/docs/module-mustache.html +193 -3
- package/docs/module-node-fetch.html +193 -3
- package/docs/module-page-data.PageData.html +1810 -0
- package/docs/module-page-data.html +226 -0
- package/docs/module-title-case.html +193 -3
- package/docs/module-urijs.html +193 -3
- package/docs/module-url-slug.html +193 -3
- package/docs/module-utilities.html +1518 -3
- package/docs/scripts/collapse.js +39 -0
- package/docs/scripts/commonNav.js +28 -0
- package/docs/scripts/linenumber.js +25 -0
- package/docs/scripts/nav.js +12 -0
- package/docs/scripts/polyfill.js +4 -0
- package/docs/scripts/{third-party → prettify}/Apache-License-2.0.txt +202 -202
- package/docs/scripts/prettify/lang-css.js +2 -0
- package/docs/scripts/prettify/prettify.js +28 -0
- package/docs/scripts/search.js +99 -265
- package/docs/styles/jsdoc.css +776 -0
- package/docs/styles/prettify.css +80 -0
- package/docs/typedefs.jsdoc.html +145 -0
- package/docs/utilities.mjs.html +154 -22
- package/examples/clipboardURLToMarkdown.mjs +80 -1
- package/package.json +5 -3
- package/src/LinkData.class.mjs +24 -52
- package/src/LinkTemplate.class.mjs +25 -32
- package/src/Linkifier.class.mjs +46 -62
- package/src/PageData.class.mjs +50 -68
- package/src/defaults.mjs +42 -8
- package/src/index.js +48 -13
- package/src/typedefs.jsdoc +52 -0
- package/src/utilities.mjs +59 -18
- package/docs/data/search.json +0 -1
- package/docs/fonts/Inconsolata-Regular.ttf +0 -0
- package/docs/fonts/OpenSans-Regular.ttf +0 -0
- package/docs/fonts/WorkSans-Bold.ttf +0 -0
- package/docs/module-LinkData.LinkData.html +0 -13
- package/docs/module-LinkData.html +0 -3
- package/docs/module-LinkTemplate.LinkTemplate.html +0 -3
- package/docs/module-LinkTemplate.html +0 -3
- package/docs/module-Linkifier.Linkifier.html +0 -3
- package/docs/module-Linkifier.html +0 -3
- package/docs/module-PageData.PageData.html +0 -15
- package/docs/module-PageData.html +0 -3
- package/docs/scripts/core.js +0 -726
- package/docs/scripts/core.min.js +0 -23
- package/docs/scripts/resize.js +0 -90
- package/docs/scripts/search.min.js +0 -6
- package/docs/scripts/third-party/fuse.js +0 -9
- package/docs/scripts/third-party/hljs-line-num-original.js +0 -369
- package/docs/scripts/third-party/hljs-line-num.js +0 -1
- package/docs/scripts/third-party/hljs-original.js +0 -5171
- package/docs/scripts/third-party/hljs.js +0 -1
- package/docs/scripts/third-party/popper.js +0 -5
- package/docs/scripts/third-party/tippy.js +0 -1
- package/docs/scripts/third-party/tocbot.js +0 -672
- package/docs/scripts/third-party/tocbot.min.js +0 -1
- package/docs/styles/clean-jsdoc-theme-base.css +0 -1159
- package/docs/styles/clean-jsdoc-theme-dark.css +0 -412
- package/docs/styles/clean-jsdoc-theme-light.css +0 -482
- package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -30
- package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -1
- package/docs/styles/clean-jsdoc-theme.min.css +0 -1
package/README.md
CHANGED
|
@@ -8,61 +8,161 @@ Pull requests and issues are welcomed, but on the understanding that this is a v
|
|
|
8
8
|
|
|
9
9
|
Given this reality, **do not use this repository for anything mission-critical!**.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
# Intended Purpose & Key Features
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
npm install '@bartificer/linkifier';
|
|
15
|
-
```
|
|
13
|
+
This module's reason for existing is to take a URL, fetch and parse the web page's HTML to extract the page and section titles, pass the extracted data through a transformer function appropriate for the domain to extract the page's title, then render that to a link in any language using a desired template.
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
This is not a module for simply converting a URL to a generic link. What makes it different is:
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
1. Link titles are automatically exctracted from the actual web page using customiusable *data transformers*
|
|
18
|
+
2. Links can be rendered in any language using customisible templates.
|
|
19
|
+
3. Web page processing functions and output templates are associated with domain names in a DNS-aware way
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Data transformers and templates are resolved using the DNS hierarchy. For example, if you register a data transformer for the domain `bartificer.ie`, and then you process a link on the `www.bartificer.ie` domain, the module will first check if there is a transformer registered for `www.bartificer.ie`, if not it will find the one registered for `bartificer.ie` and use that. This means that the data transformer and template registered against the DNS root domain `.` acts as defaults for all domains that don't have their own custom settings.
|
|
22
22
|
|
|
23
|
+
To use this module you will need to write your own NodeJS script that:
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
1. Imports this module
|
|
26
|
+
2. Registers your required templates and data transformers
|
|
27
|
+
3. Invokes the module's link generation function
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
// import the linkify module
|
|
28
|
-
import linkify from '@bartificer/linkify';
|
|
29
|
+
If this is more than you need, this module is not for you!
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
```
|
|
31
|
+
# Instalation & Minimal NodeJS Example
|
|
32
|
+
|
|
33
|
+
Before you begin, make sure you have [the latest LTS version of NodeJS](https://nodejs.org/en/download) installed!
|
|
34
|
+
|
|
35
|
+
Create an empty folder and open a shell in that folder.
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
First, install the module into your folder with the command:
|
|
38
38
|
|
|
39
39
|
```sh
|
|
40
|
-
|
|
40
|
+
npm install '@bartificer/linkifier';
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
The following demo code will generate a link using each of the bundled link templates:
|
|
43
|
+
Test the module is working with a basic script that uses all the defaults and makes no customisations. Create a file named `test.mjs` with the following content:
|
|
46
44
|
|
|
47
45
|
```javascript
|
|
48
|
-
|
|
49
|
-
import linkify from '@bartificer/linkify';
|
|
50
|
-
|
|
51
|
-
// get a list of all registered template names
|
|
52
|
-
const templateNames = linkify.templateNames;
|
|
53
|
-
console.log('registered templates:', templateNames);
|
|
46
|
+
import { linkify } from '@bartificer/linkify';
|
|
54
47
|
|
|
55
|
-
// iterate over each tempalte with an example URL
|
|
56
48
|
(async () => {
|
|
57
|
-
|
|
58
|
-
for(let templateName of templateNames){
|
|
59
|
-
let link = await linkify.generateLink(testURL, templateName);
|
|
60
|
-
console.log(`- ${templateName}: ${link}`);
|
|
61
|
-
}
|
|
49
|
+
console.log(await linkify.generateLink('https://github.com/bartificer'));
|
|
62
50
|
})();
|
|
63
51
|
```
|
|
64
52
|
|
|
65
|
-
|
|
53
|
+
Execute this script with the command:
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
node ./examples/clipboardURLToMarkdown.mjs
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
This should print an HTML link to this module's Git repository.
|
|
60
|
+
|
|
61
|
+
# Customising Link Generation
|
|
62
|
+
|
|
63
|
+
The module is very much intended to be customised, and while the module has been designed to make your customisation code concise and as self-documenting as possible, it's vital to understand the module's process for generating links.
|
|
64
|
+
|
|
65
|
+
## The Link Generation Process
|
|
66
|
+
|
|
67
|
+
The module's `generateLink(url)` function is the primary entry point, and the only required argument is a URL.
|
|
68
|
+
|
|
69
|
+
This URL will be converted to a `PageData` object which capture's the various titles and headings found on the page.
|
|
70
|
+
|
|
71
|
+
Based on the URL's domain, a *data transformer* will be resolved, and that function will convert the `PageData` object to a `LinkData` object containing just the fields needed to render a link.
|
|
72
|
+
|
|
73
|
+
Based on the URL's domain, a `LinkTemplate` will be resolved, and that template will be combined with the `LinkData` object to render the link.
|
|
74
|
+
|
|
75
|
+
```mermaid
|
|
76
|
+
flowchart TD
|
|
77
|
+
classDef inputNode fill:#090,color:#fff;
|
|
78
|
+
URL([fa:fa-circle-down URL]):::inputNode
|
|
79
|
+
TPLNAME([fa:fa-circle-down Template Name — Optional]):::inputNode
|
|
80
|
+
|
|
81
|
+
TPLMAP[(Domain → Default Template Mappings)]
|
|
82
|
+
TPLS[(Registered Tempaltes)]
|
|
83
|
+
TRANSMAP[(Domain → Transformer Mappings)]
|
|
84
|
+
|
|
85
|
+
FETCH{Try Fetch Page}
|
|
86
|
+
HAVETPLNAME{Template Name Specified?}
|
|
87
|
+
|
|
88
|
+
PARSE[fa:fa-gears Parse]
|
|
89
|
+
REVSLUG[fa:fa-gears Reverse Slug]
|
|
90
|
+
RSLVTPLNAME[fa:fa-gears Resolve Template Name]
|
|
91
|
+
RSLVTPL[fa:fa-gears Resolve Template]
|
|
92
|
+
RSLVTRANS[fa:fa-gears Resolve Transformer]
|
|
93
|
+
TRANS[fa:fa-gears Transform]
|
|
94
|
+
RENDER[fa:fa-gears Render]
|
|
95
|
+
|
|
96
|
+
PDATA{{fa:fa-table-cells PageData Object}}
|
|
97
|
+
LDATA{{fa:fa-table-cells LinkData Object}}
|
|
98
|
+
TPLNAMESTR{{fa:fa-quote-left Template Name}}
|
|
99
|
+
TPLOBJ{{fa:fa-table-cells Template Object}}
|
|
100
|
+
TRANSFN{{fa:fa-code Transformer Function}}
|
|
101
|
+
|
|
102
|
+
classDef outputNode fill:#090,color:#fff;
|
|
103
|
+
LINK([fa:fa-quote-left Link]):::outputNode
|
|
104
|
+
|
|
105
|
+
URL --> FETCH
|
|
106
|
+
TPLNAME --> HAVETPLNAME
|
|
107
|
+
FETCH -->|Success| PARSE
|
|
108
|
+
FETCH -->|Fail| REVSLUG
|
|
109
|
+
PARSE --> PDATA
|
|
110
|
+
REVSLUG --> PDATA
|
|
111
|
+
HAVETPLNAME -->|Yes| TPLNAMESTR
|
|
112
|
+
HAVETPLNAME -->|No| RSLVTPLNAME
|
|
113
|
+
TPLMAP --> RSLVTPLNAME
|
|
114
|
+
RSLVTPLNAME --> TPLNAMESTR
|
|
115
|
+
TPLNAMESTR --> RSLVTPL
|
|
116
|
+
RSLVTPL --> TPLOBJ
|
|
117
|
+
TPLS --> RSLVTPL
|
|
118
|
+
TRANSMAP --> RSLVTRANS
|
|
119
|
+
RSLVTRANS --> TRANSFN
|
|
120
|
+
PDATA --> TRANS
|
|
121
|
+
TRANSFN --> TRANS
|
|
122
|
+
TRANS --> LDATA
|
|
123
|
+
LDATA --> RENDER
|
|
124
|
+
TPLOBJ --> RENDER
|
|
125
|
+
RENDER --> LINK
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Customisation Points
|
|
129
|
+
|
|
130
|
+
To customise the module effectively you'll need the API documentation — [bartificer.github.io/linkify](https://bartificer.github.io/linkify/).
|
|
131
|
+
|
|
132
|
+
Before rendering links, you should do the following:
|
|
133
|
+
|
|
134
|
+
1. Use an expanded import with at least the following:
|
|
135
|
+
```javascript
|
|
136
|
+
import {linkify, LinkData, LinkTemplate} from '@bartificer/linkify';
|
|
137
|
+
```
|
|
138
|
+
2. If none of the out-of-the-box templates are appropriate (`linkify.templateNames` is the array of registered template names), register a custom template of you own and make it the defaukt. For example:
|
|
139
|
+
```javascript
|
|
140
|
+
// register a template for Markdown links with an emoji pre-fixed
|
|
141
|
+
linkify.registerTemplate('md-emoji', '🔗 [{{{title}}}]({{{url}}})');
|
|
142
|
+
|
|
143
|
+
// make the new template the default for all domains
|
|
144
|
+
linkfiy.registerDefaultTemplateMapping('.', 'md-emoji');
|
|
145
|
+
```
|
|
146
|
+
3. If the default data transformer's logic does not fit with your needs, register a new default. For example:
|
|
147
|
+
```javascript
|
|
148
|
+
linkify.registerTransformer('.', (pData) => { new LinkData(pData.url, pData.title.replace(/ · GitHub$/, ' on GitHub')) });
|
|
149
|
+
```
|
|
150
|
+
4. Register all needed domain-specific custom transformers. For Example:
|
|
151
|
+
```javascript
|
|
152
|
+
linkify.RegisterTransformer('some.domain', (pData) => { new LinkData(pData.url, pData.h1s[1]) });
|
|
153
|
+
```
|
|
154
|
+
5. Very rarely, a different tempalte is required for a given domain, in that case, assign the desirec tempalte at the domain level. For example:
|
|
155
|
+
```javascript
|
|
156
|
+
// create a special template for your home domain
|
|
157
|
+
linkify.registerTemplate('md-home', '🏠 [{{{title}}}]({{{url}}})');
|
|
158
|
+
|
|
159
|
+
// set that template as the default for just your domain (and its subdomains)
|
|
160
|
+
linkfiy.registerDefaultTemplateMapping('your.home.domain', 'md-home');
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
# Real-World Examples
|
|
164
|
+
|
|
165
|
+
_**Note:** these examples are written to work on a development clone of the module, to use them outside of that, change the import statements to import from `'@bartificer/linkify'` rather than `'../dist.index.js'`. Alternatively, fork, download and build the module (`npm ci && npm run build`)._
|
|
66
166
|
|
|
67
167
|
Two real-world scripts Bart users to build shownotes are included in [the `/example` folder in the GitHub repostitory](https://github.com/bartificer/linkify/tree/master/example):
|
|
68
168
|
|