-
I am writing a block-typed extension to quote forum posts in replies. which should result in something like:
plus some html wrappers. The extension must also work with child tokens so quote can embed another quote and so on. The base is simple:
I am using script render because in case the renderer is not configured to recognize the extension, the html sanitizer will simply remove it altogether from the final result. I think I can figure out the children rendering. I have one component that does it( I would guess I should do sub-pattern matching for the [quote] and simply look for the individual attributes inside? Though am not sure how to handle space in the name, for example. Maybe using quotes would make more sense, like But am also not sure how properly render the children in case of quote containing another quote so the regex works as it it supposed to... Anyone done block extension like this and can help out? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can use something like attributes-parser to parse the attributes. Here is a working example: import { Marked } from 'marked';
import parseAttrs from 'attributes-parser';
const marked = new Marked();
const quoteRule = /^\[quote([^\]]*)\]([^]*?)\[\/quote\]/;
const quoteStartRule = new RegExp(quoteRule.source, 'm');
const quote = {
name: 'quote',
level: 'block',
start(src) {
const m = src.match(quoteStartRule);
return m ? m.index : null;
},
tokenizer(src, tokens) {
const match = quoteRule.exec(src);
if (match) {
return {
type: 'quote',
raw: match[0],
attrs: parseAttrs(match[1]),
text: match[2],
tokens: this.lexer.blockTokens(match[2]),
};
}
},
renderer(token) {
console.log(token);
return '<script rel="quote" type="application/json">' + JSON.stringify(token.attrs) + '</script>\n';
},
};
marked.use({ extensions: [quote] });
console.log(marked.parse(`
test
[quote user=123 quote="\\\"" name="John Doe" timestamp=123456]foo bar baz lorem ipsum etc[/quote]
`)); nesting will be much harder because when the regexp matches |
Beta Was this translation helpful? Give feedback.
...ah, it's the next day, i have a clear head, i think i will just add a new tag, like
[user=123]john doe[/user]
and prepend the quote with it, rather than try to make the original idea work. even here on github, it does not use anything more than a markdown blockquote. the main idea was merely to be able to notify the user about being mentioned, so this will do just that without having to alter anything related to quotes themselves.