Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

metalsmith-bitly

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

metalsmith-bitly - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

83

lib/index.js

@@ -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"
}
}
# 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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc