Skip to content

Commit

Permalink
Merge pull request #5 from artstorm/feature/page-title
Browse files Browse the repository at this point in the history
Feature/meta robots
  • Loading branch information
artstorm authored Oct 22, 2019
2 parents 36f2cef + 74cec69 commit c585b04
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Config = require("./src/Config");
const SEO = require("./src/SEO");
const Canonical = require("./src/Canonical");
const MetaRobots = require("./src/MetaRobots");

module.exports = {
configFunction: (eleventyConfig, options = {}) => {
Expand All @@ -15,5 +16,10 @@ module.exports = {
const canonical = new Canonical(liquidEngine, config);
return canonical.getObject();
});

eleventyConfig.addLiquidTag("metaRobots", liquidEngine => {
const metaRobots = new MetaRobots(liquidEngine, config);
return metaRobots.getObject();
});
}
};
70 changes: 48 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ An [Eleventy](https://github.com/11ty/eleventy) plugin to generate meta tags for

_I wrote this plugin when moving from Jekyll to Eleventy to get the functionality I previously had with Jekyll SEO Tag._

## Features

* Canonical URL.
* Robots meta directive for pagination.

## Installation

Available on [npm](https://www.npmjs.com/package/eleventy-plugin-seo):
Expand All @@ -27,6 +32,16 @@ module.exports = function(eleventyConfig) {
};
```

## Usage

Add the following right before `</head>` in your site's template(s):

```liquid
{% seo %}
```

Done!

## Config

Pass in an object with config options to the plugin:
Expand All @@ -47,50 +62,61 @@ eleventyConfig.addPlugin(pluginSEO, require("./src/_data/seo.json"));

#### url

Full URL to the site, without trailing slash. Ie, `https://foo.com`.

## Usage

Add the following right before `</head>` in your site's template(s):

```liquid
{% seo %}
```

Done!
Full URL to the site without trailing slash, `https://foo.com`.

## Additional Tags

While adding the `seo` tag is all that is needed, the plugin defines more liquid tags that it uses internally that can be convenient to use in other places.
While adding the `{% seo %}` tag is all that is needed, the plugin defines more liquid tags that it uses internally that can be convenient to use in other places.

The following tags are supplied by the plugin.
The following liquid tags are supplied by the plugin.

### `canonicalURL [url]`

Uses: `site.url`
Generates the canonical URL for the current page or for another page if passing in an argument.

```liquid
{% canonicalURL %}
{% canonicalURL /feed.xml %}
{% canonicalURL post.url %}
```

Use without arg to canonical url for current page
```
Use without arg to canonical url for current page:

```liquid
<link rel="canonical" href="{% canonicalURL %}">
```

Use with arg to link to a specific page, like feed.xml
Use with arg to link to a specific page, like feed.xml:

```liquid
<link type="application/atom+xml" rel="alternate" href="{% canonicalURL /feed.xml %}" title="{{ site.title }}" >
```

Use with eleventy data arg, like post.url that resolves to the current url in places like feed.liquid or sitemap.liquid
Use with Eleventy provided variables, like `page.url` that resolves to an url in places like...

...feed.liquid:

```liquid
{% for post in collections.posts limit: 10 %}
...
<link>{% canonicalURL post.url %}</link>
...
{% endfor %}
```

...sitemap.liquid:
```liquid
// from feed.liquid
<link>{% canonicalURL post.url %}</link>
{% for item in collections.all %}
...
<loc>{% canonicalURL item.url %}</loc>
...
{% endfor %}
```

### `metaRobots`

// from sitemap.liquid
<loc>{% canonicalURL item.url %}</loc>
Adds robots meta directive with `index,follow` except on paginated pages which gets `noindex,follow`.

```liquid
{% metaRobots %}
```
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "eleventy-plugin-seo",
"version": "0.3.1",
"version": "0.3.2",
"description": "Eleventy plugin to generate meta tags for improved SEO.",
"main": ".eleventy.js",
"scripts": {
"check": "npx prettier --check 'src/**/*.js' 'test/**/*.js' .eleventy.js",
"test": "npx nyc --reporter=lcov ava"
"test": "npx nyc --reporter=lcov --reporter=text ava"
},
"files": [
"src"
],
"repository": {
"type": "git",
"url": "git+https://github.com/artstorm/eleventy-plugin-seo.git"
Expand Down
22 changes: 22 additions & 0 deletions src/MetaRobots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const BaseTag = require("./BaseTag");

class MetaRobots extends BaseTag {
getObject() {
return {
render: (scope, hash) => {
const pagination = scope.contexts[0].pagination;
let robots = "index,follow";

if (pagination) {
if (pagination.pageNumber > 0) {
robots = `no${robots}`;
}
}

return Promise.resolve(robots);
}
};
}
}

module.exports = MetaRobots;
2 changes: 2 additions & 0 deletions src/SEO.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class SEO extends BaseTag {
parse: (tagToken, remainToken) => {},
render: async (scope, hash) => {
const template = `
<meta name="robots" content="{% metaRobots %}">
<link rel="canonical" href="{% canonicalURL %}">
`;

Expand Down
42 changes: 42 additions & 0 deletions test/MetaRobotsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import test from "ava";
import MetaRobots from "../src/MetaRobots";

test.beforeEach(t => {
t.context.scope = {
contexts: [
{
pagination: {
pageNumber: 0
}
}
]
};
});

test("Ordinary pages gets index and follow", t => {
const metaRobots = new MetaRobots();
const object = metaRobots.getObject();

return object.render(t.context.scope).then(result => {
t.is(result, "index,follow");
});
});

test("Ordinary pages gets noindex and follow", t => {
const metaRobots = new MetaRobots();
const object = metaRobots.getObject();
t.context.scope.contexts[0].pagination.pageNumber = 1;

return object.render(t.context.scope).then(result => {
t.is(result, "noindex,follow");
});
});

test("Missing pagination gets index and follow", t => {
const metaRobots = new MetaRobots();
const object = metaRobots.getObject();

return object.render({ contexts: [{}] }).then(result => {
t.is(result, "index,follow");
});
});
2 changes: 2 additions & 0 deletions test/SEOTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ test("SEO renders template", t => {
const seo = new SEO(liquidEngine, config);
const object = seo.getObject();

object.parse();

return object.render(t.context.scope).then(result => {
t.truthy(
result.includes(`<link rel="canonical" href="{% canonicalURL %}">`)
Expand Down

0 comments on commit c585b04

Please sign in to comment.