Skip to content

Commit

Permalink
TOC page updates:
Browse files Browse the repository at this point in the history
* use uuid() to generate page id
* only generate TOC if there's more than one page
* add to README
  • Loading branch information
danburzo committed Oct 30, 2018
1 parent db08be0 commit baa5063
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -74,6 +74,7 @@ The `pdf`, `epub`, and `html` commands have these options:
| `--css` | Additional CSS styles you can pass from the command-line to override the default/custom stylesheet styles |
| `--no-amp` | Don't prefer the AMP version of the web page |
| `--debug` | Print more detailed information |
| `--toc` | Include a Table of Contents page |

## Examples

Expand Down
13 changes: 6 additions & 7 deletions index.js
Expand Up @@ -10,6 +10,7 @@ const css = require('css');
const slugify = require('slugify');
const Readability = require('./vendor/readability');
const pkg = require('./package.json');
const uuid = require('uuid/v1');

const spinner = ora();

Expand Down Expand Up @@ -108,11 +109,7 @@ async function cleanup(url, options) {
}).parse();

spinner.succeed();
const _id = Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substr(2, 10);
return { ...parsed, _id, url };
return { ...parsed, id: `percollate-page-${uuid()}`, url };
} catch (error) {
spinner.fail(error.message);
throw error;
Expand All @@ -129,7 +126,7 @@ async function bundle(items, options) {

const stylesheet = resolve(options.style || './templates/default.css');
const style = fs.readFileSync(stylesheet, 'utf8') + (options.css || '');
const generateToc = options.toc;
const use_toc = options.toc && items.length > 1;

const html = nunjucks.renderString(
fs.readFileSync(
Expand All @@ -140,7 +137,9 @@ async function bundle(items, options) {
items,
style,
stylesheet, // deprecated
generateToc
options: {
use_toc
}
}
);

Expand Down
7 changes: 6 additions & 1 deletion templates/default.css
Expand Up @@ -84,7 +84,12 @@ pre code {
display: none;
}

nav.toc {
/*
Table of Contents page
----------------------------------------------------
*/

.toc {
page-break-after: always;
}

Expand Down
14 changes: 8 additions & 6 deletions templates/default.html
Expand Up @@ -10,19 +10,21 @@
</head>
<body>

{% if generateToc %}
<nav class="toc">
<h1>Items</h1>
<ol>
{% if options.use_toc %}
<nav class='toc'>
<h1 class='toc__title'>Table of Contents</h1>
<ol class='toc__list'>
{% for item in items %}
<li class="toc__line"><a href="#{{ item._id }}">{{ item.title }}</a></li>
<li class='toc__list-item'>
<a href="#{{ item.id }}" class='no-href'>{{ item.title }}</a>
</li>
{% endfor %}
</ol>
</nav>
{% endif %}

{% for item in items %}
<article id="{{ item._id }}" class='article'>
<article id='{{ item.id }}' class='article'>
<header class='article__header'>
<h1 class='article__title'>
{{ item.title }}
Expand Down

0 comments on commit baa5063

Please sign in to comment.