@bartificer/linkify 2.3.4 → 2.4.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/dist/index.js +1 -1
- package/docs/LinkData.class.mjs.html +93 -5
- package/docs/LinkTemplate.class.mjs.html +93 -5
- package/docs/Linkifier.class.mjs.html +99 -11
- package/docs/PageData.class.mjs.html +93 -5
- package/docs/defaults.mjs.html +121 -12
- package/docs/docdash-overrides.css +5 -0
- package/docs/externals.jsdoc.html +91 -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/index.html +930 -7
- package/docs/index.js.html +139 -17
- package/docs/module-cheerio.html +189 -3
- package/docs/module-defaults.html +585 -3
- package/docs/module-link-data.LinkData.html +944 -0
- package/docs/module-link-data.html +222 -0
- package/docs/module-link-template.LinkTemplate.html +817 -0
- package/docs/module-link-template.html +222 -0
- package/docs/module-linkifier.Linkifier.html +2323 -0
- package/docs/module-linkifier.html +232 -0
- package/docs/module-linkify.html +806 -3
- package/docs/module-mustache.html +189 -3
- package/docs/module-node-fetch.html +189 -3
- package/docs/module-page-data.PageData.html +1788 -0
- package/docs/module-page-data.html +222 -0
- package/docs/module-title-case.html +189 -3
- package/docs/module-urijs.html +189 -3
- package/docs/module-url-slug.html +189 -3
- package/docs/module-utilities.html +1468 -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/utilities.mjs.html +124 -5
- package/examples/clipboardURLToMarkdown.mjs +130 -29
- package/package.json +3 -2
- package/src/LinkData.class.mjs +2 -1
- package/src/LinkTemplate.class.mjs +2 -1
- package/src/Linkifier.class.mjs +8 -7
- package/src/PageData.class.mjs +2 -1
- package/src/defaults.mjs +31 -8
- package/src/index.js +48 -13
- package/src/utilities.mjs +33 -1
- 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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
.pln {
|
|
2
|
+
color: #ddd;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/* string content */
|
|
6
|
+
.str {
|
|
7
|
+
color: #61ce3c;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/* a keyword */
|
|
11
|
+
.kwd {
|
|
12
|
+
color: #fbde2d;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/* a comment */
|
|
16
|
+
.com {
|
|
17
|
+
color: #aeaeae;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* a type name */
|
|
21
|
+
.typ {
|
|
22
|
+
color: #8da6ce;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* a literal value */
|
|
26
|
+
.lit {
|
|
27
|
+
color: #fbde2d;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* punctuation */
|
|
31
|
+
.pun {
|
|
32
|
+
color: #ddd;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/* lisp open bracket */
|
|
36
|
+
.opn {
|
|
37
|
+
color: #000000;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* lisp close bracket */
|
|
41
|
+
.clo {
|
|
42
|
+
color: #000000;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* a markup tag name */
|
|
46
|
+
.tag {
|
|
47
|
+
color: #8da6ce;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/* a markup attribute name */
|
|
51
|
+
.atn {
|
|
52
|
+
color: #fbde2d;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* a markup attribute value */
|
|
56
|
+
.atv {
|
|
57
|
+
color: #ddd;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* a declaration */
|
|
61
|
+
.dec {
|
|
62
|
+
color: #EF5050;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* a variable name */
|
|
66
|
+
.var {
|
|
67
|
+
color: #c82829;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* a function name */
|
|
71
|
+
.fun {
|
|
72
|
+
color: #4271ae;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* Specify class=linenums on a pre to get line numbering */
|
|
76
|
+
ol.linenums {
|
|
77
|
+
margin-top: 0;
|
|
78
|
+
margin-bottom: 0;
|
|
79
|
+
padding-bottom: 2px;
|
|
80
|
+
}
|
package/docs/utilities.mjs.html
CHANGED
|
@@ -1,8 +1,68 @@
|
|
|
1
|
-
<!DOCTYPE html
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
|
|
5
|
+
<meta charset="utf-8">
|
|
6
|
+
<title>utilities.mjs - @bartificer/linkify Documentation</title>
|
|
7
|
+
|
|
8
|
+
<meta name="description" content="Documentation for the @bartificer/linkify package" />
|
|
9
|
+
|
|
10
|
+
<meta name="keywords" content="documentation, linkify, link, template, javascript, generator, npm, module, package" />
|
|
11
|
+
<meta name="keyword" content="documentation, linkify, link, template, javascript, generator, npm, module, package" />
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
<meta property="og:title" content="@bartificer/linkify"/>
|
|
16
|
+
<meta property="og:type" content="website"/>
|
|
17
|
+
<meta property="og:image" content=""/>
|
|
18
|
+
<meta property="og:site_name" content="@bartificer/linkify Documentation"/>
|
|
19
|
+
<meta property="og:url" content="https://bartificer.github.io/linkify/"/>
|
|
20
|
+
|
|
21
|
+
<script src="scripts/prettify/prettify.js"></script>
|
|
22
|
+
<script src="scripts/prettify/lang-css.js"></script>
|
|
23
|
+
<!--[if lt IE 9]>
|
|
24
|
+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
25
|
+
<![endif]-->
|
|
26
|
+
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
|
|
27
|
+
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
|
|
28
|
+
<script src="scripts/nav.js" defer></script>
|
|
29
|
+
|
|
30
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
31
|
+
</head>
|
|
32
|
+
<body>
|
|
33
|
+
|
|
34
|
+
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
|
|
35
|
+
<label for="nav-trigger" class="navicon-button x">
|
|
36
|
+
<div class="navicon"></div>
|
|
37
|
+
</label>
|
|
38
|
+
|
|
39
|
+
<label for="nav-trigger" class="overlay"></label>
|
|
40
|
+
|
|
41
|
+
<nav class="wrap">
|
|
42
|
+
|
|
43
|
+
<input type="text" id="nav-search" placeholder="Search" />
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
<h2><a href="index.html">Home</a></h2><h2><a href="https://github.com/bartificer/linkify" target="_blank" >On GitHub</a></h2><h2><a href="https://bartificer.net/" target="_blank" >Bartificer Creations</a></h2><h3>Modules</h3><ul><li><a href="module-defaults.html">defaults</a><ul class='members'><li data-type='member'><a href="module-defaults.html#.linkTemplates">linkTemplates</a></li><li data-type='member'><a href="module-defaults.html#.smallWords">smallWords</a></li><li data-type='member'><a href="module-defaults.html#.speciallyCapitalisedWords">speciallyCapitalisedWords</a></li></ul></li><li><a href="module-link-data.html">link-data</a></li><li><a href="module-link-template.html">link-template</a></li><li><a href="module-linkifier.html">linkifier</a></li><li><a href="module-linkify.html">linkify</a><ul class='members'><li data-type='member'><a href="module-linkify.html#.LinkData">LinkData</a></li><li data-type='member'><a href="module-linkify.html#.LinkTemplate">LinkTemplate</a></li><li data-type='member'><a href="module-linkify.html#.Linkifier">Linkifier</a></li><li data-type='member'><a href="module-linkify.html#.PageData">PageData</a></li><li data-type='member'><a href="module-linkify.html#.VERSION">VERSION</a></li><li data-type='member'><a href="module-linkify.html#.default">default</a></li><li data-type='member'><a href="module-linkify.html#.linkify">linkify</a></li></ul></li><li><a href="module-page-data.html">page-data</a></li><li><a href="module-utilities.html">utilities</a><ul class='methods'><li data-type='method'><a href="module-utilities.html#.batchFixCustomWordCases">batchFixCustomWordCases</a></li><li data-type='method'><a href="module-utilities.html#.escapeRegex">escapeRegex</a></li><li data-type='method'><a href="module-utilities.html#.extractSlug">extractSlug</a></li><li data-type='method'><a href="module-utilities.html#.regulariseWhitespace">regulariseWhitespace</a></li><li data-type='method'><a href="module-utilities.html#.stripQueryString">stripQueryString</a></li><li data-type='method'><a href="module-utilities.html#.stripUTMParameters">stripUTMParameters</a></li><li data-type='method'><a href="module-utilities.html#.toTitleCase">toTitleCase</a></li></ul></li></ul><h3>Classes</h3><ul><li><a href="module-link-data.LinkData.html">LinkData</a><ul class='methods'><li data-type='method'><a href="module-link-data.LinkData.html#asPlainObject">asPlainObject</a></li></ul></li><li><a href="module-link-template.LinkTemplate.html">LinkTemplate</a><ul class='methods'><li data-type='method'><a href="module-link-template.LinkTemplate.html#addFilter">addFilter</a></li><li data-type='method'><a href="module-link-template.LinkTemplate.html#filtersFor">filtersFor</a></li></ul></li><li><a href="module-linkifier.Linkifier.html">Linkifier</a><ul class='methods'><li data-type='method'><a href="module-linkifier.Linkifier.html#fetchPageData">fetchPageData</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#generateLink">generateLink</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#getTemplate">getTemplate</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#getTemplateNameForDomain">getTemplateNameForDomain</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#getTransformerForDomain">getTransformerForDomain</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#registerDefaultTemplateMapping">registerDefaultTemplateMapping</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#registerTemplate">registerTemplate</a></li><li data-type='method'><a href="module-linkifier.Linkifier.html#registerTransformer">registerTransformer</a></li></ul></li><li><a href="module-page-data.PageData.html">PageData</a><ul class='methods'><li data-type='method'><a href="module-page-data.PageData.html#addSecondaryHeading">addSecondaryHeading</a></li><li data-type='method'><a href="module-page-data.PageData.html#addTopLevelHeading">addTopLevelHeading</a></li><li data-type='method'><a href="module-page-data.PageData.html#asPlainObject">asPlainObject</a></li><li data-type='method'><a href="module-page-data.PageData.html#h1">h1</a></li><li data-type='method'><a href="module-page-data.PageData.html#h2">h2</a></li></ul></li></ul><h3>Externals</h3><ul><li><a href="module-cheerio.html">cheerio</a></li><li><a href="module-mustache.html">mustache</a></li><li><a href="module-node-fetch.html">node-fetch</a></li><li><a href="module-title-case.html">title-case</a></li><li><a href="module-urijs.html">urijs</a></li><li><a href="module-url-slug.html">url-slug</a></li></ul>
|
|
47
|
+
|
|
48
|
+
</nav>
|
|
49
|
+
|
|
50
|
+
<div id="main">
|
|
51
|
+
|
|
52
|
+
<h1 class="page-title">utilities.mjs</h1>
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
<section>
|
|
61
|
+
<article>
|
|
62
|
+
<pre class="prettyprint source linenums"><code>/**
|
|
4
63
|
* @file A collection of useful utility functions.
|
|
5
64
|
* @author Bart Busschots <opensource@bartificer.ie>
|
|
65
|
+
* @license MIT
|
|
6
66
|
*/
|
|
7
67
|
|
|
8
68
|
/**
|
|
@@ -68,6 +128,7 @@ export function escapeRegex(str) {
|
|
|
68
128
|
* @param {string} str - the string to apply the replacemnts to.
|
|
69
129
|
* @param {string[]} [words] - an array of words in their desired capitalisations. Defaults to the default list of custom capitalisations.
|
|
70
130
|
* @returns {string}
|
|
131
|
+
* @see {@link module:defaults.speciallyCapitalisedWords} for the default list of custom capitalisations.
|
|
71
132
|
*/
|
|
72
133
|
export function batchFixCustomWordCases(str, words){
|
|
73
134
|
// coerce the first argument to a string
|
|
@@ -98,6 +159,36 @@ export function batchFixCustomWordCases(str, words){
|
|
|
98
159
|
return ans;
|
|
99
160
|
}
|
|
100
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Convert a string to title case, with some custom capitalisations.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} str - the string to convert to title case.
|
|
166
|
+
* @param {string[]} [words] - a list of words with custom capitalisations to correct after title-casing. Defaults to the default list of custom capitalisations.
|
|
167
|
+
* @return {string}
|
|
168
|
+
* @see {@link module:defaults.speciallyCapitalisedWords} for the default list of custom capitalisations.
|
|
169
|
+
* @see {@link module:title-case} for the Title Case module who's titleCase function is used to convert to title case, and which has its own default list of small words that are preserved in lower case.
|
|
170
|
+
* @see {@link module:defaults.smallWords} for the additional list of small words that are preserved in lower case by the title-casing function.
|
|
171
|
+
*/
|
|
172
|
+
export function toTitleCase(str, words){
|
|
173
|
+
// coerce the first argument to a string
|
|
174
|
+
let ans = String(str);
|
|
175
|
+
|
|
176
|
+
// add the additional small words
|
|
177
|
+
for (const smallWord of defaults.smallWords){
|
|
178
|
+
console.log(`adding smallWord: ${smallWord}`);
|
|
179
|
+
titleCase.SMALL_WORDS.add(smallWord);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// convert to title case
|
|
183
|
+
ans = titleCase.titleCase(ans);
|
|
184
|
+
|
|
185
|
+
// fix any words with unusual customisations
|
|
186
|
+
ans = batchFixCustomWordCases(ans, words);
|
|
187
|
+
|
|
188
|
+
// return the result
|
|
189
|
+
return ans;
|
|
190
|
+
}
|
|
191
|
+
|
|
101
192
|
/**
|
|
102
193
|
* Extract the slug from a URL and convert it to a title-case string.
|
|
103
194
|
*
|
|
@@ -145,7 +236,35 @@ export function extractSlug(url, words){
|
|
|
145
236
|
title = titleCase.titleCase(title);
|
|
146
237
|
|
|
147
238
|
// fix any words with unusual customisations
|
|
148
|
-
title =
|
|
239
|
+
title = toTitleCase(title, words);
|
|
149
240
|
|
|
150
241
|
return title;
|
|
151
|
-
};</code></pre
|
|
242
|
+
};</code></pre>
|
|
243
|
+
</article>
|
|
244
|
+
</section>
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
</div>
|
|
252
|
+
|
|
253
|
+
<br class="clear">
|
|
254
|
+
|
|
255
|
+
<footer>
|
|
256
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Apr 13 2026 16:45:07 GMT+0100 (Irish Standard Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
257
|
+
</footer>
|
|
258
|
+
|
|
259
|
+
<script>prettyPrint();</script>
|
|
260
|
+
<script src="scripts/polyfill.js"></script>
|
|
261
|
+
<script src="scripts/linenumber.js"></script>
|
|
262
|
+
|
|
263
|
+
<script src="scripts/search.js" defer></script>
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
<link type="text/css" rel="stylesheet" href="./docdash-overrides.css">
|
|
268
|
+
|
|
269
|
+
</body>
|
|
270
|
+
</html>
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
// import Linkify Lib
|
|
2
|
+
import URI from 'urijs';
|
|
2
3
|
import { linkify, LinkTemplate, LinkData } from '../dist/index.js';
|
|
3
4
|
|
|
4
5
|
// import 3rd-party library for interacting with the clipboard
|
|
5
6
|
import clipboardy from 'clipboardy';
|
|
6
7
|
|
|
8
|
+
// NOTE: this example supports one optional argument, a special-case name, if passed:
|
|
9
|
+
// 1. if there is a custom transformer defined with that name, it will be used as a one-off transformer
|
|
10
|
+
// 2. if there is a template defined with that name, it will be used as a one-off template
|
|
11
|
+
|
|
12
|
+
//
|
|
13
|
+
// === Register custom templates, transformers, etc... ===
|
|
14
|
+
//
|
|
15
|
+
|
|
7
16
|
// register a custom Markdown link template and make it the default
|
|
8
17
|
linkify.registerTemplate('md-bartificer', new LinkTemplate(
|
|
9
18
|
'[{{{text}}} — {{{uri.hostname}}}{{#uri.hasPath}}/…{{/uri.hasPath}}]({{{url}}})',
|
|
@@ -24,28 +33,94 @@ linkify.registerTemplate('md-apr', new LinkTemplate(
|
|
|
24
33
|
));
|
|
25
34
|
linkify.registerDefaultTemplateMapping('apple.com', 'md-apr');
|
|
26
35
|
|
|
36
|
+
// register a special Markdown template for XKCD cartoons presss releases and make it the default for XKCD's domain
|
|
37
|
+
// TO DO — figure out the right way to add support for extra fields like the sequence number, image permalink & alt text
|
|
38
|
+
linkify.registerTemplate('md-xkcd', new LinkTemplate(
|
|
39
|
+
// the description should the the comic's sequence number, at least for now
|
|
40
|
+
'[XKCD {{{description}}}: {{{text}}}]({{{url}}})\n'
|
|
41
|
+
));
|
|
42
|
+
linkify.registerDefaultTemplateMapping('xkcd.com', 'md-xkcd');
|
|
43
|
+
|
|
44
|
+
// register a special Markdown template that just contains the link title, and nothing more
|
|
45
|
+
linkify.registerTemplate('md-title-only', new LinkTemplate(
|
|
46
|
+
'[{{{text}}}]({{{url}}})',
|
|
47
|
+
[
|
|
48
|
+
['url', linkify.util.stripUTMParameters],
|
|
49
|
+
['text', linkify.util.regulariseWhitespace]
|
|
50
|
+
]
|
|
51
|
+
));
|
|
52
|
+
|
|
53
|
+
// capture any templates that should be made available for special cases
|
|
54
|
+
const templates = {
|
|
55
|
+
mastodonServer: 'md-title-only'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Cache commonly needed transforer functions to reduce code repetition
|
|
59
|
+
// NOTE: these are the transformers that are available as special cases
|
|
60
|
+
const transformers = {
|
|
61
|
+
mainHeading: function(pData){
|
|
62
|
+
return new LinkData(pData.url, pData.mainHeading);
|
|
63
|
+
},
|
|
64
|
+
titleMinusPrefix: function(pData, prefix){
|
|
65
|
+
const regex = new RegExp(`^${linkify.util.escapeRegex(prefix)}`);
|
|
66
|
+
return new LinkData(pData.url, pData.title.replace(regex, '').trim());
|
|
67
|
+
},
|
|
68
|
+
titleMinusPostscript: function(pData, postscript){
|
|
69
|
+
const regex = new RegExp(`${linkify.util.escapeRegex(postscript)}$`);
|
|
70
|
+
return new LinkData(pData.url, pData.title.replace(regex, '').trim());
|
|
71
|
+
},
|
|
72
|
+
mastodonServer: function(pData){
|
|
73
|
+
const mastodonServer = pData.uri.hostname();
|
|
74
|
+
|
|
75
|
+
// see if the link points to a user-related page on the server
|
|
76
|
+
const mastodonPathMatch = pData.uri.path().match(/^\/@(?<handle>[^\/]+)(?:\/(?<postId>\d+))?/);
|
|
77
|
+
if (mastodonPathMatch && mastodonPathMatch.groups){
|
|
78
|
+
// if so, return the handle as the link text and the post ID as the description (if there is one)
|
|
79
|
+
const handle = mastodonPathMatch.groups.handle;
|
|
80
|
+
const postId = mastodonPathMatch.groups.postId;
|
|
81
|
+
|
|
82
|
+
// see if we're a post or some kind of profile page
|
|
83
|
+
if(postId){
|
|
84
|
+
// this is a post, so use the handle and the post snippet as the text
|
|
85
|
+
|
|
86
|
+
// extract the post snippet from the title
|
|
87
|
+
let snippet = pData.title.replace(/^[^"]+"/, '').replace(/"[^"]+$/, '').trim();
|
|
88
|
+
|
|
89
|
+
return new LinkData(pData.url, `@${handle}@${mastodonServer} on Mastodon: "${snippet}"`);
|
|
90
|
+
}else{
|
|
91
|
+
// some kind of profile page, so just return the handle with some text to indicate this is a Mastodon server
|
|
92
|
+
return new LinkData(pData.url, `@${handle}@${mastodonServer} on Mastodon`);
|
|
93
|
+
}
|
|
94
|
+
return new LinkData(pData.url, pData.title, mastodonServer);
|
|
95
|
+
} else {
|
|
96
|
+
// we're on a generic page, so return the title with with a note that this is a Mastodon server
|
|
97
|
+
return new LinkData(pData.url, `${pData.title} (Mastodon)`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
27
102
|
// define & register custom transformers for domains that need them
|
|
28
|
-
linkify.registerTransformer('9to5mac.com',
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return new LinkData(pData.url, pData.title.replace(' – Krebs on Security', ''));
|
|
42
|
-
});
|
|
43
|
-
linkify.registerTransformer('macstories.net', function(pData){
|
|
44
|
-
return new LinkData(pData.url, pData.title.replace(' - MacStories', ''));
|
|
45
|
-
});
|
|
46
|
-
linkify.registerTransformer('nakedsecurity.sophos.com', function(pData){
|
|
47
|
-
return new LinkData(pData.url, pData.title.replace(' – Naked Security', ''));
|
|
103
|
+
linkify.registerTransformer('9to5mac.com', transformers.mainHeading);
|
|
104
|
+
linkify.registerTransformer('apod.nasa.gov', (pData) => {
|
|
105
|
+
// parse the title to extract the date and picture title
|
|
106
|
+
let titlePartsMatch = pData.title.match(/^APOD:[ ](?<year>\d{4})[ ](?<month>[a-zA-Z]+)[ ](?<day>\d{1,2})[ ]–[ ](?<title>.+)$/);
|
|
107
|
+
if(titlePartsMatch && titlePartsMatch.groups){
|
|
108
|
+
return new LinkData(
|
|
109
|
+
pData.url,
|
|
110
|
+
`NASA Astronomy Picture of the Day for ${titlePartsMatch.groups.day} ${titlePartsMatch.groups.month} ${titlePartsMatch.groups.year}: ${titlePartsMatch.groups.title}`,
|
|
111
|
+
)
|
|
112
|
+
} else {
|
|
113
|
+
// fall back on just using the title
|
|
114
|
+
return new LinkData(pData.url, pData.title);
|
|
115
|
+
}
|
|
48
116
|
});
|
|
117
|
+
linkify.registerTransformer('social.bartificer.ie', transformers.mastodonServer);
|
|
118
|
+
linkify.registerTransformer('cultofmac.com', transformers.mainHeading);
|
|
119
|
+
linkify.registerTransformer('daringfireball.net', (pData) => transformers.titleMinusPrefix(pData, 'Daring Fireball: '));
|
|
120
|
+
linkify.registerTransformer('intego.com', (pData) => transformers.titleMinusPostscript(pData, ' | Intego'));
|
|
121
|
+
linkify.registerTransformer('krebsonsecurity.com', (pData) => transformers.titleMinusPostscript(pData, ' – Krebs on Security'));
|
|
122
|
+
linkify.registerTransformer('macstories.net', (pData) => transformers.titleMinusPostscript(pData, ' - MacStories'));
|
|
123
|
+
linkify.registerTransformer('nakedsecurity.sophos.com', (pData) => transformers.titleMinusPostscript(pData, ' – Naked Security'));
|
|
49
124
|
linkify.registerTransformer('overcast.fm', function(pData){
|
|
50
125
|
// strip Overcast append, split on em-dash to replace with n-dash and get podcast name
|
|
51
126
|
let textParts = pData.title.replace(' — Overcast', '').split('—');
|
|
@@ -55,20 +130,46 @@ linkify.registerTransformer('overcast.fm', function(pData){
|
|
|
55
130
|
let linkText = podcastName.trim() + ': ' + textParts.join(' – ').replace(/[ ]+/g, ' ').replace(':', '-').trim();
|
|
56
131
|
return new LinkData(pData.url, linkText);
|
|
57
132
|
});
|
|
58
|
-
linkify.registerTransformer('sixcolors.com',
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
linkify.registerTransformer('
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return new
|
|
133
|
+
linkify.registerTransformer('sixcolors.com', transformers.mainHeading);
|
|
134
|
+
linkify.registerTransformer('theverge.com', transformers.mainHeading);
|
|
135
|
+
linkify.registerTransformer('wired.com', transformers.mainHeading);
|
|
136
|
+
linkify.registerTransformer('xkcd.com', (pData) => {
|
|
137
|
+
// extract the sequence number from the URL and add it to the description field
|
|
138
|
+
const comicNumber = pData.uri.path().replaceAll('/', '');
|
|
139
|
+
const comicTitle = pData.title.replace(/^xkcd[:][ ]/, '');
|
|
140
|
+
return new LinkData(pData.url, comicTitle, comicNumber);
|
|
66
141
|
});
|
|
67
142
|
|
|
143
|
+
//
|
|
144
|
+
// === Genereate the Link ===
|
|
145
|
+
//
|
|
146
|
+
|
|
68
147
|
// read the URL from the clipboard
|
|
69
148
|
let testURL = clipboardy.readSync();
|
|
70
149
|
|
|
150
|
+
// check if a specicla-case was passed
|
|
151
|
+
let specialCaseName = '';
|
|
152
|
+
let templateName = null;
|
|
153
|
+
if(process.argv.length > 2){
|
|
154
|
+
specialCaseName = String(process.argv[2]);
|
|
155
|
+
}
|
|
156
|
+
if(specialCaseName){
|
|
157
|
+
// check for a special-case transformer
|
|
158
|
+
if (transformers[specialCaseName]){
|
|
159
|
+
linkify.registerTransformer(URI(testURL).hostname(), transformers[specialCaseName]);
|
|
160
|
+
} else {
|
|
161
|
+
console.warn(`No transformer found with the name "${specialCaseName}". Proceeding without a one-off transformer.`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// check for a special-case template
|
|
165
|
+
if(templates[specialCaseName]){
|
|
166
|
+
templateName = templates[specialCaseName];
|
|
167
|
+
} else {
|
|
168
|
+
console.warn(`No template found for special case "${specialCaseName}". Proceeding without a one-off template.`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
71
172
|
// try generate the formatted link from the URL
|
|
72
|
-
linkify.generateLink(testURL).then(function(d){
|
|
173
|
+
linkify.generateLink(testURL, templateName).then(function(d){
|
|
73
174
|
console.log(d);
|
|
74
175
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bartificer/linkify",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "An module for converting URLs into pretty links in any format.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "webpack",
|
|
20
|
-
"docs": "rm -rf ./docs/* && npx jsdoc -c ./jsdoc.conf.json
|
|
20
|
+
"docs": "rm -rf ./docs/* && npx jsdoc -c ./jsdoc.conf.json",
|
|
21
21
|
"release": "npm run build && npm run docs && npm login && npm publish"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"clean-jsdoc-theme": "^4.3.0",
|
|
44
|
+
"docdash": "^2.0.2",
|
|
44
45
|
"jsdoc": "^4.0.5",
|
|
45
46
|
"webpack": "^5.105.4",
|
|
46
47
|
"webpack-cli": "^7.0.2"
|
package/src/LinkData.class.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file The definition of the class representing a link.
|
|
3
3
|
* @author Bart Busschots <opensource@bartificer.ie>
|
|
4
|
+
* @license MIT
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* This module provides as class for representing the information that can be used when rendering a link.
|
|
8
|
-
* @module
|
|
9
|
+
* @module link-data
|
|
9
10
|
* @requires module:urijs
|
|
10
11
|
*/
|
|
11
12
|
import {default as URI} from 'urijs';
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file The definition of the class representing a link generation template.
|
|
3
3
|
* @author Bart Busschots <opensource@bartificer.ie>
|
|
4
|
+
* @license MIT
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* This module provides as class for representing a template used for generating links.
|
|
8
|
-
* @module
|
|
9
|
+
* @module link-template
|
|
9
10
|
* @requires module:urijs
|
|
10
11
|
*/
|
|
11
12
|
|
package/src/Linkifier.class.mjs
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file The definition of the main Linkifier class which provides the link rendering functionality with the help of the other classes and modules.
|
|
3
3
|
* @author Bart Busschots <opensource@bartificer.ie>
|
|
4
|
+
* @license MIT
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Linkifier's core link rendering functionality.
|
|
8
|
-
* @module
|
|
9
|
-
* @requires
|
|
10
|
-
* @requires
|
|
11
|
-
* @requires
|
|
9
|
+
* @module linkifier
|
|
10
|
+
* @requires link-data
|
|
11
|
+
* @requires link-template
|
|
12
|
+
* @requires page-data
|
|
12
13
|
* @requires module:node-fetch
|
|
13
14
|
* @requires module:cheerio
|
|
14
15
|
* @requires module:mustache
|
|
@@ -203,7 +204,7 @@ export class Linkifier {
|
|
|
203
204
|
|
|
204
205
|
/**
|
|
205
206
|
* The default link template.
|
|
206
|
-
* @type {LinkTemplate}
|
|
207
|
+
* @type {module:LinkTemplate.class.LinkTemplate}
|
|
207
208
|
*/
|
|
208
209
|
get defaultTemplate(){
|
|
209
210
|
return this._linkTemplates[this._pageDataToLinkTemplateName['.']];
|
|
@@ -212,8 +213,8 @@ export class Linkifier {
|
|
|
212
213
|
/**
|
|
213
214
|
* Register a link template.
|
|
214
215
|
*
|
|
215
|
-
* @param {
|
|
216
|
-
* @param {
|
|
216
|
+
* @param {string} name
|
|
217
|
+
* @param {LinkTemplate} template
|
|
217
218
|
* @throws {ValidationError} A validation error is thrown unless both a valid
|
|
218
219
|
* name and template object are passed.
|
|
219
220
|
*/
|
package/src/PageData.class.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @file The definition of the class representing a web page.
|
|
3
3
|
* @author Bart Busschots <opensource@bartificer.ie>
|
|
4
|
+
* @license MIT
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* This module provides as class for representing the information extracted from web pages which can be used to generate the link data avaiable for rendering links.
|
|
8
|
-
* @module
|
|
9
|
+
* @module page-data
|
|
9
10
|
* @requires module:urijs
|
|
10
11
|
*/
|
|
11
12
|
import {default as URI} from 'urijs';
|