Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extractYaml option #494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,20 @@ const editor = new EasyMDE({
previewClass: "my-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, preview) => { // Async method
setTimeout(() => {
preview.innerHTML = customMarkdownParser(plainText);
}, 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
// vdom diffing.
// return null;
Expand Down
27 changes: 23 additions & 4 deletions src/js/easymde.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require('codemirror/addon/display/autorefresh.js');
require('codemirror/addon/selection/mark-selection.js');
require('codemirror/addon/search/searchcursor.js');
require('codemirror/mode/gfm/gfm.js');
require('codemirror/mode/yaml-frontmatter/yaml-frontmatter.js');
require('codemirror/mode/xml/xml.js');
var CodeMirrorSpellChecker = require('codemirror-spell-checker');
var marked = require('marked').marked;
Expand Down Expand Up @@ -2010,6 +2011,24 @@ EasyMDE.prototype.updateStatusBar = function (itemName, content) {
*/
EasyMDE.prototype.markdown = function (text) {
if (marked) {
// remove yaml-frontmatter from preview
if (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 = '';
}
if (this.options.extractYaml) {
this.options.extractYaml(yaml);
}
}
// Initialize
var markedOptions;
if (this.options && this.options.renderingConfig && this.options.renderingConfig.markedOptions) {
Expand Down Expand Up @@ -2115,25 +2134,25 @@ EasyMDE.prototype.render = function (el) {
document.addEventListener('keydown', this.documentOnKeyDown, false);

var mode, backdrop;

var defaultMode = 'yaml-frontmatter';
// CodeMirror overlay mode
if (options.overlayMode) {
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';
backdrop = options.parsingConfig;
backdrop.gitHubSpice = false;
} else {
mode = options.parsingConfig;
mode.name = 'gfm';
mode.name = defaultMode;
mode.gitHubSpice = false;
}
if (options.spellChecker !== false) {
mode = 'spell-checker';
backdrop = options.parsingConfig;
backdrop.name = 'gfm';
backdrop.name = defaultMode;
backdrop.gitHubSpice = false;

if (typeof options.spellChecker === 'function') {
Expand Down
1 change: 1 addition & 0 deletions types/easymde.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ declare namespace EasyMDE {
autoRefresh?: boolean | { delay: number; };
blockStyles?: BlockStyleOptions;
element?: HTMLElement;
extractYaml?: (yaml: string) => void;
forceSync?: boolean;
hideIcons?: ReadonlyArray<ToolbarButton>;
indentWithTabs?: boolean;
Expand Down