metalsmith-bitly
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,31 +0,68 @@ | ||
const Bitly = require('bitly'); | ||
// const Bitly = require('bitlyapi'); | ||
const Bitly = require('bitlyapi'); | ||
/** | ||
* Expose `plugin`. | ||
*/ | ||
module.exports = plugin; | ||
const plugin = (options = {}, ...rest) => { | ||
const accessToken = options.accessToken; | ||
const bitly = new Bitly(accessToken); | ||
const pathMetadataKey = options.pathMetadataKey; | ||
const brandedShortDomain = options.brandedShortDomain; | ||
/** | ||
* Metalsmith plugin to hide drafts from the output. | ||
* | ||
* @return {Function} | ||
*/ | ||
return (files, metalsmith, done) => { | ||
const baseURL = options.baseURL || metalsmith.metadata()[options.baseURLGlobalMetadataKey]; | ||
function plugin(options) { | ||
const bitly = new Bitly(options.token); | ||
// Errors | ||
// ====== | ||
return (files, metalsmith, done) => { | ||
for (const file in files) { | ||
bitly.shorten(metalsmith.metadata().siteUrl + files[file].slug) | ||
.then(function(response) { | ||
files[file].bitlyURL = (options.shortUrl) ? options.shortUrl + response.data.hash : response.data.url; | ||
if (rest.length > 0) { | ||
done(new Error('invalid options, this plugin expects a single options object.')) | ||
}; | ||
console.log(response.data); | ||
if (!accessToken) { | ||
done(new Error('you must provide a value for accessToken')) | ||
}; | ||
done(); | ||
}, function(error) { | ||
throw error; | ||
}); | ||
if (!baseURL) { | ||
done(new Error('you must provide a metadate value for siteUrl or the baseURL option on the plugin')) | ||
}; | ||
}; | ||
// Functions | ||
// ========= | ||
const addBitlyMeta = async (filename) => { | ||
const file = files[filename]; | ||
const pageURL = (pathMetadataKey) ? baseURL + file[pathMetadataKey] : baseURL + file.path; | ||
const response = await bitly.shorten(pageURL); | ||
const bitlyURL = await makeUrl(response); | ||
return file.bitlyURL = bitlyURL; | ||
}; | ||
const makeUrl = async (response) => { | ||
// check to see if a Branded Short Domain is provided. if it is concatenate it with the hash, otherwise use | ||
// the default URL response | ||
return (brandedShortDomain) ? brandedShortDomain + response.data.hash : response.data.url; | ||
}; | ||
const filterFiles = (filename) => { | ||
const file = files[filename]; | ||
// filter out files that are undefined and lack a metaDataKey or path | ||
return (file[pathMetadataKey] !== undefined && (file[pathMetadataKey] || file.path)); | ||
}; | ||
// The Business | ||
// ============ | ||
// Map all files that should be processed to an array of promises | ||
const promises = Object.keys(files) | ||
.filter(filterFiles) | ||
.map(addBitlyMeta) | ||
// Call done callback when all promises are resolved | ||
Promise.all(promises) | ||
.then(() => done()) | ||
.catch(error => done(error)); | ||
} | ||
}; | ||
module.exports = plugin; |
{ | ||
"name": "metalsmith-bitly", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A metalsmith plugin for bitly links", | ||
@@ -18,3 +18,3 @@ "main": "lib/index.js", | ||
"author": "I. Welch Canavan", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -25,4 +25,4 @@ "url": "https://github.com/xiwcx/metalsmith-bitly/issues" | ||
"dependencies": { | ||
"bitly": "^4.1.1" | ||
"bitlyapi": "^1.0.9" | ||
} | ||
} |
163
README.md
# metalsmith-bitly | ||
A Metalsmith plugin that add bitly links to the metadat of each file. | ||
[![npm version][version-badge]][version-url] | ||
> A Metalsmith plugin that adds [bitly](http://bitly.com) links to the metadata of each file. | ||
This plugin allows you to retrieve bit.ly short links and use them in your templates. For support questions please use | ||
[stack overflow][stackoverflow-url] or the [slack channel][slack-url]. | ||
## Installation | ||
``` | ||
$ npm i metalsmith-bitly | ||
``` | ||
## Usage | ||
You can use `metalsmith-bitly` with the with Metalsmith's [Javascript API][metalsmith-api-url] or [CLI][metalsmith-cli-url]. You must also set the `siteURL` value on your site's metadata configuration. | ||
### Options | ||
* `accessToken`: your personal bitly access token (**required**) | ||
* `baseURL`: your production base URL (_semi-required_, either this or `baseURLGlobalMetadataKey` is needed) | ||
* `baseURLGlobalMetadataKey`: the global metaday key of your production base URL (_semi-required_, either this or `baseURL` is needed) | ||
* `brandedShortDomain`: an override for the default domain (optional, defaults to `bit.ly`) | ||
* `pathMetadataKey`: an override for the default file path (optional, defaults to `path`) | ||
#### `accessToken` | ||
In order to utilize this plugin you must request an access token at [bit.ly's developer site][bitly-auth-url]. Please remember to exclude your bitly authorization key if your repository is public. You can use the below example and include your private configuration file in your repository's `.gitignore` file. | ||
```javascript | ||
const bitly = require('metalsmith-bitly'); | ||
const configPrivate = require('../configPrivate'); | ||
const Metalsmith = require('metalsmith'); | ||
return Metalsmith(__dirname) | ||
.use(bitly({ | ||
accessToken: configPrivate.bitlyToken, | ||
baseURL: 'http://welchcanavan.com/' | ||
})) | ||
.build(function(err) { | ||
if (err) throw err; | ||
}); | ||
``` | ||
#### `baseURL` | ||
A semi-required value, you must specify either this or [`baseURLGlobalMetadataKey`](#baseurlglobalmetadatakey). This is the base URL that will be combined with your file's path to sent to bit.ly. | ||
```javascript | ||
const bitly = require('metalsmith-bitly'); | ||
const configPrivate = require('../configPrivate'); | ||
const Metalsmith = require('metalsmith'); | ||
return Metalsmith(__dirname) | ||
.use(bitly({ | ||
accessToken: configPrivate.bitlyToken, | ||
baseURL: 'http://welchcanavan.com/' | ||
})) | ||
.build(function(err) { | ||
if (err) throw err; | ||
}); | ||
``` | ||
#### `baseURLGlobalMetadataKey` | ||
A semi-required value, you must specify either this or [`baseURL`](#baseurl). Use this if you'd like to make sure this plugin stays in sync with a globally used or required metadate value for your site's production base URL. | ||
```javascript | ||
const bitly = require('metalsmith-bitly'); | ||
const configPrivate = require('../configPrivate'); | ||
const Metalsmith = require('metalsmith'); | ||
return Metalsmith(__dirname) | ||
.metadata({ | ||
siteURL: 'http://welchcanavan.com/', | ||
}) | ||
.use(bitly({ | ||
accessToken: configPrivate.bitlyToken, | ||
baseURLGlobalMetadataKey: siteURL | ||
})) | ||
.build(function(err) { | ||
if (err) throw err; | ||
}); | ||
``` | ||
#### `brandedShortDomain` | ||
Specify this value if you have set up a [Branded Short Domain][bitly-bsd-url] with bit.ly. | ||
```javascript | ||
const bitly = require('metalsmith-bitly'); | ||
const configPrivate = require('../configPrivate'); | ||
const Metalsmith = require('metalsmith'); | ||
return Metalsmith(__dirname) | ||
.use(bitly({ | ||
accessToken: configPrivate.bitlyToken, | ||
baseURL: 'http://welchcanavan.com/', | ||
brandedShortDomain: 'http://xiw.cx/' | ||
})) | ||
.build(function(err) { | ||
if (err) throw err; | ||
}); | ||
``` | ||
#### `pathMetadataKey` | ||
To be used in concert with the [metalsmith-permalinks][permalinks-url] plugin. You can specify a file metadata key to match a pattern used in the permalinks plugin. Order is important here, you have to run `metalsmith-bitly` after `metalsmith-permalinks` or the paths won't match. | ||
```markdown | ||
--- | ||
title: A Post About A Thing | ||
slug: post-thing | ||
--- | ||
``` | ||
```javascript | ||
const bitly = require('metalsmith-bitly'); | ||
const collections = require('metalsmith-collections'); | ||
const configPrivate = require('../configPrivate'); | ||
const Metalsmith = require('metalsmith'); | ||
const permalinks = require('metalsmith-permalinks'); | ||
return Metalsmith(__dirname) | ||
.use(collections({ | ||
posts: { | ||
pattern: 'posts/*.md', | ||
sortBy: 'date', | ||
reverse: true | ||
} | ||
})) | ||
.use(permalinks({ | ||
linksets: [ | ||
{ | ||
match: { collection: 'posts' }, | ||
pattern: ':slug' | ||
} | ||
]}) | ||
) | ||
.use(bitly({ | ||
accessToken: configPrivate.bitlyToken, | ||
pathMetadataKey: 'slug' | ||
})) | ||
.build(function(err) { | ||
if (err) throw err; | ||
}); | ||
``` | ||
**Note:** I've thought about including an option to focus this plugin on a collection or list of collections. If that would be of interest to you, feel free to leave a note or a pull request. | ||
## License | ||
MIT | ||
[bitly-auth-url]: http://bitly.com/a/oauth_apps | ||
[bitly-bsd-url]: https://support.bitly.com/hc/en-us/articles/230898888-How-do-I-set-up-a-Branded-Short-Domain-BSD- | ||
[metalsmith-api-url]: https://github.com/segmentio/metalsmith#api | ||
[metalsmith-cli-url]: https://github.com/segmentio/metalsmith#cli | ||
[permalinks-url]: https://github.com/segmentio/metalsmith-permalinks | ||
[stackoverflow-url]: http://stackoverflow.com/questions/tagged/metalsmith | ||
[slack-url]: http://metalsmith-slack.herokuapp.com/ | ||
[version-badge]: https://img.shields.io/npm/v/metalsmith-bitly.svg | ||
[version-url]: https://www.npmjs.com/package/metalsmith-bitly |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8144
52
165
+ Addedbitlyapi@^1.0.9
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@0.2.01.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedbitlyapi@1.0.9(transitive)
+ Addedboom@5.3.3(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbrowser-stdout@1.3.0(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcaseless@0.11.0(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.9.0(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedcoveralls@2.13.3(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddebug@2.6.8(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddiff@3.2.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedesprima@2.7.3(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.1.4(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgenerate-function@2.3.1(transitive)
+ Addedgenerate-object-property@1.2.0(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedglob@7.1.1(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedgraceful-readlink@1.0.1(transitive)
+ Addedgrowl@1.9.2(transitive)
+ Addedhar-validator@2.0.6(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhas-flag@1.0.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhe@1.1.1(transitive)
+ Addedhoek@4.3.1(transitive)
+ Addedhttp-signature@1.1.1(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-my-ip-valid@1.0.1(transitive)
+ Addedis-my-json-valid@2.20.6(transitive)
+ Addedis-property@1.0.2(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjs-yaml@3.6.1(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjson3@3.3.2(transitive)
+ Addedjsonpointer@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlcov-parse@0.0.10(transitive)
+ Addedlodash._baseassign@3.2.0(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._basecreate@3.0.3(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash.create@3.1.1(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedlog-driver@1.2.5(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@0.0.81.2.0(transitive)
+ Addedmkdirp@0.5.1(transitive)
+ Addedmocha@3.5.3(transitive)
+ Addedms@2.0.0(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.13.16.3.3(transitive)
+ Addedrequest@2.79.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedstringstream@0.0.6(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.03.1.2(transitive)
+ Addedtough-cookie@2.3.4(transitive)
+ Addedtunnel-agent@0.4.3(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addedurl@0.11.4(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedbitly@^4.1.1
- Removedbitly@4.1.1(transitive)
- Removedisomorphic-fetch@2.2.1(transitive)
- Removedwhatwg-fetch@3.6.20(transitive)