Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Practical Hexo: A Hands-On Introduction to Building Blogs Using the Hexo Framework
Practical Hexo: A Hands-On Introduction to Building Blogs Using the Hexo Framework
Practical Hexo: A Hands-On Introduction to Building Blogs Using the Hexo Framework
Ebook393 pages3 hours

Practical Hexo: A Hands-On Introduction to Building Blogs Using the Hexo Framework

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Leverage the power of Hexo to quickly produce static blog sites that are efficient and fast. This project-oriented book simplifies the process of setting up Hexo and  manipulating content, using little more than a text editor and free software. It will equip you with a starting toolset that you can use to develop future projects, incorporate into your existing workflow and allow you to take your websites to the next level.

Hexo is based on JavaScript and Node.js, two of the biggest tools available for developers: you can enhance, extend, and configure Hexo as requirements dictate. With Hexo the art of possible is only limited by the extent of your imagination and the power of JavaScript and Node.js.

Practical Hexo gets you quickly acquainted with creating and manipulating blogs using a static site generator approach. You will understand how to use the Hexo framework to rapidly create and deploy blogs that are performant, with the minimum of fuss, then extend and customize your content using plugins and themes. You will work through some practical projects to help solidify your skills, and put them into practice.

What You'll Learn

  • Implement the Hexo framework in a project
  • Customize and extend content
  • Consider maintenance requirements and how they compare to other blog systems
  • Apply skills to practical, real-world projects
  • Create themes and build plugins
  • Make changes using other tools and libraries.

Who This Book Is For

Website developers who are already familiar with JavaScript and keen to learn how to leverage the Hexo framework. 

Agile development team members, where time is of the essence to deliver results quickly. 

Developers who want to focus on simplicity, to produce efficient and properly optimized content in modern browsers using tools already in their possession.

LanguageEnglish
PublisherApress
Release dateJul 29, 2020
ISBN9781484260890
Practical Hexo: A Hands-On Introduction to Building Blogs Using the Hexo Framework

Read more from Alex Libby

Related authors

Related to Practical Hexo

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Practical Hexo

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Practical Hexo - Alex Libby

    © Alex Libby 2020

    A. LibbyPractical Hexohttps://doi.org/10.1007/978-1-4842-6089-0_1

    1. Getting Started

    Alex Libby¹ 

    (1)

    RUGBY, UK

    Cast your mind back to the 1990s. Anyone remember the likes of Usenet, CompuServe, bulletin boards, and the like? These online communities were very popular at the time and gave rise to what we now know as weblogs.

    The term blog was coined back in December 1997 by the developer Peter Merholz on his site, Peterme.com – who would know how a subtle but important change in how weblog would be articulated would make such an impact on developers worldwide? He simply said we blog, instead of weblog, sometime during 1999, and the rest we say is history…

    But I digress – time to come back to reality! Blogging as an activity has become extremely popular over the years, with people posting articles on a host of different subjects, from their daily diary through to recipes for making cakes and beyond. There are a host of different tools available to create blogs, such as WordPress, Joomla, or even online tools such as Wix.com – all are perfectly valid tools, and each serves its own purpose.

    So, what’s new…?

    That is a good question – one might think that with all of the tools available, we wouldn’t need to change the status quo, right? After all, we can spin up a WordPress site for free, and with only a modest outlay and plenty of time, we can begin to create our masterpiece.

    Well, I hate to disappoint, but there are several good reasons why we need to change – they are:

    Simplicity

    Speed

    Security

    Okay, so the triple-S factor isn’t something I’ve coined deliberately, but there is a good reason for this: current blogging tools just aren’t as effective as they should be! Let me explain what I mean.

    In this modern age of technology, customers are increasingly demanding speed and simplicity; the pressure is absolutely on us as developers to create apps that are performant and don’t require an advanced degree in physics to work out how to use them! Blogging tools such as WordPress work very well in their own right, but are not the most efficient. I suspect a part of this is down to the amount of functionality they offer, but they also suffer from one other problem: databases.

    Yes, odd as it may seem, but databases are an essential part of the architecture that makes up a blog; we need them to store information, yet a badly configured database can be slow and inefficient and increase the risk that our site will be hacked. So, what can we do? Is there an alternative?

    Introducing Hexo

    Absolutely! Let me introduce you to the world of static site generators (or SSGs) , and – more to the point – one of them: Hexo! So, what are SSGs?

    Put simply, SSGs take a set of markdown instructions and convert or transpile them into static sites. Gone are the days of having to communicate with databases; SSGs do away with them by default, which results in content that can be rendered faster, is more performant, and is equally more secure. Hexo is one such example of an SSG; all use similar techniques, but Hexo has been designed to focus specifically on creating blogs.

    Hexo was originally introduced in 2012, by the developer Tommy Chen, and is available from https://www.hexo.io – since then, it has powered thousands of blogs worldwide, so has become very stable for production use. It’s very easy to set up, with only two key software dependencies that have to be installed as part of the process. Let’s take a look at what these are in detail, before we go ahead with setting up Hexo.

    Some housekeeping tasks

    So, we’ve been introduced to Hexo, but before we get stuck into setting it up and create our first blog, there is a little housekeeping we need to do! Don’t worry, it’s nothing onerous: we have to download a couple of packages, among other things. Let’s take a look at what we need to do.

    The first task is to make sure you have a decent text editor – I’m sure you will have something suitable already, but just in case you don’t, then there are plenty of options available! Here are a few you can try:

    Atom is an open source cross-platform editor available from https://atom.io; it has Git control built in by default, which will be particularly useful for working with Hexo!

    You can also try out Visual Studio Code, available from https://code.visualstudio.com/ – this is open source and works across Windows, Linux, and Mac platforms.

    If you prefer a commercial offering, then give Sublime Text a try – it is available for Windows, Linux, and Mac and can be downloaded from https://www.sublimetext.com/3.

    Most editors will allow you to work with Hexo – it’s a case of finding one you like the look and feel of and which fits in with your own development environment.

    We will also need somewhere to host the files we generate: as you will see later, the content is already served via localhost, but this is intended to be in a development capacity. I would recommend availing yourself with a local web server such as Apache (this should already be available on Mac and Linux platforms by default); for Windows users, you can use something like WampServer (https://www.wampserver.com/en/), if you don’t already have an existing local server installed and ready for use.

    You can of course use any free web space, but you may not want to release content into the wild if it is publicly available!

    Next up, we need to download Git for your platform – this is used to get packages such as starter themes or plugins:

    For Windows users, Git can be downloaded from https://git-scm.com/download/win.

    If you are a Mac user, you can install it using Homebrew, MacPorts, or the Git installer from https://sourceforge.net/projects/git-osx-installer/.

    For Linux users on Ubuntu or Debian, use this command: sudo apt-get install git-core.

    We also need Node.js – we’ll use this for a host of tasks, such as downloading and installing plugins and themes or creating build files for deployment. Node.js is available for download from https://nodejs.org/en/download/ – you will need to download the relevant version for your platform.

    I would recommend choosing the option marked Recommended for Most Users – this is sufficient for our needs.

    Okay, we have our dependencies in place; let’s move on and get everything installed and ready for use.

    Getting the dependencies installed

    We’ve downloaded our two dependencies for our platform, so it’s time to get them installed – both are straightforward, but do require us working through a few steps, particularly with Git! Let’s take a look at the steps needed in more detail, as part of our first exercise.

    The instructions for each exercise will assume you are using Windows: this is the author’s native platform. If you are using Linux or macOS as your preferred platform, you may need to adjust the steps to suit.

    Installing Our Dependencies

    To get started with installing Hexo, go ahead with these steps – I will assume you’ve downloaded the relevant packages as outlined earlier:

    1.

    We’ll begin by installing Git as an administrator. For this, go ahead and double-click the installer you’ve downloaded; click Yes to install.

    2.

    Click Next and then Next again, to accept the default settings on the Select Components screen.

    3.

    On the Choose the default Editor… screen, go ahead and pick which editor you want to use; then click Next.

    The available options will depend on what editors you have installed – select the most appropriate for your platform.

    4.

    On the next screen, choose the option Git from the command line and also 3rd-party software; then click Next.

    5.

    On the Choosing HTTPS transport backend screen, choose Use the OpenSSL library; then click Next.

    6.

    For the Configuring the line ending conversions, select Checkout Windows-style, commit Unix-style line endings; then click Next.

    7.

    We’re almost done. For the next screen, choose Use Windows’ default console window; then click Next.

    8.

    On the final selection screen, click the checkboxes on the top two options; then click Install.

    9.

    Git will now install – once done, it will prompt you if you want to see the release notes. Deselect this checkbox, and then click Next, to close the installer.

    10.

    We now need to install Node.js. Go ahead and double-click the installer; click Yes to accept the admin privileges.

    11.

    When prompted, go ahead and click Next; then select the checkbox to accept the license terms.

    12.

    Keep clicking Next to accept the default options and Finish at the end – this is sufficient for our needs.

    Phew! We’re done with setting up our development environment; time now for the all-important part: setting up Hexo! Thankfully, this is easier to install by comparison. Let’s dive in and take a look at the steps we need to work through to get Hexo set up for use.

    Installing Hexo

    We’re well on the way to getting our first blog set up – to install Hexo, we need to use the command line. It’s a one-liner command which will make Hexo available across our entire PC. There is a good reason for doing this; before we explain why, let’s get Hexo installed.

    Demo: Installing Hexo

    To get Hexo installed, follow these steps:

    1.

    Fire up Node.js terminal session, then enter npm install hexo-cli -g, and press Enter.

    2.

    It will run through the installation and end up with something similar to Figure 1-1.

    ../images/496646_1_En_1_Chapter/496646_1_En_1_Fig1_HTML.jpg

    Figure 1-1

    Installing Hexo via the command line

    3.

    Leave the terminal session window open – we’re going to make use of it very shortly, when we create our first blog.

    For existing users of Node, you may spot that we’ve not created a package.json file; this is deliberate. This will be created later during the install process automatically; setting up a file now can result in NPM throwing an error.

    At this stage, we now have Hexo installed. We’re ready to create our first blog; that was painless, right? Creating a blog is equally easy to do. Let’s take a look at the steps required in more detail.

    Creating our first example blog

    Okay, so we’re ready to set up our first blog: for this, we’ll use the name Hexo as our example. Don’t worry at this stage if you’re not entirely sure what’s happening: we will absolutely go through everything, but for now, let’s simply run through the steps to set up our blog as part of the next exercise.

    Creating Our First Example Blog

    Go ahead and follow these steps to create our first blog:

    1.

    First, revert back to the Node.js terminal session we had open from the previous exercise.

    2.

    At the prompt, enter Hexo init myblog and press Enter, to create our first blog.

    3.

    You will see various steps being performed (Figure 1-2) – when you see INFO Start blogging with Hexo!, enter cd myblog to change to the myblog folder, and press Enter.

    ../images/496646_1_En_1_Chapter/496646_1_En_1_Fig2_HTML.jpg

    Figure 1-2

    Running through the install for our first blog…

    4.

    At the prompt, enter npm install to set up our first blog – NPM will install some additional packages. You may see warnings from fsevents when running on Windows – fsevents is a Mac-only package, so these can be ignored.

    5.

    At the prompt, enter hexo server and then wait a couple of moments; it will indicate that you can now browse to your first site – fire up your browser, and head over to http://localhost:4000 to see our site (Figure 1-3).

    ../images/496646_1_En_1_Chapter/496646_1_En_1_Fig3_HTML.jpg

    Figure 1-3

    Our first blog, up and running

    Congratulations if you made it this far. It might seem a lot, but trust me we only need to do the Git and Node.js installs once! With our two dependencies done, we only need to run the last part for any blog project we create.

    In the meantime, we’ve run through what is arguably the most important part of working with Hexo – even though it looks relatively straightforward, it covers a few useful points that we should be aware, so let’s dive in and take a look at what we’ve done in more detail.

    Exploring the changes in detail

    So, what did we do? It might seem a lot of steps, but as mentioned just now, most of them are only needed for the first time! Once that is done, then setting up a basic blog is a much quicker process. Let’s take a look in more detail.

    We kicked off by running through the install process for the two dependencies needed by Hexo, namely, Git and Node.js; we ran through the installation steps for Windows, but the choices will be similar for Linux and Mac platforms.

    Next up, we then installed Hexo – we first installed Hexo as a global package, to allow us to create each blog site in separate folders. We then ran the init command to create our first blog and initialize the folder structure for it; this was completed by running the standard npm install to set up any missing dependencies. As the final step, we then ran the hexo server command to compile our code into static files and allow us to run the site in a browser window.

    Okay, so we’ve built our first blog – granted, it’s not going to win any style awards anytime soon, but hey, we must start somewhere! Trouble is I’ll bet you’re going to be a little unsure how all of this works, right? No problem – it’s important to understand how Hexo works, so we can fine-tune it to our needs. With that in mind, let’s dive in and take a look at the innards in more detail.

    Understanding how Hexo operates

    Now that we’ve built our first blog, do one thing for me – take a look at the compiled source. You can do this using Developer Tools in a browser – in Chrome, use Ctrl+Shift+I for Windows or Linux, or Cmd+Shift+I for Macs. Go ahead and have a look around, particularly at the Network tab. Have you noticed anything in particular?

    Hopefully, you will see a site that appears to use standard HTML markup, and you might expect to see network calls to databases – in this case, you will definitely see the former, but not the latter! What gives…?

    Well, we touched on how Hexo doesn’t need databases by default – one of the ways it gets around not needing them is to use markdown files. These are just plain text files with the .md extension; Hexo uses these for content and YAML files for configuring each site. This is an important part of how Hexo works – to understand why, let’s take a look at both the content and configuration files, starting first with content.

    Breaking apart the folder structure

    When we created our first blog, we set up a number of files and folders as part of the install process. Well, now would be a good time to take another look at the contents of our blog folder – go ahead and open it up in your file manager or explorer. Once open, you should see something akin to the screenshot shown in Figure 1-4.

    ../images/496646_1_En_1_Chapter/496646_1_En_1_Fig4_HTML.jpg

    Figure 1-4

    The folder (and file) structure of a Hexo blog

    Inside it, we can see a handful of folders and files. We have the typical node_modules folder which we get with any Node.js-based installation. I see a package.json there too, as well as three folders. What do they all do…? It’s a good question. Let’s start first with the folders.

    Any Hexo blog will be made up of three folders (plus the standard node_modules folder) – they are all listed in Table 1-1.

    Table 1-1

    The folder structure for a Hexo blog

    Hang on though. That’s not all I see there. What about the files we mentioned just now? Yes, that’s right. In addition, there are two files that are critical to the operation of any Hexo blog; they are _config.yml and package.json. The first takes care of configuring how your blog should be assembled; the latter contains the NPM dependencies required for Hexo to operate. Let’s take a look at both in turn, starting with the _config.yml file.

    You may also see a package-lock.json file appear too – this can be ignored. It contains an exact, versioned tree of dependencies, rather than the wildcard version you see in the standard package.json file.

    The _config.yml file

    The _config.yml file is where the magic happens – if you go ahead and open it up in a text editor, you will see something akin to the screenshot shown in Figure 1-5.

    ../images/496646_1_En_1_Chapter/496646_1_En_1_Fig5_HTML.jpg

    Figure 1-5

    An extract from our _config.yml file

    Inside this file, you will see a host of different settings – these are all used to configure our blog. In our case, we can see that our site’s title is Hexo, the site’s core language is English, and the author is named John Doe. Take a look at our blog from the previous demo – you will be able to see the site title top-center, with the author’s name down near the bottom-left corner.

    Using an alternative configuration file

    In most instances, using a standard _config.yml file will satisfy our needs; however, it’s worth noting that Hexo does allow us to use a custom configuration file if needed, using the –-config flag.

    To use it, we need to have either a YAML or JSON file (or it can be multiple files, in a comma-separated list with no spaces). We then run the hexo server command as before, but this time append the --config parameter, similar to the examples shown in the following:

    # use 'myconfig.yml' in place of '_config.yml'

    $ hexo server --config myconfig.yml

    # use 'myconfig.yml' & 'myconfig2.json', prioritizing 'custom2.json'

    $ hexo server --config myconfig.yml,myconfig2.json

    The # symbol represents a comment line in YAML files and will be ignored when processed.

    If we decide to use multiple configuration files, these are automatically merged together by Hexo into a _multiconfig.yml file . The important thing to note is that if the files contain duplicate settings, then the latter values take precedence. So, if we specified foo: bar in custom.yml, but foo: dinosaur in custom2.json, _multiconfig.yml would contain foo: dinosaur.

    Overriding a theme configuration

    Although we talk about using _config.yml files as our means of configuring a site, in reality these are really config files for themes that we use in each project. This is perfect if we’re running a single blog, but what if we’re building multiple blogs?

    The typical response would be to edit the config files for each blog as needed or potentially fork them and maintain our own custom settings, but that is time consuming, particularly if we’re using similar settings across multiple blogs!

    Instead, we can set up a primary _config.yml file and then override individual settings as needed in each project. It means we will need to maintain two configuration files per project, but this is a small price to pay when

    Enjoying the preview?
    Page 1 of 1