-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add include tag plugin and parseFilters options
- Loading branch information
Showing
10 changed files
with
191 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "magicbook", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "An lightweight and scalable docs system for markdown, text or other.", | ||
"homepage": "http://ipluser.github.io/magicbook/", | ||
"keywords": [ | ||
|
@@ -16,4 +16,4 @@ | |
"authors": [ | ||
"Plus <[email protected]>" | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
;(function (global, $, Magicbook) { // eslint-disable-line | ||
|
||
var TAG_NAME = 'include'; | ||
var defaults = { | ||
openTag: '{%', | ||
closeTag: '%}' | ||
}; | ||
|
||
function tempate(cfg) { | ||
var options = $.extend({}, defaults, cfg); | ||
var openTag = options.openTag; | ||
var closeTag = options.closeTag; | ||
|
||
function parse(chunk) { | ||
var tokens = chunk.trim().replace(/\s+/g, ' ').split(' '); | ||
var tag = tokens[0]; | ||
var len = tokens.length; | ||
|
||
if (len <= 1 || tag !== TAG_NAME) { | ||
return openTag + chunk + closeTag; // | ||
} | ||
|
||
var content; | ||
var url = tokens[1]; | ||
var _url = url.length > 2 ? url.substring(1, url.length - 1) : url; | ||
_url = _url.search(/\:\/\//) !== -1 ? _url : this.relativeCurrentUrl(_url); | ||
|
||
$.ajax({ | ||
url: _url, | ||
async: false, | ||
success: function templateIncludeTagSuccess(data) { | ||
content = data; | ||
}, | ||
error: function templateIncludeTagError() { | ||
content = [TAG_NAME, ': \'', url, '\' not found'].join(' '); | ||
} | ||
}); | ||
|
||
return content; | ||
} | ||
|
||
return function render(source) { | ||
var blocks = (source || '').split(openTag); | ||
var len = blocks.length; | ||
var index; | ||
|
||
if (len <= 1) { | ||
return source; | ||
} | ||
|
||
var out = ''; | ||
for (index = 0; index < len; index++) { | ||
var block = blocks[index]; | ||
var chunks = block.split(closeTag); | ||
|
||
if (chunks.length === 1) { | ||
out += chunks[0]; | ||
continue; | ||
} | ||
|
||
var content = parse.call(this, chunks[0]); | ||
|
||
out += content; | ||
out += chunks[1]; | ||
} | ||
|
||
return out; | ||
}; | ||
} | ||
|
||
Magicbook.potion.enableIncludeTag = function enableIncludeTag(cfg) { | ||
var self = this; | ||
|
||
self.config.parseFilters.push({ | ||
before: tempate(cfg) | ||
}); | ||
|
||
self.templateIncludeTag = tempate(cfg); | ||
}; | ||
})(window, jQuery, Magicbook); // eslint-disable-line |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
public/doc/plugins/components/js/magicbook-plugin-include.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## magicbook-plugin-include.js | ||
The **magicbook-plugin-include** supports `include` template tag in document. | ||
|
||
### usage | ||
#### Import Dependencies | ||
To use magicbook-plugin-include, you’ll need to make sure `magicbook` are included. | ||
```html | ||
<script src="plugins/components/js/magicbook-plugin-include.min.js"></script> | ||
``` | ||
|
||
#### To use include tag | ||
```markup | ||
{% include './test.md' %} | ||
``` | ||
|
||
#### Initialize | ||
```js | ||
{magicbook instance}.enableIncludeTag({ | ||
openTag: '{%', | ||
closeTag: '%}' | ||
}); | ||
|
||
// or | ||
|
||
{magicbook instance}.enableIncludeTag(); | ||
``` | ||
|
||
### configuration | ||
#### openTag | ||
Open controls for include tag. | ||
|
||
##### default | ||
`{%` | ||
|
||
|
||
#### closeTag | ||
Close controls for include tag. | ||
|
||
##### default | ||
`%}` | ||
|
||
|
||
### methods | ||
#### templateIncludeTag | ||
Parse include tag. | ||
|
||
##### parameters | ||
| name | description | | ||
|-----------|------------------| | ||
| source | document content | |