如何将我的降价包裹在HTML Div中?

发布于 2025-02-02 04:51:59 字数 1116 浏览 9 评论 0原文

我正在使用标记 github风味的降价

我有一些工作的降级:

## Test heading
a paragraph.
## second heading
another paragraph

创建的:

<h2 id="test-heading">Test heading</h2>
<p>a paragraph.</p>
<h2 id="second-heading">second heading</h2>
<p>another paragraph</p>

我想在Div中包装该降价部分,例如:

<div class="blog-post">
## Test heading
a paragraph.
## second heading
another paragraph
</div>

但是,这返回以下html:

<div class="blog-post">
## Test heading
a paragraph.
## second heading
another paragraph
</div>

例如,没有降压,字面上的“ ## test标题”出现在html中。

我如何在Div中正确包裹我的降价?

我找到了以下解决方法,但是它很丑陋,而不是实际的解决方案:

<div class="blog-post">
<div></div>

## Test heading
a paragraph.
## second heading
another paragraph

</div>

I am using MarkEd which implements GitHub flavoured markdown.

I have some working markdown:

## Test heading
a paragraph.
## second heading
another paragraph

Which creates:

<h2 id="test-heading">Test heading</h2>
<p>a paragraph.</p>
<h2 id="second-heading">second heading</h2>
<p>another paragraph</p>

I would like to wrap that markdown section in a div, eg:

<div class="blog-post">
## Test heading
a paragraph.
## second heading
another paragraph
</div>

However this returns the following HTML:

<div class="blog-post">
## Test heading
a paragraph.
## second heading
another paragraph
</div>

Eg, no markdown, literally '## Test heading' appears in the HTML.

How can I properly wrap my markdown in a div?

I have found the following workaround, however it is ugly and not an actual fix:

<div class="blog-post">
<div></div>

## Test heading
a paragraph.
## second heading
another paragraph

</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青丝拂面 2025-02-09 04:51:59

Markdown

的Markdown,这是设计。来自 inline html html markdown参考的部分:

请注意,在块级HTML标签中未处理标记格式化语法。例如,您不能在HTML块内使用Markdown风格的强调

但是明确允许使用跨度级标签:

与块级html标签不同,在跨级标签中处理了标记语法。

因此,根据您的用例,您可能会使用span而不是div

commark

如果您使用的库,则使用inmentness commonmark ,您很幸运。示例 108 and 109 表明,如果您在HTML块和Markdown代码之间保持空线,则内容将被解释为Markdown:

<div>

*Emphasized* text.

</div>

应该使用:虽然以下内容不应:

<div>
*Emphasized* text.
</div>

并且,根据参考文献中的同一部分,某些实现在HTML标签上识别一个附加的markdown = 1属性,以启用其内部分析的分析。

虽然它似乎在Stackoverflow中还没有用:

Testing **Markdown** inside a red-background div.

Markdown

For Markdown, This is by design. From the Inline HTML section of the Markdown reference:

Note that Markdown formatting syntax is not processed within block-level HTML tags. E.g., you can’t use Markdown-style emphasis inside an HTML block.

But it is explicitly allowed for span-level tags:

Unlike block-level HTML tags, Markdown syntax is processed within span-level tags.

So, depending on your use-case, you might get away with using a span instead of a div.

CommonMark

If the library you use implements CommonMark, you are lucky. Example 108 and 109 of the spec show that if you keep an empty line in between the HTML block and the markdown code, the contents will be parsed as Markdown:

<div>

*Emphasized* text.

</div>

should work, while the following shouldn't:

<div>
*Emphasized* text.
</div>

And, again according to the same section in the reference, some implementations recognize an additional markdown=1 attribute on the HTML tag to enable parsing of Markdown inside it.

Though it doesn't seem to work in StackOverflow yet:

Testing **Markdown** inside a red-background div.

贪恋 2025-02-09 04:51:59

github页面支持markdown =“ 1”属性属于html元素内的分析降价,例如

<div class="tip" markdown="1">Have **fun!**</div>

注意:截至2019/03起,这在github.com上不起作用,只有github页面。

注意:引用,如markdown =“ 1”,HTML5不需要,但是如果您不使用引号(markdown = 1) ),github不将其识别为HTML。另外,支持现在是越野车。如果您的HTML元素大于单个段落,则可能会获得不正确的输出。例如,由于错误,我无法将降价列表嵌入DIV中。

如果您在markdown =“ 1”不起作用的环境中发现自己,但span确实如此,另一个选项是使用&lt; span style =”显示:block“&gt;,因此块级类别与之兼容,例如

<span style="display:block" class="note">It **works!**</span>

提示: &lt; span class =“ note” /code>比&lt; div class =“ note” markdown =“ 1”&gt;&lt;/div&gt;,因此,如果您控制CSS,则可能更喜欢使用&lt;跨度&gt;并添加显示:block; to css。

GitHub Pages supports the markdown="1" attribute to parse markdown inside HTML elements, e.g.

<div class="tip" markdown="1">Have **fun!**</div>

Note: As of 2019/03, this doesn't work on github.com, only GitHub Pages.

Note: Quotes, as in markdown="1", are not required by HTML5 but if you don't use quotes (markdown=1), GitHub does not recognize it as HTML. Also, support is buggy right now. You will likely get incorrect output if your HTML element is larger than a single paragraph. For example, due to bugs I was unable to embed a Markdown list inside a div.

If you find yourself in an environment in which markdown="1" doesn't work but span does, another option is to use <span style="display:block"> so that block-level classes are compatible with it, e.g.

<span style="display:block" class="note">It **works!**</span>

Tip: <span class="note"></span> is shorter than <div class="note" markdown="1"></div>, so if you control the CSS you might prefer to use <span> and add display: block; to your CSS.

献世佛 2025-02-09 04:51:59

需要额外的额外额外的时间才能在HTML块内进行降级格式化作品,请检查此处所述的文档 - &gt; https://michelf.ca/projects/php-markdown/php-markdown/extra/

Markdown额外
任何块级标签。您可以通过将降价属性添加到
带有值1的标签 - 给出markdown =“ 1”

Markdown Extra is needed to be able to for Markdown formatting works inside an HTML blocks, please check the documentation stated here -> https://michelf.ca/projects/php-markdown/extra/

Markdown Extra gives you a way to put Markdown-formatted text inside
any block-level tag. You do this by adding a markdown attribute to the
tag with the value 1 — which gives markdown="1"

拥抱影子 2025-02-09 04:51:59

最后一个度假胜地选项:

某些库可能对案例敏感。

尝试&lt; div&gt;,而不是&lt; div&gt;,看看会发生什么。

MarkdownSharp具有此特征 - 尽管在Stackoverflow上它们无论如何都会剥离所有Divs,因此不要指望它在这里工作。

Last resort option:

Some libraries may be case sensitive.

Try <DIV> instead of <div> and see what happens.

Markdownsharp has this characteristic - although on StackOverflow they strip out all DIVs anyway so don't expect it to work here.

心在旅行 2025-02-09 04:51:59

在我的情况下(在GitHub上),当我在HTML标签和Markdown Text之间添加Newline时,解决了问题。

In my case (on GitHub), the problem was resolved when I added newline between html tags and markdown text.

enter image description here

萝莉病 2025-02-09 04:51:59

通过查看扩展标记的并修改html < html <渲染器方法,您可以执行类似的操作以用解析的降价替换标签之间的零件。我没有进行大量测试,但它可以与我的第几次尝试一起使用。

const marked = require('marked');
const renderer = new marked.Renderer();
renderer.html = (mixedContent) => mixedContent.replace(/[^<>]+?(?=<)/g, (match) => {
    const tokens = marked.lexer(match);
    return marked.parser(tokens);
});

编辑

此新的正则表达式将确保仅使用IT和HTML标签之间的线条进行降压。

const marked = require('marked');
const renderer = new marked.Renderer();
renderer.html = (mixedContent) => mixedContent.replace(/\n\n[^<>]+?\n\n(?=<)/g, (match) => {
    const tokens = marked.lexer(match);
    return marked.parser(tokens);
});

By looking at the docs for Extending Marked and modifying the html renderer method, you can do something like this to replace the parts between tags with parsed markdown. I haven't done extensive testing, but it worked with my first few attempts.

const marked = require('marked');
const renderer = new marked.Renderer();
renderer.html = (mixedContent) => mixedContent.replace(/[^<>]+?(?=<)/g, (match) => {
    const tokens = marked.lexer(match);
    return marked.parser(tokens);
});

Edit

this new regex will ensure that only markdown with lines between it and the html tags will be parsed.

const marked = require('marked');
const renderer = new marked.Renderer();
renderer.html = (mixedContent) => mixedContent.replace(/\n\n[^<>]+?\n\n(?=<)/g, (match) => {
    const tokens = marked.lexer(match);
    return marked.parser(tokens);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文