Giter Site home page Giter Site logo

docpad-plugin-cleanurls's Introduction

Clean URLs Plugin for DocPad

Travis CI Build Status NPM version NPM downloads Dependency Status Dev Dependency Status
GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Adds support for clean URLs to DocPad

Usage

static

In non-static environments we work by setting the document's url to it's clean url. This means that redirection occurs on the dynamic server level.

For the static environment (i.e. when running DocPad with the --env static flag, e.g. running docpad generate --env static) we will set the static plugin configuration option to true. This will in addition to performing redirections via the built-in dynamic server within DocPad for speed, we will also write special static redirection HTML files to the output directory, that will redirect the user to the new clean location (e.g. the document pages/welcome.html will now be outputted to pages/welcome/index.html, with pages/welcome.html now being a special HTML redirect document to the clean location).

If you would like to always use the static mode, you can set the static plugin configuration option to true with:

plugins:
	cleanurls:
		static: true

If you would like to disable the static mode for the static environment, you can do so with:

environments:
	static:
		plugins:
			cleanurls:
				static: false

If you would like to disable clean urls completely (not just the static mode) in the static environment, you can do so with:

environments:
	static:
		plugins:
			cleanurls:
				enabled: false

trailingSlashes

Enable this plugin configuration option to generate document.urls like '/beep/' instead of /beep. Defaults to false.

collectionName

You can use this plugin configuration option (defaults to html) to tell the cleanurls plugin to use your own custom collection for which documents to apply clean URLs to.

For instance, if you are wanting to remove all clean URLs for all documents that have cleanurls: false in the meta data, then you could do the following in your DocPad configuration file:

# Define a custom collection for cleanurls that ignores the documents we don't want
collections:
	cleanurls: ->
		@getCollection('html').findAllLive(cleanurls: $ne: false)

# Tell our clean urls plugin to use this collection
plugins:
	cleanurls:
		collectionName: 'cleanurls'

getRedirectTemplate

You can customise the HTML template that is used for the redirect pages by specifying the getRedirectTemplate option which is a function that accepts url argument and an option title argument and returns a string.

simpleRedirects

Simple redirects work via routes in dynamic environments, and on static environments work via generating redirect HTML pages at the location of the source relative URL. They can be defined like so:

plugins:
	cleanurls:
		simpleRedirects:
			'/relative-url': '/somewhere-else'
			'/other-relative-url': 'http://somehere.else'

advancedRedirects

Advanced redirects work via routes in dynamic environments, and on static environments work via a client-side javascript injection into the Script Block on your 404 Page document. They can be defined like so:

plugins:
	cleanurls:
		advancedRedirects: [
			# Regular expressions redirects are possible too
			[/^\/github\/?(.*)$/, 'https://github.com/docpad/$1']
			[/^\/plugin\/(.+)$/, 'https://github.com/docpad/docpad-plugin-$1']

			# Absolute URL redirects are even possible
			['http://production.com/favourite-website', 'http://wikipedia.org']
			['http://localhost:9778/favourite-website', 'http://facebook.com']
		]

To ensure they work, you must make sure that your 404 Page document calls @getBlock('scripts').toHTML() to output the Script Block, which we inject the client-side javascript into. Here is an example of such a document using eco and location at src/documents/404.html.eco:

<!DOCTYPE html>
<html>
    <head>
        <!-- Standard Meta -->
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <!-- Site Properties -->
        <title>404 Page Not Found</title>

        <!-- DocPad Meta -->
        <%- @getBlock('meta').toHTML() %>

        <!-- DocPad Styles -->
        <%- @getBlock('styles').add('/vendor/404.css').toHTML() %>
    </head>
    <body>
        <!-- 404 Page Content -->
        <div class="container">
            <h1>Not Found ๐Ÿ˜ฒ</h1>
            <p>Sorry, but the page you were trying to view does not exist.</p>
            <p>It looks like this was the result of either:</p>
            <ul>
                <li>a mistyped address</li>
                <li>an out-of-date link</li>
            </ul>
            <script>
                var GOOG_FIXURL_LANG = (navigator.language || '').slice(0, 2),
                    GOOG_FIXURL_SITE = location.host
            </script>
            <script src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>
        </div>

        <!-- DocPad Scripts -->
        <%- @getBlock('scripts').toHTML() %>
    </body>
</html>

You can modify the client-side javascript by providing the option getRedirectScript` which is a function that accepts the advancedRedirects value as the first and only argument and returns a string which is the script to be injected.

Install

Install this DocPad plugin by entering docpad install cleanurls into your terminal.

History

Discover the release history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Contributors

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under:

docpad-plugin-cleanurls's People

Contributors

balupton avatar hurrymaplelad avatar misterdai avatar robloach avatar stongo avatar stormpooper avatar vsopvsop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docpad-plugin-cleanurls's Issues

TypeError: Object -3 has no method 'bind'

I've been getting this error inside cleanurls. I have no idea why. The key with a value of 3 is _curOpen.

error: Something went wrong with the action
error: An error occured:
TypeError: Object -3 has no method 'bind'
    at Object.<anonymous> (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad-plugin-cleanurls/node_modules/safefs/out/lib/safefs.js:119:27)
    at Object.<anonymous> (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad-plugin-cleanurls/node_modules/safefs/out/lib/safefs.js:125:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad-plugin-eco/node_modules/eco/node_modules/coffee-script/lib/coffee-script/coffee-script.js:211:36)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at CleanUrlsPlugin.writeAfter (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad-plugin-cleanurls/out/cleanurls.plugin.js:66:18)
    at ambi (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:23:18)
    at Task.<anonymous> (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad/node_modules/event-emitter-grouped/out/lib/event-emitter-grouped.js:38:23)
    at ambi (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:23:18)
    at fire (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:159:23)
    at b (domain.js:183:18)
    at Domain.run (domain.js:123:23)
    at Task.fire (/Users/Thoughtworker/repos/opensource/zurb-foundation.docpad/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:166:25)
    at processImmediate [as _immediateCallback] (timers.js:330:15)

Publish new release

Was wondering if someone could create a new release of master branch and publish to npm. Thanks in advance :)

For the static environment output redirect documents

When inside the static environment we should:

  1. Set the outPath to the primary url physical location
  2. Set redirect documents to all the secondary urls physical locations

Currently with v2.3 with static environments we don't take custom urls into account at all.

hard-coded static env name

line 80 in your src plugin code there is :

Static

if 'static' in docpad.getEnvironments()

the static features could also be triggered by a custom parameters to not enforce static env name use. It could improve compatibility with other plugin using env specific rules.

Remove numerics from file name in the URL

Say I have 01-index.html, 02-getting-started.html and so on. Is there was to have clean urls emit urls like /index and /getting-started instead of /01-index and /02-getting-started ?

I can remove the numeric from file and have the order in metadata instead which will avoid this issue. But having them on the file makes it easier to glean info about which file goes first and so on.

Maybe its possible via advancedRedirect rules? Or maybe there is an even simpler way?

Not compatible with docpad-plugin-paged

Not compatible with docpad-plugin-paged

Just an FYI, paged plugin uses the base file name and extension to generate paginated pages. I'll look into fixing one or the other.

Generate custom url output within coffee configuration

Is it possible to output site pages inside a directory with the "page name" and an "index.html" file inside? This would provide a clean url with a "/" ending in the url address.

Python's Pelican static site generator uses a config file to specify this function simply like so:
PAGE_SAVE_AS = '{slug}/index.html'

{slug} would be the page name used in the markdown file.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.