Constructor
new Linkifier()
- Description:
Builds a Linkifier instance ready for use rendering links using the default configration.
- Source:
- See:
-
- module:defaults for the default configuration settings.
Members
(readonly) defaultTemplate :module:link-template.LinkTemplate
- Description:
The default link template.
- Source:
The default link template.
Type:
defaultTemplateName :string
- Description:
The name of the default template used when rendering links.
- Source:
The name of the default template used when rendering links.
Type:
- string
(readonly) domainToDefaultTemplateNameMappings :Object.<string, string>
- Description:
The mappings of domain names to default template names.
- Source:
The mappings of domain names to default template names.
Type:
- Object.<string, string>
(readonly) domainToTransformerMappings :Object.<string, dataTransformer>
- Description:
The registered mappings from domain names to data transformers.
- Source:
The registered mappings from domain names to data transformers.
Type:
- Object.<string, dataTransformer>
(readonly) smallWords :Set.<string>
- Description:
The set of known small words for title case conversions.
This list is initialised with the list of small words from the title-case module augmented with the additionall small words defined in the defaults module.
- Source:
- See:
-
- module:defaults.smallWords for the additional small words added from the defaults module.
- module:title-case for details of the title-case dependency.
The set of known small words for title case conversions.
This list is initialised with the list of small words from the title-case module augmented with the additionall small words defined in the defaults module.
Type:
- Set.<string>
(readonly) speciallyCapitalisedWords :Set.<string>
- Description:
The set of known of known words with special capitalisations.
Note that 'words' can contain spaces, e.g. 'the US' can be added as a word to avoid the word 'us' being wrongly capitalised.
This list is initialised with the list of specially capitalised words from the defaults module.
- Source:
- See:
-
- module:defaults.speciallyCapitalisedWords for the initial list of specially capitalised words.
The set of known of known words with special capitalisations.
Note that 'words' can contain spaces, e.g. 'the US' can be added as a word to avoid the word 'us' being wrongly capitalised.
This list is initialised with the list of specially capitalised words from the defaults module.
Type:
- Set.<string>
(readonly) templateNames :Array.<string>
- Description:
A list of the names of the registered link templates.
- Source:
A list of the names of the registered link templates.
Type:
- Array.<string>
util
- Description:
Shorthand property for
.utilities.
- Source:
- See:
Shorthand property for .utilities.
(readonly) utilities :Object.<string, function()>
- Description:
A collection of utility functions, both used within the module's own code, and available for use when customising the module.
Note that for convenience, this same suite of functions is also available as a static member.
- Source:
- See:
-
- module:utilities for the utility functions available in this collection.
A collection of utility functions, both used within the module's own code, and available for use when customising the module.
Note that for convenience, this same suite of functions is also available as a static member.
Type:
- Object.<string, function()>
(static, readonly) defaults :Object
- Description:
The default values used for renderig links.
- Source:
- See:
-
- module:defaults for the list of defaults defined.
The default values used for renderig links.
Type:
- Object
(static, readonly) utilities :Object
- Description:
A suite of utility functions.
Note that for convenience, this same suite of functions is also available as an instance member.
- Source:
- See:
-
- module:utilities for the list of functions provided.
A suite of utility functions.
Note that for convenience, this same suite of functions is also available as an instance member.
Type:
- Object
Methods
(async) fetchPageData(url, extraFieldsExtractoropt) → {module:page-data.PageData}
- Description:
Fetch the page data for a given URL.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
url |
string | The URL to fetch the page data for. |
|
extraFieldsExtractor |
extraFieldsExtractorFunction |
<optional> |
An optional function to extract additional fields from the web page DOM object. |
Throws:
-
-
A TypeError is thrown if the URL is missing or invalid, or if the extraFieldsExtractor is provided but is not a function.
- Type
- TypeError
-
-
-
An Error is thrown if the page source cannot be fetched and an extraFieldsExtractor is provided (can't fall back to reversing the URL slug). Any errors thrown by the field extractor, if present, are also re-thrown.
- Type
- Error
-
Returns:
(async) generateLink(url, templateNameopt) → {string}
- Description:
Generate a link given a URL. By default the registered template for the URL's domain will be used, or, if none is registered, the overall default will be used (
html).
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
url |
string | ||
templateName |
string |
<optional> |
An optional template name to override the normal template resolution process. The value passed must correspond to a registered template name, and will be coerced to a string with |
Throws:
-
-
A TypeError is thrown if the URL is missing or invalid.
- Type
- TypeError
-
-
-
A TypeError is thrown if a template name is passed but is invalid or doesn't correspond to a registered template.
- Type
- TypeError
-
-
-
An error is thrown if the resolved template supports extra fields but the page source cannot be fetched, or, if the templates's extra field extractor function throws an error, or, if the link data transformation fails.
- Type
- Error
-
Returns:
The generated link.
- Type
- string
getTemplate(templateName) → {module:link-template.LinkTemplate}
- Description:
Get a registered link template by name.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
templateName |
string |
Throws:
-
A validation error is thrown unless a valid name is passed and corresponds to a registered template.
- Type
- TypeError
Returns:
getTemplateForDomain(domain) → {string}
- Description:
Get the template for a given domain.
Note that domains are searched from the subdomain up. For example, if passed the domain
www.bartificer.iethe function will first look for a template for the domainwww.bartificer.ie, if there's no template registered for that domain it will look for a template for the domainbartificer.ie, if there's no template for that domain either it will return the default template.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
domain |
string | The fully qualified domain name to get the template for. |
Returns:
- Type
- string
getTemplateNameForDomain(domain) → {string}
- Description:
Get the template name for a given domain.
Note that domains are searched from the subdomain up. For example, if passed the domain
www.bartificer.iethe function will first look for a template for the domainwww.bartificer.ie, if there's no template registered for that domain it will look for a template for the domainbartificer.ie, if there's no template for that domain either it will return the default template.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
domain |
string | The fully qualified domain name to get the template for. |
Returns:
- Type
- string
getTransformerForDomain(domain) → {dataTransformer}
- Description:
Get the data transformer function for a given domain.
Note that domains are searched from the subdomain up. For example, if passed the domain
www.bartificer.iethe function will first look for a transformer for the domainwww.bartificer.ie, if there's no transformer registered for that domain it will look for a transformer for the domainbartificer.ie, if there's no transformer for that domain either it will return the default transformer.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
domain |
string | The fully qualified domain for which to get the data transformer. |
Returns:
- Type
- dataTransformer
hasTemplate(tplName) → {boolean}
- Description:
Check whether or not a template is registered with the given name
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
tplName |
string | the template name to check. |
Returns:
- Type
- boolean
registerDefaultTemplateMapping(domain, templateName)
- Description:
Register a default template for use with a given domain. This template will override the overall default for this domain and all its subdomains.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
domain |
string | The fully qualified domain name for which this template should be used by default. |
templateName |
string | The name of the template to use. |
registerTemplate(name, template)
- Description:
Register a link template.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | |
template |
module:link-template.LinkTemplate |
Throws:
-
if invalid arguments are passed
- Type
- TypeError
registerTransformer(domain, transformerFn)
- Description:
Register a data transformer function to a domain name.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
domain |
string | The fully qualified domain for which this transformer should be used. |
transformerFn |
dataTransformer | The data transformer callback. |
renderLink(lData, tpl) → {string}
- Description:
Render a link given a
LinkDataobject and a template name orLinkTemplateobject.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
lData |
module:link-data.LinkData | the information about the link. |
tpl |
module:link-template.LinkTemplate | string | a the tempalte to use, as an actualy object, or a name. |
Throws:
-
A TypeError is thrown if parameters are missing or invalid.
- Type
- TypeError
Returns:
The rendered link.
- Type
- string
transformPageData(pageData) → {module:link-data.LinkData}
- Description:
Transform a given
PageDataobject into aLinkDataobject using the appropriate transformer.
- Source:
- See:
Parameters:
| Name | Type | Description |
|---|---|---|
pageData |
module:page-data.PageData | the page data to transform. |
Throws:
-
-
A TypeError is thrown if parameters are missing or invalid.
- Type
- TypeError
-
-
-
An Error is thrown if there is a problem applying the registered transformer.
- Type
- Error
-
Returns:
(async, static) importConfig(configPathopt) → {configurationObject}
- Description:
A function to try import a customised Linkfifier object and/or CLI options from a configuration module.
If no path is provided, tries to import from a standard file name in the user's home directory.
Paths are coerced to strings with
String()and relative paths are resolved relative to the user's current working directory.
- Source:
- See:
-
- module:defaults.configFilename for the default file name used.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
configPath |
string |
<optional> |
— an optional path to import the configuration from. |
Throws:
-
-
A TypeError is thrown if the loaded module does not export an object as
default, the exported object contains a key namedlinfifierthat is not an instance of theLinkifierclass, or, a key namedoptionsthat is not an object. - Type
- TypeError
-
-
-
An Error is thrown if a path is passed but a module can't be imported from it.
- Type
- Error
-
Returns:
if no path is passed, and there is no module in the default location, an empty object will be returned.
- Type
- configurationObject