npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

docpad-plugin-posteditor

v2.1.2

Published

Docpad plugin that allows the saving and loading of posts

Downloads

3

Readme

Post Editor Plugin for DocPad

Build Status NPM version NPM downloads

Docpad plugin that allows the saving and updating of posts/articles. It provides the basis for an admin/CMS interface.

Screen shot

It manages the saving and updating of posts and loading of existing posts for an editor interface. It does not provide the editor interface. It is editor interface agnostic. It assumes you have provided an editor on the client side with client code to request existing articles via the plugin's configured load url and save articles via the configured save url. The editor can be a simple textarea element or a full-blown text editor like CKeditor. The choice is yours.

The plugin will also save old versions of an article to a "versions" folder - thus maintaining a history of each post edited.

The plugin also (optionally) manually triggers the regeneration of a saved article and the home page. This is especially useful if you want to stop Docpad from regenerating the whole website on each save. It is also particularly relevant if you are storing articles and documents outside of the Docpad root/repo.

The plugin is designed to work with an existing authentication/login system - although it will work without any authentication as well. In truth, it is built to work in tandem with my other plugin, docpad-plugin-authentication. User name and user id is added to the metadata of the document saved.

Default configuration

    config:
        #custom metadata fields outside of the standard title,layout and tags.
        #Prevents the arbitrary addition of extra (unexpected) metadata fields.
        #A validation of user input.
        customFields: []
        #Location for version files. This also needs to be outside of the
        #docpad src folder so that version files are not generated or copied to the
        #out folder
        dataPath: null

        defaultLayout: 'post'
        postCollection: 'posts'
        defaultSavePath: 'posts'

        #retain previous version of post that is edited and move it
        #to a 'version' folder
        saveVersions: true
        loadURL: '/load/:docId'
        saveURL: '/save'
        #URL to manually trigger regeneration of post
        generateURL: '/generate'
        #whether to send rendered HTML to the client or raw markdown
        #wysiwyg editors will want the rendered HTML and then this edited content
        #will have to be converted back to markdown for saving back to the server
        sendRenderedContent: true
        #manually trigger post regeneration rather than let docpad do it automatically.
        #usefull when there is a custom regeneration path - ie when documents are stored
        #outside of the docpad application root/repo. In such a case docpad will regenerate
        #ALL documents regardless if standalone=true or referencesOthers=false.
        handleRegeneration: true
        #if handleRegeneration=true then force the home page to be generated after each post update
        generateHomePage: true

        sanitize: require('bleach').sanitize

        #remember to include the space character but not the tab (\t) or newline (\n)
        titleReg: /[^A-Za-z0-9_.\-~\?! ]/g

Example JS for loading an article/post

    function getPost(docId,slug) {
        var id = docId ? docId : slug;
        $.get('/load/' + id)
            .done(function (data) {
                $('#post-title').val(data.title);
                $('#slug').text(data.slug);
                $('#feature-img').attr('src', data.img);
                $('#docId').text(data.docId);
                var tags = $('#tags');
                for (var i = 0; i < data.tags.length; i++) {
                    tags.append('<li><a class="icon-cancel-circle"></a>' + data.tags[i] + "</li>");
                }

                CKEDITOR.instances.editor1.setData(data.content, {
                    callback: function () {
                        var btn = $('.cke_voice_label:contains("save")').parent();
                        btn.addClass('save-btn');
                    }
                });

            });
    }

Example JS for saving an article/post

    function postData() {
        var txt = downshow(CKEDITOR.instances.editor1.getData());
        var title = $('#post-title').val();
        var slug = $('#slug').text();
        var docId = parseInt($('#docId').text());
        var inputs = $('#tags li');
        var tags = [];
        inputs.each(function () {
            tags.push($(this).text());
        });

        //check we actually have something written
        //and that we have a title
        if (txt.length < 10) {
            alert("Write something first!");
        } else if (title.length < 2) {
            $('#title-callout').fadeIn();
            var titleCallout = function () {
                $('#title-callout').fadeOut();
                $(this).unbind('click', titleCallout);
            };
            $('#post-title').click(titleCallout);


        } else {
            $('#edit-loader').show();

            var obj = {
                content: txt,
                title: title,
                slug: slug,
                tags: tags,
                docId: docId
            };

            $.post('/save', obj)
                .done(function () {
                    $('#edit-msg').show();
                    $('#edit-loader').fadeOut(1000, function () {
                        $('#edit-msg').fadeOut(1000);
                    });
                })
                .fail(function (xhr, err, msg) {
                    $('#edit-loader').fadeOut(500);
                    $('#edit-msg').fadeOut(500);
                    alert(msg);
                });
        }

    }

Regeneration of saved posts

The plugin works by saving edited or new articles/posts directly to the file system. It then uses Docpad's inbuilt system for regenerating a page when the corresponding source file is modified. To give finer control over the regeneration process the plugin will manually trigger the regeneration of the document just updated. This works best when Docpad is run with the docpad server command rather than docpad run, otherwise the regeneration process will run twice. If you want docpad to handle all regeneration on its own (as normal), then set the config option handleRegeneration: false.

Converting HTML to Markdown

The plugin expects Markdown to be sent to the server. Typically, however, the general user will not know Markdown. Fair enough. So typically 'wiswyg' editors (like CKeditor) will work with HTML. So, to facilitate this, the plugin will send the rendered content (ie HTML) to the client. To send the content back to the server we need to convert the HTML back to Markdown. To do this I use the Downshow js library.

License

Licensed under the incredibly permissive MIT license

Copyright © 2016+ Steve McArthur [email protected] (http://www.stevemcarthur.co.uk)