-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
contentScriptMarkdownIt.js
35 lines (31 loc) · 1.18 KB
/
contentScriptMarkdownIt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module.exports = {
default: function(context) {
return {
plugin: function(markdownIt, _options) {
const contentScriptId = context.contentScriptId;
const defaultRender = markdownIt.renderer.rules.fence || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options, env, self);
};
markdownIt.renderer.rules.fence = function(tokens, idx, options, env, self) {
const token = tokens[idx];
if (token.info !== 'postMessageDemo') return defaultRender(tokens, idx, options, env, self);
const postMessageWithResponseTest = `
webviewApi.postMessage('${contentScriptId}', 'messageFromMarkdownIt').then(function(response) {
console.info('Got response in Markdown-it content script: ' + response);
});
return false;
`;
return `
<div style="padding:10px; border: 1px solid green;">
<p>Plugin active! Content: <p><pre>${token.content}</pre><p></p>
<p><a href="#" onclick="${postMessageWithResponseTest.replace(/\n/g, ' ')}">Click to post a message to plugin and check the response in the console</a></p>
</div>
`;
};
},
assets: function() {
return [];
},
}
},
}