From 44ac51b9c21d2752f39ad6108d0091a6fff89c29 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Sun, 20 Nov 2016 15:56:37 +0800 Subject: [PATCH] first commit --- .eslintrc | 6 ++++ .gitignore | 3 ++ LICENSE | 21 ++++++++++++ README.md | 33 +++++++++++++++++++ docs/404.html | 12 +++++++ docs/README.md | 1 + lib/docsify.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++ lib/docsify.min.js | 1 + package.json | 38 +++++++++++++++++++++ rollup.config.js | 13 ++++++++ src/ajax.js | 10 ++++++ src/index.js | 35 ++++++++++++++++++++ src/render.js | 26 +++++++++++++++ test/404.html | 12 +++++++ test/README.md | 27 +++++++++++++++ test/docsify.js | 1 + test/server.js | 8 +++++ test/theme.md | 1 + test/themes | 1 + themes/vue.css | 0 20 files changed, 331 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 docs/404.html create mode 100644 docs/README.md create mode 100644 lib/docsify.js create mode 100644 lib/docsify.min.js create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 src/ajax.js create mode 100644 src/index.js create mode 100644 src/render.js create mode 100644 test/404.html create mode 100644 test/README.md create mode 120000 test/docsify.js create mode 100644 test/server.js create mode 100644 test/theme.md create mode 120000 test/themes create mode 100644 themes/vue.css diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..d5691ad --- /dev/null +++ b/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": ["vue"], + "globals": { + "XMLHttpRequest": true + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b915831 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.log +node_modules +yarn.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4808f79 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 cinwell.li + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0eab429 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# docsify +[![Build Status](https://travis-ci.org/QingWei-Li/docsify.svg?branch=master)](https://travis-ci.org/QingWei-Li/docsify) +[![Coverage Status](https://coveralls.io/repos/github/QingWei-Li/docsify/badge.svg?branch=master)](https://coveralls.io/github/QingWei-Li/docsify?branch=master) +[![npm](https://img.shields.io/npm/v/docsify.svg)](https://www.npmjs.com/package/docsify) + +>🃏 A magical documentation site generator. + +## Features +- Easy and lightweight +- Custom themes and plugins + +## Quick start +Such as [./docs](./docs), Just create `404.html` and `README.md` into `/docs`. + +404.html + +```html + + + + + + + + + + + +``` + + +## License +MIT diff --git a/docs/404.html b/docs/404.html new file mode 100644 index 0000000..bb8343c --- /dev/null +++ b/docs/404.html @@ -0,0 +1,12 @@ + + + + + docsify + + + + + + + diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..b6ba9f2 --- /dev/null +++ b/docs/README.md @@ -0,0 +1 @@ +# ahhh \ No newline at end of file diff --git a/lib/docsify.js b/lib/docsify.js new file mode 100644 index 0000000..a6360d9 --- /dev/null +++ b/lib/docsify.js @@ -0,0 +1,82 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('marked'), require('prismjs')) : + typeof define === 'function' && define.amd ? define(['marked', 'prismjs'], factory) : + (global.Docsify = factory(global.marked,global.Prism)); +}(this, (function (marked,Prism) { 'use strict'; + +marked = 'default' in marked ? marked['default'] : marked; +Prism = 'default' in Prism ? Prism['default'] : Prism; + +var ajax = function (url, options) { + if ( options === void 0 ) options = {}; + + var xhr = new XMLHttpRequest(); + + xhr.open(options.method || 'get', url); + xhr.send(); + + return { + then: function (cb) { return xhr.addEventListener('load', cb); } + } +}; + +var renderer = new marked.Renderer(); + +/** + * render anchor tag + * @link https://github.com/chjj/marked#overriding-renderer-methods + */ +renderer.heading = function (text, level) { + var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-'); + + return '' + text + + '' +}; + +marked.setOptions({ + highlight: function highlight (code, lang) { + return Prism.highlight(code, Prism.languages[lang]) + } +}); + +var render = function (content) { + return marked(content, { renderer: renderer }) +}; + +var Docsify = function Docsify (opts) { + if ( opts === void 0 ) opts = {}; + + Docsify.installed = true; + + this.dom = document.querySelector(opts.el || 'body'); + this.loc = document.location.pathname; + this.loc = this.loc === '/' ? 'README' : this.loc; + this.load(); +}; + +Docsify.prototype.load = function load () { + var this$1 = this; + + ajax(((this.loc) + ".md")).then(function (res) { + var target = res.target; + if (target.status >= 400) { + this$1.render('not found'); + } else { + this$1.render(res.target.response); + } + }); +}; + +Docsify.prototype.render = function render$1 (content) { + this.dom.innerHTML = render(content); +}; + +window.addEventListener('load', function () { + if (Docsify.installed) { return } + new Docsify(); +}); + +return Docsify; + +}))); diff --git a/lib/docsify.min.js b/lib/docsify.min.js new file mode 100644 index 0000000..1318efb --- /dev/null +++ b/lib/docsify.min.js @@ -0,0 +1 @@ +(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory(require("marked"),require("highlight.js")):typeof define==="function"&&define.amd?define(["marked","highlight.js"],factory):global.Docsify=factory(global.marked,global.hljs)})(this,function(marked,hljs){"use strict";marked="default"in marked?marked["default"]:marked;hljs="default"in hljs?hljs["default"]:hljs;var ajax=function(url,options){if(options===void 0)options={};var xhr=new XMLHttpRequest;xhr.open(options.method||"get",url);xhr.send();return{then:function(cb){return xhr.addEventListener("load",cb)},catch:function(cb){return xhr.addEventListener("error",cb)}}};var renderer=new marked.Renderer;renderer.heading=function(text,level){var escapedText=text.toLowerCase().replace(/[^\w]+/g,"-");return"'+text+""};var render=function(content){return marked(content,{renderer:renderer,highlight:function highlight(code){return hljs.highlightAuto(code).value}})};var Docsify=function Docsify(opts){if(opts===void 0)opts={};Docsify.installed=true;this.dom=document.querySelector(opts.el||"body");var loc="https://yanagieiichi.github.io/reciper/intro/README.md";this.load(loc)};Docsify.prototype.load=function load(loc){var this$1=this;ajax(""+loc).then(function(res){this$1.render(res.target.response)})};Docsify.prototype.render=function render$1(content){this.dom.innerHTML=render(content)};window.addEventListener("load",function(){if(Docsify.installed){return}new Docsify});return Docsify}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8530f1c --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "docsify", + "version": "0.0.0", + "description": "A magical documentation generator.", + "main": "index.js", + "files": [ + "lib", + "themes" + ], + "scripts": { + "build": "rollup -c && uglifyjs lib/docsify.js -o lib/docsify.min.js", + "dev": "nodemon -w src -x 'rollup -c && node test/server.js'", + "test": "eslint src server" + }, + "keywords": [ + "doc", + "docs", + "documentation", + "creator", + "generator" + ], + "author": "qingwei-li (https://github.com/QingWei-Li)", + "license": "MIT", + "devDependencies": { + "buffet": "^1.0.10", + "eslint": "^3.10.2", + "eslint-config-vue": "^2.0.1", + "eslint-plugin-vue": "^1.0.0", + "nodemon": "^1.11.0", + "rollup": "^0.36.3", + "rollup-plugin-buble": "^0.14.0", + "uglify-js": "^2.7.4" + }, + "dependencies": { + "marked": "^0.3.6", + "prismjs": "^1.5.1" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..add33ea --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,13 @@ +import buble from 'rollup-plugin-buble' + +export default { + entry: 'src/index.js', + dest: 'lib/docsify.js', + plugins: [buble()], + globals: { + marked: 'marked', + prismjs: 'Prism' + }, + format: 'umd', + moduleName: 'Docsify' +} diff --git a/src/ajax.js b/src/ajax.js new file mode 100644 index 0000000..71e3fe0 --- /dev/null +++ b/src/ajax.js @@ -0,0 +1,10 @@ +export default function (url, options = {}) { + const xhr = new XMLHttpRequest() + + xhr.open(options.method || 'get', url) + xhr.send() + + return { + then: cb => xhr.addEventListener('load', cb) + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..744cc98 --- /dev/null +++ b/src/index.js @@ -0,0 +1,35 @@ +import ajax from './ajax' +import render from './render' + +class Docsify { + constructor (opts = {}) { + Docsify.installed = true + + this.dom = document.querySelector(opts.el || 'body') + this.loc = document.location.pathname + this.loc = this.loc === '/' ? 'README' : this.loc + this.load() + } + + load () { + ajax(`${this.loc}.md`).then(res => { + const target = res.target + if (target.status >= 400) { + this.render('not found') + } else { + this.render(res.target.response) + } + }) + } + + render (content) { + this.dom.innerHTML = render(content) + } +} + +window.addEventListener('load', () => { + if (Docsify.installed) return + new Docsify() +}) + +export default Docsify diff --git a/src/render.js b/src/render.js new file mode 100644 index 0000000..2cbd5f3 --- /dev/null +++ b/src/render.js @@ -0,0 +1,26 @@ +import marked from 'marked' +import Prism from 'prismjs' + +const renderer = new marked.Renderer() + +/** + * render anchor tag + * @link https://github.com/chjj/marked#overriding-renderer-methods + */ +renderer.heading = function (text, level) { + const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-') + + return '' + text + + '' +} + +marked.setOptions({ + highlight (code, lang) { + return Prism.highlight(code, Prism.languages[lang]) + } +}) + +export default function (content) { + return marked(content, { renderer }) +} diff --git a/test/404.html b/test/404.html new file mode 100644 index 0000000..14e8da7 --- /dev/null +++ b/test/404.html @@ -0,0 +1,12 @@ + + + + + docsify + + + + + + + diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..ac81856 --- /dev/null +++ b/test/README.md @@ -0,0 +1,27 @@ +# docsify +> A magical documentation generator. + +## Usage +Create `404.html` into `/docs` + +```html + + + + + + + + + + + +``` + +```javascript +var a = require('a') +console.log(a) +``` + +## License +MIT diff --git a/test/docsify.js b/test/docsify.js new file mode 120000 index 0000000..5f1f5a5 --- /dev/null +++ b/test/docsify.js @@ -0,0 +1 @@ +../lib/docsify.js \ No newline at end of file diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..12694d9 --- /dev/null +++ b/test/server.js @@ -0,0 +1,8 @@ +const server = require('http').createServer() +const buffet = require('buffet')({ root: './test' }) + +server.on('request', buffet) + +server.listen(6677, function () { + console.log('Ready! Listening on http://localhost:6677') +}) diff --git a/test/theme.md b/test/theme.md new file mode 100644 index 0000000..e86c6e5 --- /dev/null +++ b/test/theme.md @@ -0,0 +1 @@ +# Themes \ No newline at end of file diff --git a/test/themes b/test/themes new file mode 120000 index 0000000..de90031 --- /dev/null +++ b/test/themes @@ -0,0 +1 @@ +../themes \ No newline at end of file diff --git a/themes/vue.css b/themes/vue.css new file mode 100644 index 0000000..e69de29