Don't depend on imports for md extensions
This commit is contained in:
parent
5d139e461c
commit
98e03b04b5
|
@ -143,11 +143,16 @@ function setupImageDimensions(md) {
|
|||
md.renderer.rules.image = renderImage;
|
||||
}
|
||||
|
||||
let Helpers;
|
||||
|
||||
export function setup(opts, siteSettings, state) {
|
||||
if (opts.setup) {
|
||||
return;
|
||||
}
|
||||
|
||||
// we got to require this late cause bundle is not loaded in pretty-text
|
||||
Helpers = Helpers || requirejs('pretty-text/engines/markdown-it/helpers');
|
||||
|
||||
opts.markdownIt = true;
|
||||
|
||||
let optionCallbacks = [];
|
||||
|
@ -190,6 +195,10 @@ export function setup(opts, siteSettings, state) {
|
|||
delete opts[entry];
|
||||
});
|
||||
|
||||
copy.helpers = {
|
||||
textReplace: Helpers.textReplace
|
||||
};
|
||||
|
||||
opts.discourse = copy;
|
||||
getOptions.f = () => opts.discourse;
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import { textReplace } from 'pretty-text/engines/markdown-it/helpers';
|
||||
|
||||
function addHashtag(buffer, matches, state) {
|
||||
const options = state.md.options.discourse;
|
||||
const [hashtag, slug] = matches;
|
||||
|
@ -99,7 +97,7 @@ export function setup(helper) {
|
|||
|
||||
helper.registerPlugin(md=>{
|
||||
|
||||
md.core.ruler.push('category-hashtag', state => textReplace(
|
||||
md.core.ruler.push('category-hashtag', state => md.options.discourse.helpers.textReplace(
|
||||
state, applyHashtag, true /* skip all links */
|
||||
));
|
||||
});
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { buildEmojiUrl, isCustomEmoji } from 'pretty-text/emoji';
|
||||
import { translations } from 'pretty-text/emoji/data';
|
||||
import { textReplace } from 'pretty-text/engines/markdown-it/helpers';
|
||||
|
||||
const MAX_NAME_LENGTH = 60;
|
||||
|
||||
|
@ -240,7 +239,7 @@ export function setup(helper) {
|
|||
});
|
||||
|
||||
helper.registerPlugin((md)=>{
|
||||
md.core.ruler.push('emoji', state => textReplace(
|
||||
md.core.ruler.push('emoji', state => md.options.discourse.helpers.textReplace(
|
||||
state, (c,s)=>applyEmoji(c,s,md.options.discourse.emojiUnicodeReplacer))
|
||||
);
|
||||
});
|
||||
|
|
|
@ -13,66 +13,6 @@ export default null;
|
|||
// emitter: emitter
|
||||
// });
|
||||
|
||||
export function inlineRegexRule(md, options) {
|
||||
|
||||
const start = options.start.charCodeAt(0);
|
||||
const maxLength = (options.maxLength || 500) + 1;
|
||||
|
||||
return function(state, silent) {
|
||||
const pos = state.pos;
|
||||
|
||||
if (state.src.charCodeAt(pos) !== start || silent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// test prev
|
||||
if (pos > 0) {
|
||||
let prev = state.src.charCodeAt(pos-1);
|
||||
if (!md.utils.isWhiteSpace(prev) && !md.utils.isPunctChar(String.fromCharCode(prev))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// skip if in a link
|
||||
if (options.skipInLink && state.tokens) {
|
||||
let i;
|
||||
for(i=state.tokens.length-1;i>=0;i--) {
|
||||
let token = state.tokens[i];
|
||||
let type = token.type;
|
||||
if (type === 'link_open' || (type === 'html_inline' && token.content.substr(0,2).toLowerCase() === "<a")) {
|
||||
return false;
|
||||
}
|
||||
if (type.block || type === 'link_close' || (type === 'html_inline' && token.content.substr(0,4).toLowerCase() === "</a>")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const substr = state.src.slice(pos, Math.min(pos + maxLength,state.posMax));
|
||||
|
||||
const matches = options.matcher.exec(substr);
|
||||
if (!matches) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// got to test trailing boundary
|
||||
const finalPos = pos+matches[0].length;
|
||||
if (finalPos < state.posMax) {
|
||||
const trailing = state.src.charCodeAt(finalPos);
|
||||
if (!md.utils.isSpace(trailing) && !md.utils.isPunctChar(String.fromCharCode(trailing))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (options.emitter(matches, state)) {
|
||||
state.pos = Math.min(state.posMax, finalPos);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
// based off https://github.com/markdown-it/markdown-it-emoji/blob/master/dist/markdown-it-emoji.js
|
||||
//
|
||||
|
|
|
@ -81,6 +81,10 @@ module PrettyText
|
|||
ctx_load(ctx, "vendor/assets/javascripts/lodash.js")
|
||||
ctx_load_manifest(ctx, "pretty-text-bundle.js")
|
||||
|
||||
if SiteSetting.enable_experimental_markdown_it
|
||||
ctx_load_manifest(ctx, "markdown-it-bundle.js")
|
||||
end
|
||||
|
||||
root_path = "#{Rails.root}/app/assets/javascripts/"
|
||||
|
||||
apply_es6_file(ctx, root_path, "discourse/lib/utilities")
|
||||
|
|
|
@ -8,7 +8,7 @@ __emojiUnicodeReplacer = null;
|
|||
|
||||
__setUnicode = function(replacements) {
|
||||
require('pretty-text/engines/discourse-markdown/emoji').setUnicodeReplacements(replacements);
|
||||
|
||||
|
||||
let unicodeRegexp = new RegExp(Object.keys(replacements).sort().reverse().join("|"), "g");
|
||||
|
||||
__emojiUnicodeReplacer = function(text) {
|
||||
|
|
Loading…
Reference in New Issue