Skip to content

Commit

Permalink
Merge c6cd0b4 into 20ee30b
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 4, 2018
2 parents 20ee30b + c6cd0b4 commit 4c9c3bb
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 78 deletions.
93 changes: 33 additions & 60 deletions packages/zmarkdown/README.md
Expand Up @@ -143,80 +143,58 @@ Only `metadata` is described in the **Response** sections below.

This endpoint only returns `{}` as metadata, i.e. an empty object.



[ping]: https://www.npmjs.com/package/remark-ping
[iframes]: https://www.npmjs.com/package/remark-iframes



## Client Architecture

The architecture of the client is similar to `remark` or `Vue`. The *manager*, here `client/client.js`, exposes the variable `ZMarkdown`. This *manager* doesn't work alone because it does not know how to convert the input to the desired output. Example, if you want to convert markdown to html, the *manager* doesn't know how to do this. It need modules to know how to do this.
The architecture of the client is similar to `remark` or `Vue`. The *manager*, here `client/client.js`, exposes a global variable `ZMarkdown`. This *manager* doesn't work alone because it does not know how to convert the input to the desired output. Example, if you want to convert markdown to html, the *manager* doesn't know how to do this. It need modules to know how to do this.

### Manager

You need to add modules with `ZMarkdown.use(obj)` to manage rendering.

To convert a markdown string you should use `ZMarkdown.render(str, moduleName = null, cb = null)`:
To convert a markdown string you should use `ZMarkdown.render(str, moduleName = defaultModule, cb = undefined)`:
- **str**: `string`, string to convert
- **moduleName**: `string`, name of the module you want to use. This parameter can be **omitted only** if you define a default module with `ZMarkdown.setDefaultModule` (you can reset default type with `ZMarkdown.resetDefaultModule`)
- **moduleName**: `string`, name of the module you want to use. This parameter can be **omitted only** if you define a default module with `ZMarkdown.setDefaultModule` (you can reset default module with `ZMarkdown.resetDefaultModule`)
- **cb**: `function(err, vfile)`, called when process is done

This function return a `Promise` if no callback specified.
This function returns a `Promise` if no callback specified.

`ZMarkdown` have also a `parse(moduleName)` function to get the MDAST tree and `getParser(moduleName)` to get the whole parser. This parameter can be **omitted only** if you define a default module with `ZMarkdown.setDefaultModule`.
`ZMarkdown` also has a `parse(moduleName)` function to get the MDAST tree and `getParser(moduleName)` to get the whole parser. This parameter can be **omitted only** if you define a default module with `ZMarkdown.setDefaultModule`.

### Module

The module is an object that should have the following properties:
A module is an object with the following properties:

- **name**: `string`, a string that define the name of the plugin. This is used to identify each plugins
- **render**: `function(input, cb): void`, a function called in `ZMarkdown.render`. It returns a `Promise` if no callback specified
- **parse**: `function(input): object`, get MDAST tree
- **getParser**: `function(): object`, get the whole of parser
- **initialize**: `function(config)`, configure the module with a custom configuration. You do not have to call this function if you want to use the default configuration
- **name**: `string`, a string defining the name of the plugin. This is used to identify each module.
- **render**: `function(input, cb): void`, a function called by `ZMarkdown.render`. It returns a `Promise` if no callback specified.
- **parse**: `function(input): object`, gets MDAST tree.
- **getParser**: `function(): object`, gets the whole of parser.
- **initialize**: `function(config)`, configure the module with a custom configuration. You do not have to call this function if you want to use the default configuration.

### Tips

You can start a module with a base parser using `./common.js`. The module exports a function that can take two optionals parameters:
- **opts**: an object that can have:
- **remarkConfig**: `object` , your remark config (defaults to the configuration from `config/remark`)
- **extraRemarkPlugins**: `array of objects`, remark plugins you want to add to the default parser (remark pipeline). The object should contain:
- **obj**: `remark plugin`
- **option**: optional, option of the plugin
- **check**: `function(config)` optional, a function that returns a boolean to use this plugin depending to remark config passing in argument of `common`
- **processor**: `function(config)` (default `getHTMLProcessor` of `./common.js`), processor function used to configure the remark pipeline for you output


You can start a module with a base parser using `./common.js` (see `./modules/zhtml.js` for instance). The module exports a function that can take two optionals parameters:
- **opts**: an object that can have:
- **remarkConfig**: `object`, your remark config (defaults to the configuration from `config/remark.js`).
- **extraRemarkPlugins**: `Array<objects>`, remark plugins you want to add to the default parser (remark pipeline). The object should contain:
- **obj**: `remark plugin`.
- **option**: optional, plugin config.
- **processor**: `function(config)` (defaults to `getHTMLProcessor` of `./common.js`), processor function used to configure the remark pipeline for your output

Module example:
### Module Example

```js
const common = require('../../common') /* zmarkdown common file */
const common = require('zmarkdown/modules/common') /* zmarkdown common file */
const remarkToc = require('remark-toc')
const remark2rehype = require('remark-rehype')

const opts = {
remarkConfig: null /* custom remark config. Null or omit to use default conf */,
remarkConfig: undefined /* custom remark config, defaults to ./config/remark.js */,
extraRemarkPlugins: [
{
obj: remarkToc,
option: undefined /* remark plugin option. undefined or omit to not configure it */,
check: (config) => {
/*
* This function allow to use this plugin only on certain cases.
* The config passed in argument are remark config.
*
* Note: This config are the config passed in opts when
* instancing common. Common make a clone of this conf, thereby
* any update make after instancing common will not apear.
* For example, the "noTypography" define in processor function
* below not appear in this conf.
*/
return true
},
option: undefined /* remark plugin option, undefined or omit to not configure it */
},
],
}

Expand Down Expand Up @@ -244,11 +222,7 @@ export function getParser () {
export const name = 'custom-html'
```



Modules need to be bundled for used on the client.

Here is a webpack conf of the previous example:
Modules need to be bundled, here is a webpack config for the previous example:

```js
const path = require('path')
Expand All @@ -258,11 +232,11 @@ const mode = process.env.NODE_ENV ? process.env.NODE_ENV : 'production'
module.exports = {
mode,
name: 'ZMarkdownCustomHTML', // name of process if you use parallel-webpack
entry: ['./plugins/client/custom-html'], // path to your module
entry: ['./modules/custom-html'], // path to your module
output: {
path: path.resolve(__dirname, 'dist'), // destination folder
filename: 'custom-html.js', // file name
library: 'ZMarkdownCustomHTML', // Name of the global constant for dom access
library: 'ZMarkdownCustomHTML', // Name of the object that will be available on the `window` global object
},
module: {
rules: [
Expand All @@ -278,16 +252,15 @@ module.exports = {
}
```

### Dev build

If you want to watch the local files while working zmarkdown, you can use `npm run watch:client`. Run the client by opening `./public/index.html`.

### Dev mode

If you want a file watcher when you working zmarkdown, you can use `npm run watch:client`. You can run the client by openning `./public/index.html`.
*Note: the current implementation (parallel-webpack) doesn't support hot-reload, you will have to manually refresh the webpage after each change*.

*Note: the current implementation (parallel-webpack) doesn't support hot-reload, so after each change, you should refresh your internet tab*.
### Production build

To build for production, just run `npm run release`. Generated files are located in `./dist`.


### Build for production

To build for production, just run `npm run release`. Generated files are located in `./dist`.
[ping]: https://www.npmjs.com/package/remark-ping
[iframes]: https://www.npmjs.com/package/remark-iframes
8 changes: 4 additions & 4 deletions packages/zmarkdown/client/index.js
Expand Up @@ -3,14 +3,14 @@ import dedent from 'dedent'
export const modules = {}
export let defaultType

function checkValidModule (moduleName) {
function validateModule (moduleName) {
if (modules.length === 0) {
throw new Error('No module available.')
}

if (!moduleName && !defaultType) {
if (!moduleName || typeof moduleName !== 'string') {
throw new Error(dedent`Bad type for parameter 'moduleName'.
throw new Error(dedent`Bad type for parameter 'moduleName'.
Expected 'string' but was: ${typeof moduleName}`)
}
throw new Error('This function expects to be called with ' +
Expand Down Expand Up @@ -90,7 +90,7 @@ export function render (str, moduleName = null, cb = null) {
}

export function parse (str, moduleName = null) {
checkValidModule(moduleName)
validateModule(moduleName)

if (!moduleName) {
moduleName = defaultType
Expand All @@ -100,7 +100,7 @@ export function parse (str, moduleName = null) {
}

export function getParser (moduleName = null) {
checkValidModule(moduleName)
validateModule(moduleName)

if (!moduleName) {
moduleName = defaultType
Expand Down
8 changes: 1 addition & 7 deletions packages/zmarkdown/common.js
Expand Up @@ -122,13 +122,7 @@ const zmdParser = (config, extraRemarkPlugins = []) => {
})

for (const record of extraRemarkPlugins) {
if (!record.check || record.check(config)) {
if (typeof record.option !== 'undefined') {
mdProcessor.use(record.obj, record.option)
} else {
mdProcessor.use(record.obj)
}
}
mdProcessor.use(record.obj, record.option)
}

return mdProcessor
Expand Down
@@ -1,4 +1,4 @@
const common = require('../../common')
const common = require('../common')

let parser = common()

Expand Down
@@ -1,7 +1,7 @@
const rebberStringify = require('rebber/src')
const rebberConfig = require('../../config/rebber')
const rebberConfig = require('../config/rebber')

const common = require('../../common')
const common = require('../common')

let parser

Expand Down
2 changes: 2 additions & 0 deletions packages/zmarkdown/public/main.css
Expand Up @@ -17,6 +17,8 @@ body {
/* Editor style */

.editor {
font-family: monospace;
font-size: 14px;
border: 0;
-moz-box-sizing: border-box;
box-sizing: border-box;
Expand Down
4 changes: 2 additions & 2 deletions packages/zmarkdown/public/script.js
Expand Up @@ -15,7 +15,7 @@ Split(['#center-top', '#center-bottom'], {
ZMarkdown.use(ZMarkdownZHTML);
ZMarkdown.use(ZMarkdownZLatex);

ZMarkdown.setDefaultModule("zhtml");
ZMarkdown.setDefaultModule('zhtml');

const ansiUp = new AnsiUp()
const editor = document.querySelector('#left-top > textarea')
Expand Down Expand Up @@ -50,7 +50,7 @@ function update () {
ZMarkdown.render(editor.value).then((vFile) => {
render.innerHTML = vFile.toString().trim()
html.textContent = vFile.toString().trim()
buildSpoilers(render.querySelectorAll(".custom-block-spoiler"))
buildSpoilers(render.querySelectorAll('.custom-block-spoiler'))
})
ZMarkdown.render(editor.value, 'zlatex').then((vFile) => {
latex.textContent = vFile.toString().trim()
Expand Down
4 changes: 2 additions & 2 deletions packages/zmarkdown/webpack.config.js
Expand Up @@ -29,7 +29,7 @@ module.exports = [
}),
Object.assign({}, defaultConf, {
name: 'ZMarkdownZHTML',
entry: ['./plugins/client/zhtml'],
entry: ['./modules/zhtml'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'zmarkdown-zhtml.js',
Expand All @@ -38,7 +38,7 @@ module.exports = [
}),
Object.assign({}, defaultConf, {
name: 'ZMarkdownZLatex',
entry: ['./plugins/client/zlatex'],
entry: ['./modules/zlatex'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'zmarkdown-zlatex.js',
Expand Down

0 comments on commit 4c9c3bb

Please sign in to comment.