2017-05-24 13:21:15 -04:00
|
|
|
import unified from 'unified';
|
2017-06-09 23:49:14 -04:00
|
|
|
import remarkToMarkdown from 'remark-parse';
|
2017-05-24 13:21:15 -04:00
|
|
|
import { Mark } from 'prosemirror-model';
|
2017-06-09 17:23:58 -04:00
|
|
|
import markdownToProseMirror from './markdownToProseMirror';
|
2016-11-04 11:04:54 -07:00
|
|
|
|
2017-06-09 17:23:58 -04:00
|
|
|
const state = { activeMarks: Mark.none, textsArray: [] };
|
2017-06-07 22:23:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses unified to parse markdown and apply plugins.
|
|
|
|
* @param {string} src raw markdown
|
|
|
|
* @returns {Node} a ProseMirror Node
|
|
|
|
*/
|
|
|
|
function parser(src) {
|
2017-05-24 13:21:15 -04:00
|
|
|
const result = unified()
|
2017-06-13 15:30:11 -04:00
|
|
|
.use(remarkToMarkdown, { fences: true, footnotes: true, pedantic: true })
|
2017-05-24 13:21:15 -04:00
|
|
|
.parse(src);
|
|
|
|
|
2017-06-07 22:23:06 -04:00
|
|
|
return unified()
|
2017-06-09 17:23:58 -04:00
|
|
|
.use(markdownToProseMirror, { state })
|
2017-05-24 13:21:15 -04:00
|
|
|
.runSync(result);
|
2017-06-07 22:23:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the parser and makes schema and plugins available at top scope.
|
2017-06-09 17:23:58 -04:00
|
|
|
* @param {Schema} schema - a ProseMirror schema
|
|
|
|
* @param {Map} plugins - Immutable Map of registered plugins
|
2017-06-07 22:23:06 -04:00
|
|
|
*/
|
2017-06-09 17:23:58 -04:00
|
|
|
function parserGetter(schema, plugins) {
|
|
|
|
state.schema = schema;
|
|
|
|
state.plugins = plugins;
|
2017-06-07 22:23:06 -04:00
|
|
|
return parser;
|
|
|
|
}
|
2017-05-24 13:21:15 -04:00
|
|
|
|
2017-06-07 22:23:06 -04:00
|
|
|
export default parserGetter;
|