@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.
Files changed (41) hide show
  1. package/README.md +135 -35
  2. package/dist/index.js +1 -1
  3. package/docs/LinkData.class.mjs.html +28 -53
  4. package/docs/LinkTemplate.class.mjs.html +29 -33
  5. package/docs/Linkifier.class.mjs.html +46 -59
  6. package/docs/PageData.class.mjs.html +54 -69
  7. package/docs/defaults.mjs.html +22 -2
  8. package/docs/docdash-overrides.css +9 -3
  9. package/docs/externals.jsdoc.html +6 -2
  10. package/docs/global.html +1276 -0
  11. package/docs/index.html +114 -45
  12. package/docs/index.js.html +7 -3
  13. package/docs/mermaid-init.js +21 -0
  14. package/docs/module-cheerio.html +6 -2
  15. package/docs/module-defaults.html +100 -5
  16. package/docs/module-link-data.LinkData.html +81 -212
  17. package/docs/module-link-data.html +8 -4
  18. package/docs/module-link-template.LinkTemplate.html +71 -96
  19. package/docs/module-link-template.html +8 -4
  20. package/docs/module-linkifier.Linkifier.html +103 -376
  21. package/docs/module-linkifier.html +6 -2
  22. package/docs/module-linkify.html +7 -3
  23. package/docs/module-mustache.html +6 -2
  24. package/docs/module-node-fetch.html +6 -2
  25. package/docs/module-page-data.PageData.html +224 -202
  26. package/docs/module-page-data.html +8 -4
  27. package/docs/module-title-case.html +6 -2
  28. package/docs/module-urijs.html +6 -2
  29. package/docs/module-url-slug.html +6 -2
  30. package/docs/module-utilities.html +74 -24
  31. package/docs/typedefs.jsdoc.html +145 -0
  32. package/docs/utilities.mjs.html +36 -23
  33. package/package.json +3 -2
  34. package/src/LinkData.class.mjs +22 -51
  35. package/src/LinkTemplate.class.mjs +23 -31
  36. package/src/Linkifier.class.mjs +40 -57
  37. package/src/PageData.class.mjs +48 -67
  38. package/src/defaults.mjs +16 -0
  39. package/src/index.js +1 -1
  40. package/src/typedefs.jsdoc +52 -0
  41. package/src/utilities.mjs +30 -21
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
- ## Instalation
11
+ # Intended Purpose & Key Features
12
12
 
13
- ```sh
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
- ## Examples
15
+ This is not a module for simply converting a URL to a generic link. What makes it different is:
18
16
 
19
- This is an ES6 module, so when used with NPM, your scripts must have the `.mjs` file extension (to force ES module mode).
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
- ### Generate a Link from a URL Using all the Defaults
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
- Create a file named `demo.mjs` with the following content:
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
- ```javascript
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
- // generate a link from a URL using the default template
31
- (async () => {
32
- let link = await linkify.generateLink('https://pbs.bartificer.net/pbs128');
33
- console.log(link);
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
- Execute the script with `node`:
37
+ First, install the module into your folder with the command:
38
38
 
39
39
  ```sh
40
- node demo.mjs
40
+ npm install '@bartificer/linkifier';
41
41
  ```
42
42
 
43
- ### Using the Bundled Templates
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
- // import the linkify module
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
- let testURL = 'https://pbs.bartificer.net/pbs128';
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
- ### Real-World Examples
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