I have the following variable: "hello↵↵world". (the value is coming from a textarea)
The problem is when I ask Meteor to display it with {{content}}, everything appears on the same line and the line break are not taken into account.
{{content}}
# renders
hello world
# should render
hello
world
If you're using bootstrap using the <pre> tag will come with some style that you probably don't want... If you want to avoid that you can solve this with some simple CSS:
.pre {
white-space: pre;
}
and then just wrap your content with that class:
<span class="pre">{{content}}</span>
<pre> worked fine for me until I had long lines of text. By default it disables line wrapping and long lines break the page layout or are cut off.
You could probably get around it with white-space: pre-wrap; but what I ended up doing was create a Spacebars-Helper that escapes the text first and then replaces all breaks with <br/>
UI.registerHelper('breaklines', function(text, options) {
text = s.escapeHTML(text);
text = text.replace(/(\r\n|\n|\r)/gm, '<br/>');
return new Spacebars.SafeString(text);
});
I would then use the helper in my templates in the following way:
{{breaklines title}}
escapeHTML is part of Underscore.string, a collection of string manipulation extensions that you can pull in with meteor add underscorestring:underscore.string, but any other way of escaping html should work fine as well.
Wrap with
<pre>
Any
amount of
formatting.
</pre>
One way is to create a handlebar helper:
Handlebars.registerHelper 'breaklines', (text) ->
text = text.replace /(\r\n|\n|\r)/gm, '<br>'
return text
and then to do so: (do not forget the three brackets!)
{{{breaklines content}}}
just use <br> it is enough no more troubles.
Related
eg:
```
some very long line; some very long line; some very long line; some very long line; some very long line; some very long line; some very long line; some very long line; some very long line; some very long line;
```
will force user to scroll in github/gitlab issues.
Is there a way to soft-line wrap inside code block ?
I've read the related questions but they seem different (eg jekyll etc).
EDIT: manually editing code to limit to 80 columns is not a viable option (eg, when pasting from a compiler output/log etc; that's a lot of work and should not be necessary)
EDIT: see also https://github.com/softvar/enhanced-github/issues/95
A quick workaround is to quote your codeblocks, e.g.
this line is very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long
Make sure you keep the three backticks inline with the very long line. Like this:
> ```this line is very long```
Add below CSS in your output HTML file
or
edit it in the linked CSS
code {
white-space : pre-wrap !important;
}
The imperfect workaround I currently use is to replace the
```
with a single backtick:
`
...and do that line by line. which is infeasible for larger code chunks, and also makes highlighting the code harder for viewers of the markdown. So this 'line by line' workaround is strongly inferior to a genuine code wrap for code contained inside ```.
Simple: Use extension like User Css
Code for default pre code (this too) and gitlab
pre code, /* stackoverflow */
.md:not(.use-csslab) pre code
{
white-space: pre-wrap;
}
Or use another extension, like JSScript triks for full control of own js/css
Switch "Wrap/Unwrap" for pre code anywhere:
in JS section add switch init:
jQuery example to add an switch (checkbox) only for long lines
// https://stackoverflow.com/a/57341170/5447232
var ws = function(i,e) {
$("pre code").filter(function(){
var _t = $(this).parent();
return !_t.has("input[switch").length
&& _t.get(0).scrollWidth > _t.innerWidth();
}).before("<input type='checkbox' checked switch title='Wrap/Unwrap' />");
}
$("body").on("change", ws).change();
$(window).bind("scroll", ws);
in CSS section add styles:
Styles, depend checkbox status
/* https://stackoverflow.com/a/57341170/5447232 */
pre {position:relative;}
input[type="checkbox"][switch] {
position: absolute;
display: block;
right:0.5em;
top:0.5em;
}
pre input[switch]:checked + code { white-space: pre-wrap; }
/* pre input[switch]:not(:checked) + code { white-space: pre; } */
See code at https://gitlab.com/MrSwed/wrap-unwrap-for-pre-code
Instead of wrapping your code with triple backticks ``` wrap it with <code>...</code> tags.
This new feature was added to GitLab in Feb 2022.
Looks like this is not possible yet. But you can use some extensions to work around the markup on GitHub - this chrome extension here is pretty cool,
See this github thread here for more
For non-github places where markdown is supported, extending #Tarun's answer (which works great for regular HTML pages) as follows:
If you don't have access to external CSS, just add the following <style> on the same page:
<style>
code {
white-space : pre-wrap !important;
word-break: break-word;
}
</style>
All code blocks on that page will be word-wrapped. The word-break: break-word will avoid breaking the words across lines.
(SO's question is about Github issues specifically, but putting this down as an answer here since this is the first link on Google that appears for wrapping lines in code blocks in markdown - and would be helpful to many people as evident from the upvotes on similar answer)
Found following solution for myself and tested it on the "github":
1) Create file with suffix ".md"
2) Use <div> tag to solve the requested goal - splitting up long lines into several making it be marked down as one line.
Example:
<div>I am
very
long line</div>
Will be viewed as following:
I am very long line
I was trying to type the few lines in firefox and expect to use getContent{format: text} to only fetch the content.
Here is the content: (I have three lines and each start with the leftmost postion)
"text me if
there is
a chance"
It works in chrome that it gives the following format when running getContent.
...
<body>
text me if
there is
a chance
</body>
...
However in firefox I got:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
text me ifthere isa chance
</body>
</html>
It seems to strip off the line break. Could someone help on this?
In TinyMCE 4+ there is no option to disable stripping off line breaks. For me a littly dirty hack worked - just replace line breaks with <br> tags before initializing TinyMCE:
//imagine that your textarea is called textarea#message
var a = $("textarea#message").val();
a = a.replace(/\n/g, '<br />');
$("textarea#message").val(a);
If you need afterwards text without <br> but with line breaks, just apply the same transformation vise versa:
var a = $("textarea#message").val();
a = a.replace(/<br\s*[\/]?>/gi, "\n");
$("textarea#message").val(a);
Maybe a bit late to the party, but it just happened to me to walk into the same problem. It seems firefox has some issues with linebreaks in text that gets inserted into a textarea. My solution was something like this:
var text = text.replace("\r\n", "\\r\\n");
I guess FireFox is just a great fan of Slash(es)...
For the full solution I used the following code, which does not use the format: text, but format: html which get parsed by the DOMParser first:
var text = editor.getContent({ format: 'html' });
var doc = new DOMParser().parseFromString(text, 'text/html');
text = doc.body.textContent.replace("\r\n", "\\r\\n");
In case you still want to have tags within the code to display aswell as the link as the text within the tag add this line before the DOMParser:
text = text.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, ' $2: $1 ');
Which will change:
Go to some page
to this:
Go to some page: //some.link
Cheers!
To make sure things like this don't happen you should use a <br> tag instead of a regular line brea (ctrl+return). This is the default procedure in html code.
I got around it by using DOMParser().
tinymceContextValue = tinymce.get('abstract1').getContent({format: 'html'});
var doc = new DOMParser().parseFromString(tinymceContextValue ,'text/html');
tinymceContextValue = doc.body.textContent;
In this case, we could still preserve the line break. But be noted that do not change the tinymce configuration to enable . It will not work with it.
I have a textareaField in Silverstripe Backend in Edit Page View... The text to insert contains linebreaks. If I save the Page the text shows correctly with linebreaks in the textareaField. The linebreaks are for sure saved correctly to the database. But how do I display the text correctly in the frontend? It´s always outputted without linebreaks in a single line.
I tried already $Text.RAW, $Text.XML,... nothing works.
Thanks for the help,
Kind regards,
Florian
Assuming that you are using 3.0 this is a bug. You can see it here http://open.silverstripe.org/ticket/7596
A work around is to write your own function calling nl2br on your text field.
Here is an example:
public function NiceDescription () {
return (nl2br (Convert::raw2xml ($this->Description), true));
}
You can replace "Description" with the name of your text property.
Then in your template file if you need to display the description field you will call the function:
$NiceDescription
to visually render the newlines in html, you need to convert them to <BR> tags.
see http://php.net/manual/de/function.nl2br.php
I'm working on a site in EPiServer, and whenever I create a page property with the type set to "XHTML string" (which uses the WYSIWYG content editor in Edit mode), it wraps all content in <p> tags.
Is there any way to prevent this from happening? I can't remove the paragraph margins universally through my CSS (e.g. p {margin: 0 !important;}) since I do need the margins for actual paragraphs of text. I've even tried going to the HTML source view in the editor and manually deleting the <p> tags that it generates, but it immediately adds them back in when I save!
It doesn't happen when the property type is either a long or short string, but that's not always an option since the content might contain images, dynamic controls, etc.
This is becoming a real nuisance since it's very hard to achieve the layout I need when basically every element on the page has extra margins applied to it.
As Johan is saying, they are there for a reason - see more info here. That being said, it's not impossible to remove them. It can be done in one of two ways (taken from world.episerver.com:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
myEditor.InitOptions["force_p_newlines"] = "false";
}
or
<script type="text/javascript">
tinyMCE.init({
force_p_newlines: false
});
</script>
You can add your own custom TinyMCE-config that removes P-elements or strip them out using regular expressions either when saving the page or when rendering the property/page.
I think it's a bad idea though. P-elements are what the editors generate the most and in most cases their content is also semantically correct. Better to wrap your property in a div with a class and adjust margins using CSS like you mention.
If you're using a version of EPiServer with TinyMCE editors, you can insert <br /> elements instead of <p> elements if you type shift-enter instead of enter. This should eliminate your margin problems.
More info at the link below:
http://www.tinymce.com/wiki.php/TinyMCE_FAQ#TinyMCE_produce_P_elements_on_enter.2Freturn_instead_of_BR_elements.3F
EDIT: My comment below answers his question better.
I discovered that while I can't remove the <p> tags from the source view (because it adds them back in automatically), if I replace them with <div> tags, it'll leave things alone. It does mean that I've got an extra <div> wrapping some elements that I don't really need, but at least a <div> doesn't add margins like a <p> does, so...good enough!
GWT Label widgets iterprets everything as text, not as html tags - that's good, but I would like it to interpret \n as a <br /> how do i do that.
I would make subclass, but I cant find what to override to achieve this behaviour
(I could use HTML widget, but it would interpret all tags - and all I need is an line brak)
Use an HTML widget and set its value using a SafeHtml constructed with SafeHtmlBuilder.appendEscapedLines:
HTML label = new HTML(new SafeHtmlBuilder().appendEscapedLines("foo<bar\nbaz>quux").toSafeHtml());
(alternatively, you can split("\n", -1) your text, call SafeHtml.htmlEscape on each part and join them back with a <br>, that's what appendEscapedLines does)
Another option is to use CSS, which may be sufficient in some cases where this problem emerges.
Add the CSS attribute white-space: pre or pre-wrap in the area where you display the text. It will ensure that line breaks are respected when rendering the document.
This approach has the potential to reduce some complexity, e.g. the processing of input where \n is replaced with <br/>.
You can use com.google.gwt.user.client.ui.HTML class to achieve this or simply write,
Label label = new HTML("// html code you wnat to write");
The problem to display multi-line text with HTML in XML files is that we are not allowed to use character < in the content. For example the below code cannot be compiled:
<g:HTML HTML="Line 1<br />Line2<br />Line 3" />
In my case, I declare that text as a i18n string then use in html
<ui:with field='ln' type='com.mycompany.i18n.LocalizableStrings'/>
<g:HTML HTML="{ln.EXPLAINATION}" />