Skip to content

Commit

Permalink
Merge pull request #153 from WideChat/multiple_newline_fix
Browse files Browse the repository at this point in the history
[FIX] Markdown Rules for Multiple Newlines in Messages
  • Loading branch information
ear-dev authored Sep 13, 2022
2 parents 3603799 + 62fced2 commit cfbaaa0
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
1 change: 0 additions & 1 deletion build/5.chunk.bcb58.js.map

This file was deleted.

4 changes: 2 additions & 2 deletions build/5.chunk.bcb58.js → build/5.chunk.d45dc.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/5.chunk.d45dc.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/bundle.31a33.js → build/bundle.2fcbc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/bundle.31a33.js.map → build/bundle.2fcbc.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html><head><meta charset="utf-8"><title></title><meta name="viewport" content="width=device-width,initial-scale=1"></head><body><script src="livechat/0.chunk.7df7a.js"></script><script src="livechat/polyfills.b9285.js"></script><script src="livechat/vendors~bundle.chunk.b4ff7.js"></script><script src="livechat/bundle.31a33.js"></script></body></html>
<!doctype html><html><head><meta charset="utf-8"><title></title><meta name="viewport" content="width=device-width,initial-scale=1"></head><body><script src="livechat/0.chunk.7df7a.js"></script><script src="livechat/polyfills.b9285.js"></script><script src="livechat/vendors~bundle.chunk.b4ff7.js"></script><script src="livechat/bundle.2fcbc.js"></script></body></html>
35 changes: 35 additions & 0 deletions src/components/Messages/MessageText/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ const md = new MarkdownIt({
typographer: true,
});

function setNewlines(tokens, idx, previousInlinePos) {
let result = '';
if (idx > 1) {
let inline = tokens[idx - previousInlinePos];
const paragraph = tokens[idx];
if (inline.map && inline.map[1] && paragraph.map && paragraph.map[0]) {
const diff = paragraph.map[0] - inline.map[1];
if (diff > 0) {
result = '<br>'.repeat(diff);
}
}

const listClose = tokens[idx - previousInlinePos + 1];
if (listClose.type.includes('list_close')) {
inline = tokens[idx - previousInlinePos - 2];
if (inline.map && inline.map[1] && paragraph.map && paragraph.map[0]) {
const diff = paragraph.map[0] - inline.map[1];
if (diff > 0) {
result = '<br>'.repeat(diff);
}
}
}
}
return result;
}

const defaultRender = md.renderer.rules.link_open || ((tokens, idx, options, env, self) => self.renderToken(tokens, idx, options));

md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
Expand All @@ -29,6 +55,15 @@ md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
return defaultRender(tokens, idx, options, env, self);
};

md.renderer.rules.paragraph_open = function(tokens, idx, options, env, self) {
return setNewlines(tokens, idx, 2) + defaultRender(tokens, idx, options, env, self);
};

md.renderer.rules.list_item_open = function(tokens, idx, options, env, self) {
return setNewlines(tokens, idx, 3) + defaultRender(tokens, idx, options, env, self);
};


md.use((md) => {
const renderStrong = (tokens, idx, opts, _, slf) => {
const token = tokens[idx];
Expand Down

0 comments on commit cfbaaa0

Please sign in to comment.