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

add option to force insert line breaks to highlighting code. #329

Open
wants to merge 3 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: 9 additions & 0 deletions contributors/dindinw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
2024-11-27

I hereby agree to the terms of the "Markdown Here Individual Contributor License Agreement", with MD5 checksum dda72cea89d55de9fda0a102494134b4.

I furthermore declare that I am authorized and able to make this agreement and sign this declaration.

Signed,

Alex Wu https://github.com/dindinw
1 change: 1 addition & 0 deletions src/common/markdown-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function markdownRender(mdText, userprefs, marked, hljs) {
// so we'll add it by sneaking it into this config field.
langPrefix: 'hljs language-',
math: userprefs['math-enabled'] ? mathify : null,
highlightForceBreaks: userprefs['highlight-line-breaks-enabled'],
highlight: function(codeText, codeLanguage) {
if (codeLanguage &&
hljs.getLanguage(codeLanguage.toLowerCase())) {
Expand Down
4 changes: 4 additions & 0 deletions src/common/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ Renderer.prototype.code = function(code, lang, escaped) {
var out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
if (this.options.highlightForceBreaks) {
out = out.replace(/\n/g, '<br>');
}
code = out;
}
}
Expand Down Expand Up @@ -1248,6 +1251,7 @@ marked.defaults = {
smartLists: false,
silent: false,
highlight: null,
highlightForceBreaks: false,
langPrefix: 'lang-',
smartypants: false,
headerPrefix: '',
Expand Down
6 changes: 5 additions & 1 deletion src/common/options-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var DEFAULTS = {
'hotkey': { shiftKey: false, ctrlKey: true, altKey: true, key: 'M' },
'forgot-to-render-check-enabled': false,
'header-anchors-enabled': false,
'gfm-line-breaks-enabled': true
'gfm-line-breaks-enabled': true,
'highlight-line-breaks-enabled': false
};

/*? if(platform!=='thunderbird'){ */
Expand Down Expand Up @@ -130,6 +131,7 @@ var ChromeOptionsStore = {
'hotkey': DEFAULTS['hotkey'],
'forgot-to-render-check-enabled': DEFAULTS['forgot-to-render-check-enabled'],
'header-anchors-enabled': DEFAULTS['header-anchors-enabled'],
'highlight-line-breaks-enabled': DEFAULTS['highlight-line-breaks-enabled'],
'gfm-line-breaks-enabled': DEFAULTS['gfm-line-breaks-enabled']
},

Expand Down Expand Up @@ -294,6 +296,7 @@ var MozillaOptionsStore = {
'hotkey': DEFAULTS['hotkey'],
'forgot-to-render-check-enabled': DEFAULTS['forgot-to-render-check-enabled'],
'header-anchors-enabled': DEFAULTS['header-anchors-enabled'],
'highlight-line-breaks-enabled': DEFAULTS['highlight-line-breaks-enabled'],
'gfm-line-breaks-enabled': DEFAULTS['gfm-line-breaks-enabled']
},

Expand Down Expand Up @@ -469,6 +472,7 @@ var SafariOptionsStore = {
'hotkey': DEFAULTS['hotkey'],
'forgot-to-render-check-enabled': DEFAULTS['forgot-to-render-check-enabled'],
'header-anchors-enabled': DEFAULTS['header-anchors-enabled'],
'highlight-line-breaks-enabled': DEFAULTS['highlight-line-breaks-enabled'],
'gfm-line-breaks-enabled': DEFAULTS['gfm-line-breaks-enabled']
}
};
Expand Down
12 changes: 12 additions & 0 deletions src/common/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,18 @@ <h1>
</label>
</div>
<hr/>
<div>
<input type="checkbox" id="highlight-line-breaks-enabled"/>
<label for="highlight-line-breaks-enabled" data-i18n="highlight_line_breaks_enabled_label">
<b>Enable line breaks in Syntax Highlighting.</b>
</label>
<div class="extra-description">
<p data-i18n="highlight_line_breaks_enabled_1">
try to add line breaks &lt;br&gt; when in some case CSS "white-space:pre" not work.
</p>
</div>
</div>
<hr/>
</div>

<div class="control-group">
Expand Down
15 changes: 10 additions & 5 deletions src/common/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
*/

var cssEdit, cssSyntaxEdit, cssSyntaxSelect, rawMarkdownIframe, savedMsg,
mathEnable, mathEdit, hotkeyShift, hotkeyCtrl, hotkeyAlt, hotkeyKey,
forgotToRenderCheckEnabled, headerAnchorsEnabled, gfmLineBreaksEnabled;
var loaded = false;
mathEnable, mathEdit, hotkeyShift, hotkeyCtrl, hotkeyAlt, hotkeyKey,
forgotToRenderCheckEnabled, headerAnchorsEnabled, gfmLineBreaksEnabled,
highlightLineBreaksEnabled,
loaded = false;

function onLoad() {
var xhr;
Expand All @@ -41,6 +42,7 @@ function onLoad() {
forgotToRenderCheckEnabled = document.getElementById('forgot-to-render-check-enabled');
headerAnchorsEnabled = document.getElementById('header-anchors-enabled');
gfmLineBreaksEnabled = document.getElementById('gfm-line-breaks-enabled');
highlightLineBreaksEnabled = document.getElementById('highlight-line-breaks-enabled');

//
// Syntax highlighting styles and selection
Expand Down Expand Up @@ -86,6 +88,8 @@ function onLoad() {

gfmLineBreaksEnabled.checked = prefs['gfm-line-breaks-enabled'];

highlightLineBreaksEnabled.checked = prefs['highlight-line-breaks-enabled'];

// Start watching for changes to the styles.
setInterval(checkChange, 100);

Expand Down Expand Up @@ -233,7 +237,7 @@ function checkChange() {
mathEnable.checked + mathEdit.value +
hotkeyShift.checked + hotkeyCtrl.checked + hotkeyAlt.checked + hotkeyKey.value +
forgotToRenderCheckEnabled.checked + headerAnchorsEnabled.checked +
gfmLineBreaksEnabled.checked;
gfmLineBreaksEnabled.checked + highlightLineBreaksEnabled.checked;

if (newOptions !== lastOptions) {
// CSS has changed.
Expand Down Expand Up @@ -264,7 +268,8 @@ function checkChange() {
},
'forgot-to-render-check-enabled': forgotToRenderCheckEnabled.checked,
'header-anchors-enabled': headerAnchorsEnabled.checked,
'gfm-line-breaks-enabled': gfmLineBreaksEnabled.checked
'gfm-line-breaks-enabled': gfmLineBreaksEnabled.checked,
'highlight-line-breaks-enabled': highlightLineBreaksEnabled.checked
},
function() {
updateMarkdownRender();
Expand Down