hexo-util
Advanced tools
Comparing version 1.4.0 to 1.5.0
'use strict'; | ||
const { parse, format } = require('url'); | ||
const { URL } = require('url'); | ||
const { toUnicode } = require('./punycode'); | ||
const urlObj = (str) => { | ||
try { | ||
return new URL(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
}; | ||
const safeDecodeURI = (str) => { | ||
@@ -15,20 +23,9 @@ try { | ||
const decodeURL = (str) => { | ||
const parsed = parse(str); | ||
if (parsed.protocol) { | ||
const obj = Object.assign({}, { | ||
auth: parsed.auth, | ||
protocol: parsed.protocol, | ||
host: toUnicode(parsed.host), | ||
pathname: safeDecodeURI(parsed.pathname) | ||
}); | ||
const parsed = urlObj(str); | ||
if (typeof parsed === 'object') { | ||
if (parsed.origin === 'null') return str; | ||
if (parsed.hash) { | ||
Object.assign(obj, { hash: safeDecodeURI(parsed.hash) }); | ||
} | ||
if (parsed.search) { | ||
Object.assign(obj, { search: safeDecodeURI(parsed.search) }); | ||
} | ||
return format(obj); | ||
// TODO: refactor to `url.format()` once Node 8 is dropped | ||
const url = parsed.toString().replace(parsed.hostname, toUnicode(parsed.hostname)); | ||
return safeDecodeURI(url); | ||
} | ||
@@ -35,0 +32,0 @@ |
'use strict'; | ||
const { parse, format } = require('url'); | ||
const regexNonUrl = /^(data|javascript|mailto|vbscript)/i; | ||
const { toUnicode } = require('./punycode'); | ||
const { URL } = require('url'); | ||
function encodeURL(str) { | ||
const parsed = parse(str); | ||
if (parsed.slashes) { | ||
const obj = Object.assign({}, { | ||
auth: parsed.auth, | ||
protocol: parsed.protocol, | ||
host: parsed.host, | ||
pathname: encodeURI(safeDecodeURI(parsed.pathname)) | ||
}); | ||
if (parsed.hash) { | ||
Object.assign(obj, { hash: encodeURI(safeDecodeURI(parsed.hash)) }); | ||
} | ||
if (parsed.search) { | ||
Object.assign(obj, { search: encodeURI(safeDecodeURI(parsed.search)) }); | ||
} | ||
return format(obj); | ||
const urlObj = (str) => { | ||
try { | ||
return new URL(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
}; | ||
if (str.match(regexNonUrl)) return str; | ||
return encodeURI(safeDecodeURI(str)); | ||
} | ||
function safeDecodeURI(str) { | ||
const safeDecodeURI = (str) => { | ||
try { | ||
@@ -38,4 +20,18 @@ return decodeURI(str); | ||
} | ||
} | ||
}; | ||
const encodeURL = (str) => { | ||
const parsed = urlObj(str); | ||
if (typeof parsed === 'object') { | ||
if (parsed.origin === 'null') return str; | ||
parsed.search = encodeURI(safeDecodeURI(parsed.search)); | ||
// preserve IDN | ||
// TODO: refactor to url.format() once Node 8 EOL | ||
return parsed.toString().replace(parsed.hostname, toUnicode(parsed.hostname)); | ||
} | ||
return encodeURI(safeDecodeURI(str)); | ||
}; | ||
module.exports = encodeURL; |
@@ -11,2 +11,3 @@ 'use strict'; | ||
'\'': ''', | ||
'`': '`', | ||
'/': '/' | ||
@@ -21,5 +22,5 @@ }; | ||
// http://stackoverflow.com/a/12034334 | ||
return str.replace(/[&<>"'/]/g, a => htmlEntityMap[a]); | ||
return str.replace(/[&<>"'`/]/g, a => htmlEntityMap[a]); | ||
} | ||
module.exports = escapeHTML; |
'use strict'; | ||
const { parse } = require('url'); | ||
const { URL } = require('url'); | ||
const encodeURL = require('./encode_url'); | ||
const urlObj = (str) => { | ||
try { | ||
return new URL(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
}; | ||
function fullUrlForHelper(path = '/') { | ||
if (path.startsWith('//')) return path; | ||
const { config } = this; | ||
const data = parse(path); | ||
const data = urlObj(path); | ||
// Exit if this is an external path | ||
if (data.protocol) return path; | ||
if (typeof data === 'object') { | ||
if (data.origin !== 'null') return path; | ||
} | ||
@@ -14,0 +24,0 @@ path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/')); |
@@ -24,9 +24,12 @@ 'use strict'; | ||
const data = highlight(str, options); | ||
const lang = options.lang || data.language || ''; | ||
const classNames = (useHljs ? 'hljs' : 'highlight') + (lang ? ` ${lang}` : ''); | ||
if (useHljs && !gutter) wrap = false; | ||
if (gutter && !wrap) wrap = true; // arbitrate conflict ("gutter:true" takes priority over "wrap:false") | ||
const before = useHljs ? `<pre><code class="hljs ${options.lang}">` : '<pre>'; | ||
const before = useHljs ? `<pre><code class="${classNames}">` : '<pre>'; | ||
const after = useHljs ? '</code></pre>' : '</pre>'; | ||
if (!wrap) return useHljs ? before + data.value + after : data.value; | ||
if (!wrap) return `<pre><code class="${classNames}">${data.value}</code></pre>`; | ||
@@ -33,0 +36,0 @@ const lines = data.value.split('\n'); |
@@ -20,2 +20,3 @@ 'use strict'; | ||
exports.htmlTag = require('./html_tag'); | ||
exports.isExternalLink = require('./is_external_link'); | ||
exports.Pattern = require('./pattern'); | ||
@@ -22,0 +23,0 @@ exports.Permalink = require('./permalink'); |
@@ -9,2 +9,3 @@ 'use strict'; | ||
''': '\'', | ||
'`': '`', | ||
'/': '/' | ||
@@ -11,0 +12,0 @@ }; |
'use strict'; | ||
const { parse } = require('url'); | ||
const { URL } = require('url'); | ||
const encodeURL = require('./encode_url'); | ||
const relative_url = require('./relative_url'); | ||
const urlObj = (str) => { | ||
try { | ||
return new URL(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
}; | ||
function urlForHelper(path = '/', options) { | ||
@@ -14,3 +22,3 @@ if (path[0] === '#' || path.startsWith('//')) { | ||
const { root } = config; | ||
const data = parse(path); | ||
const data = urlObj(path); | ||
@@ -22,4 +30,4 @@ options = Object.assign({ | ||
// Exit if this is an external path | ||
if (data.protocol) { | ||
return path; | ||
if (typeof data === 'object') { | ||
if (data.origin !== 'null') return path; | ||
} | ||
@@ -26,0 +34,0 @@ |
{ | ||
"name": "hexo-util", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Utilities for Hexo.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index", |
@@ -11,2 +11,32 @@ # hexo-util | ||
## Table of contents | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [CacheStream](#cachestream) | ||
- [camelCaseKeys](#camelcasekeysobj-options) | ||
- [createSha1Hash](#createsha1hash) | ||
- [decodeURL](#decodeurlstr) | ||
- [encodeURL](#encodeurlstr) | ||
- [escapeDiacritic](#escapediacriticstr) | ||
- [escapeHTML](#escapehtmlstr) | ||
- [escapeRegex](#escaperegexstr) | ||
- [full_url_for](#full_url_forpath) | ||
- [gravatar](#gravatarstr-options) | ||
- [hash](#hashstr) | ||
- [highlight](#highlightstr-options) | ||
- [htmlTag](#htmltagtag-attrs-text-escape) | ||
- [isExternalLink](#isexternallinkurl) | ||
- [Pattern](#patternrule) | ||
- [Permalink](#permalinkrule-options) | ||
- [relative_url](#relative_urlfrom-to) | ||
- [slugize](#slugizestr-options) | ||
- [spawn](#spawncommand-args-options) | ||
- [stripHTML](#striphtmlstr) | ||
- [wordWrap](#wordwrapstr-options) | ||
- [truncate](#truncatestr-options) | ||
- [unescapeHTML](#unescapehtmlstr) | ||
- [url_for](#url_forpath-option) | ||
- [bind(hexo)](#bindhexo) | ||
## Installation | ||
@@ -78,2 +108,7 @@ | ||
// /foo/bár/ | ||
/* Alternatively, Node 10+ offers native API to decode punycoded domain */ | ||
const {format} = require('url') | ||
decodeURI(format(new URL('http://xn--br-mia.com.com/b%C3%A1r'), {unicode: true})) | ||
// http://bár.com/báz | ||
``` | ||
@@ -83,3 +118,3 @@ | ||
Encode URL or path into a [safe format](https://en.wikipedia.org/wiki/Percent-encoding). Domain is encoded into [punycode](https://en.wikipedia.org/wiki/Punycode) when necessary. | ||
Encode URL or path into a [safe format](https://en.wikipedia.org/wiki/Percent-encoding). | ||
@@ -90,5 +125,2 @@ ``` js | ||
encodeURL('http://bár.com/baz') | ||
// http://xn--br-mia.com/baz | ||
encodeURL('/foo/bár/') | ||
@@ -229,2 +261,38 @@ // /foo/b%C3%A1r/ | ||
### isExternalLink(url) | ||
Returns if a given url is external link relative to `config.url` and `config.exclude`. | ||
``` yml | ||
_config.yml | ||
url: https://example.com # example | ||
``` | ||
``` js | ||
isExternalLink('https://example.com'); | ||
// false | ||
isExternalLink('/archives/foo.html'); | ||
// false | ||
isExternalLink('https://foo.com/'); | ||
// true | ||
``` | ||
``` yml | ||
_config.yml | ||
url: https://example.com # example | ||
exclude: | ||
- foo.com | ||
- bar.com | ||
``` | ||
``` js | ||
isExternalLink('https://foo.com'); | ||
// false | ||
isExternalLink('https://bar.com'); | ||
// false | ||
isExternalLink('https://baz.com/'); | ||
// true | ||
``` | ||
### Pattern(rule) | ||
@@ -425,2 +493,3 @@ | ||
- [`relative_url()`](#relative_urlfrom-to) | ||
- [`isExternalLink()`](#isexternallinkurl) | ||
@@ -427,0 +496,0 @@ Below examples demonstrate different approaches to creating a [helper](https://hexo.io/api/helper) (each example is separated by `/******/`), |
72603
31
1339
556