Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add template option #30

Merged
merged 5 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ and
gitbook install
```

## Options

You can put your template into `book.js`(`book.json`) by `template` option.

```js
const fs = require("fs");
module.exports = {
"gitbook": "3.x.x",
"title": "gitbook-plugin-include-codeblock example",
"plugins": [
"include-codeblock"
],
"pluginsConfig": {
"include-codeblock": {
"template": fs.readFileSync(__dirname + "/user-template.hbs", "utf-8")
}
}
};
```

See [template/](template/) and [example/](example/) for details.

## Usage

**fixtures/test.js**
Expand Down
13 changes: 13 additions & 0 deletions example/book.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require("fs");
module.exports = {
"gitbook": "3.x.x",
"title": "gitbook-plugin-include-codeblock example",
"plugins": [
"include-codeblock"
],
"pluginsConfig": {
"include-codeblock": {
"template": fs.readFileSync(__dirname + "/user-template.hbs", "utf-8")
}
}
};
7 changes: 0 additions & 7 deletions example/book.json

This file was deleted.

3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"prepublish": "gitbook install",
"start": "gitbook serve",
"build": "gitbook build"
"build": "gitbook build",
"test": "npm rm gitbook-plugin-include-codeblock && npm i && npm run build"
},
"keywords": [
"gitbook"
Expand Down
5 changes: 5 additions & 0 deletions example/user-template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> <a id="{{{id}}}" href="{{originalPath}}">{{fileName}}</a>

``` {{lang}}
{{{content}}}
```
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
"engines": {
"gitbook": "*"
},
"gitbook": {
"properties": {
"template": {
"type": "string",
"description": "Template string",
"required": false
}
}
},
"dependencies": {
"language-map": "^1.1.1",
"handlebars": "^4.0.5"
Expand Down
5 changes: 3 additions & 2 deletions src/include-codeblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ var path = require('path');
import {parse} from "./parser"
module.exports = {
hooks: {
"page:before": function (page) {
"page:before": function(page) {
var options = this.options.pluginsConfig["include-codeblock"];
var pageDir = path.dirname(page.rawPath);
var results = parse(page.content, pageDir);
var results = parse(page.content, pageDir, options);
results.forEach(result => {
var {target, replaced} = result;
page.content = page.content.replace(target, replaced);
Expand Down
48 changes: 35 additions & 13 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const markdownLinkFormatRegExp = /\[([^\]]*?)\]\(([^\)]*?)\)/gm;
/**
* A counter to count how many code are imported.
*/
var codeCounter = (function () {
var codeCounter = (function() {
var count = 0;
return function () {
return function() {
return count++;
}; // Return and increment
})();
Expand Down Expand Up @@ -57,6 +57,8 @@ export function parseVariablesFromLabel(label) {
var keyvals = {
"title": undefined,
"id": undefined,
"class": undefined,
"name": undefined,
"marker": undefined
};
Object.keys(keyvals).forEach(key => {
Expand All @@ -80,9 +82,10 @@ export function parseVariablesFromLabel(label) {
* @param {string} filePath
* @param {string} originalPath
* @param {string} label
* @param {string} template
* @return {string}
*/
export function embedCode({lang, filePath, originalPath, label}) {
export function embedCode({lang, filePath, originalPath, label, template}) {
const code = fs.readFileSync(filePath, "utf-8");
const fileName = path.basename(filePath);
const keyValueObject = parseVariablesFromLabel(label);
Expand All @@ -97,7 +100,14 @@ export function embedCode({lang, filePath, originalPath, label}) {
const marker = getMarker(keyValueObject);
content = removeMarkers(markerSliceCode(code, marker));
}
return generateEmbedCode(keyValueObject, lang, fileName, originalPath, content);
return generateEmbedCode({
keyValueObject,
lang,
fileName,
originalPath,
content,
template
});
}

/**
Expand All @@ -107,31 +117,42 @@ export function embedCode({lang, filePath, originalPath, label}) {
* @param {string} fileName
* @param {string} originalPath
* @param {string} content
* @param {string} template handlebars template
* @return {string}
*/
export function generateEmbedCode(keyValueObject, lang, fileName, originalPath, content) {
export function generateEmbedCode({
keyValueObject,
lang,
fileName,
originalPath,
content,
template
}) {
const count = hasTitle(keyValueObject) ? codeCounter() : -1;
// merge objects
// if keyValueObject has `lang` key, that is overwrited by `lang` of right.
const context = Object.assign({}, keyValueObject, {lang, fileName, originalPath, content, count});

// if has the title, display the title with an anchor link.
// Mix of handlerbars and markdown templates to handle file types.
const source = fs.readFileSync(path.join(__dirname, "..", "template", "default-template.hbs"), "utf-8");

const template = Handlebars.compile(source);
// compile template
const handlebars = Handlebars.compile(template);
// compile with data
return template(context);
return handlebars(context);
}


const defaultOptions = {
template: fs.readFileSync(path.join(__dirname, "..", "template", "default-template.hbs"), "utf-8")
};
/**
* generate code with options
* @param {string} content
* @param {string} baseDir
* @param {{template?: string}} options
* @return {Array}
*/
export function parse(content, baseDir) {
export function parse(content, baseDir, options = {}) {
const results = [];
const template = options.template || defaultOptions.template;
let res;
while (res = markdownLinkFormatRegExp.exec(content)) {
const [all, label, originalPath] = res;
Expand All @@ -143,7 +164,8 @@ export function parse(content, baseDir) {
lang,
filePath: absolutePath,
originalPath: originalPath,
label
label,
template
});
results.push({
target: all,
Expand Down