2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-07-29 04:44:28 -06:00

Added extractYaml option

This commit is contained in:
faisal 2022-08-24 18:50:39 -05:00
parent 84aafddd92
commit 7dd3284398
2 changed files with 29 additions and 5 deletions

View File

@ -261,13 +261,20 @@ const editor = new EasyMDE({
previewClass: "my-custom-styling", previewClass: "my-custom-styling",
previewClass: ["my-custom-styling", "more-custom-styling"], previewClass: ["my-custom-styling", "more-custom-styling"],
extractYaml: (yaml) => {
// Extracts yaml from Markdown, use a third party parser to convert to json
// ----
// Key: value
// ---
console.log(yaml);
}
previewRender: (plainText) => customMarkdownParser(plainText), // Returns HTML from a custom parser previewRender: (plainText) => customMarkdownParser(plainText), // Returns HTML from a custom parser
previewRender: (plainText, preview) => { // Async method previewRender: (plainText, preview) => { // Async method
setTimeout(() => { setTimeout(() => {
preview.innerHTML = customMarkdownParser(plainText); preview.innerHTML = customMarkdownParser(plainText);
}, 250); }, 250);
// If you return null, the innerHTML of the preview will not // If you return null, the innerHTML of the preview will not
// be overwritten. Useful if you control the preview node's content via // be overwritten. Useful if you control the preview node's content via
// vdom diffing. // vdom diffing.
// return null; // return null;

View File

@ -10,6 +10,7 @@ require('codemirror/addon/display/autorefresh.js');
require('codemirror/addon/selection/mark-selection.js'); require('codemirror/addon/selection/mark-selection.js');
require('codemirror/addon/search/searchcursor.js'); require('codemirror/addon/search/searchcursor.js');
require('codemirror/mode/gfm/gfm.js'); require('codemirror/mode/gfm/gfm.js');
require('codemirror/mode/yaml-frontmatter/yaml-frontmatter.js');
require('codemirror/mode/xml/xml.js'); require('codemirror/mode/xml/xml.js');
var CodeMirrorSpellChecker = require('codemirror-spell-checker'); var CodeMirrorSpellChecker = require('codemirror-spell-checker');
var marked = require('marked').marked; var marked = require('marked').marked;
@ -2010,6 +2011,22 @@ EasyMDE.prototype.updateStatusBar = function (itemName, content) {
*/ */
EasyMDE.prototype.markdown = function (text) { EasyMDE.prototype.markdown = function (text) {
if (marked) { if (marked) {
// When extractYaml is present we remove it from preview
if (typeof this.options.extractYaml === 'function' && text.indexOf('---') === 0) {
var yaml = text.substring(3);
var closeIdx = yaml.indexOf('---');
if (closeIdx === -1) {
// yaml-frontmatter can be closed with ...
closeIdx = yaml.indexOf('...');
}
if (closeIdx != -1) {
text = text.substring(closeIdx + 6);
yaml = yaml.substring(0, closeIdx);
} else {
text = '';
}
this.options.extractYaml(yaml);
}
// Initialize // Initialize
var markedOptions; var markedOptions;
if (this.options && this.options.renderingConfig && this.options.renderingConfig.markedOptions) { if (this.options && this.options.renderingConfig && this.options.renderingConfig.markedOptions) {
@ -2115,11 +2132,11 @@ EasyMDE.prototype.render = function (el) {
document.addEventListener('keydown', this.documentOnKeyDown, false); document.addEventListener('keydown', this.documentOnKeyDown, false);
var mode, backdrop; var mode, backdrop;
var defaultMode = typeof options.extractYaml === 'function' ? 'yaml-frontmatter' : 'gfm';
// CodeMirror overlay mode // CodeMirror overlay mode
if (options.overlayMode) { if (options.overlayMode) {
CodeMirror.defineMode('overlay-mode', function (config) { CodeMirror.defineMode('overlay-mode', function (config) {
return CodeMirror.overlayMode(CodeMirror.getMode(config, options.spellChecker !== false ? 'spell-checker' : 'gfm'), options.overlayMode.mode, options.overlayMode.combine); return CodeMirror.overlayMode(CodeMirror.getMode(config, options.spellChecker !== false ? 'spell-checker' : defaultMode), options.overlayMode.mode, options.overlayMode.combine);
}); });
mode = 'overlay-mode'; mode = 'overlay-mode';
@ -2127,13 +2144,13 @@ EasyMDE.prototype.render = function (el) {
backdrop.gitHubSpice = false; backdrop.gitHubSpice = false;
} else { } else {
mode = options.parsingConfig; mode = options.parsingConfig;
mode.name = 'gfm'; mode.name = defaultMode;
mode.gitHubSpice = false; mode.gitHubSpice = false;
} }
if (options.spellChecker !== false) { if (options.spellChecker !== false) {
mode = 'spell-checker'; mode = 'spell-checker';
backdrop = options.parsingConfig; backdrop = options.parsingConfig;
backdrop.name = 'gfm'; backdrop.name = defaultMode;
backdrop.gitHubSpice = false; backdrop.gitHubSpice = false;
if (typeof options.spellChecker === 'function') { if (typeof options.spellChecker === 'function') {