英文:
markdown generated html is not styled by the styles specified by css
问题
你好,
我正在尝试使用Svelte创建一个Markdown预览器。我正在使用以下库来执行不同的任务:
- Marked库用于解析Markdown,
- Highlight.js用于高亮代码块,
- Sanitize-html用于清理生成的HTML以防止XSS攻击。
然而,我遇到了一个问题,即我应用于生成的HTML的自定义CSS类没有生效。以下是我正在使用的代码:
```html
<script lang="ts">
  import sanitizeHtml from 'sanitize-html';
  import { marked } from 'marked';
  import hljs from 'highlight.js';
  import { afterUpdate } from 'svelte';
  import 'highlight.js/styles/atom-one-dark.css';
  let markdown = `# 你好,世界`;
  $: html = sanitizeHtml(marked(markdown));
  afterUpdate(() => hljs.highlightAll());
  let edit = true;
</script>
<div class="container">
  <div class="editor">
    {#if edit}
      <textarea spellcheck="false" bind:value={markdown} />
    {:else}
      <div class="viewer">
        {@html html}
      </div>
    {/if}
  </div>
  <div>
    <input type="checkbox" bind:checked={edit} />
  </div>
</div>
<style>
.viewer h1 {
  font-size: 24px;
  color: #ff0000;
}
</style>
生成的h1元素没有样式
我以为可能是由于依赖冲突,所以尝试移除了所有依赖项,但仍然没有应用类。
<details>
<summary>英文:</summary>
Good day,
I am trying to create a markdown previewer using Svelte. I'm using the following libraries for different tasks:
- Marked library for parsing markdown,
- Highlight.js for highlighting code blocks,
- Sanitize-html for sanitizing the generated HTML to prevent XSS attacks.
However, I'm facing an issue where the custom CSS classes I apply to the generated HTML are not being applied. Here is the code I'm using:
```html
<script lang="ts">
  import sanitizeHtml from 'sanitize-html';
  import { marked } from 'marked';
  import hljs from 'highlight.js';
  import { afterUpdate } from 'svelte';
  import 'highlight.js/styles/atom-one-dark.css';
  let markdown = `# Hello World`;
  $: html = sanitizeHtml(marked(markdown));
  afterUpdate(() => hljs.highlightAll());
  let edit = true;
</script>
<div class="container">
  <div class="editor">
    {#if edit}
      <textarea spellcheck="false" bind:value={markdown} />
    {:else}
      <div class="viewer">
        {@html html}
      </div>
    {/if}
  </div>
  <div>
    <input type="checkbox" bind:checked={edit} />
  </div>
</div>
<style>
.viewer h1 {
  font-size: 24px;
  color: #ff0000;
}
</style>
The generated h1 element is not styled
I thought it might be because of the dependencies conflicts so I tried removing every dependencies but still no class is applied.
答案1
得分: 1
Svelte样式默认是有作用域的,您需要使用:global()来阻止作用域,因为通过@html生成的HTML将缺少用于作用域的类,例如:
.viewer :global(h1) { ... }
(如果您使用svelte-preprocess,您可以创建整个带有嵌套规则的:global块以减少冗余,这可能需要安装额外的预处理器,如PostCSS、SASS或LESS。)
英文:
Svelte styles are scoped by default, you need to use :global() to prevent scoping in this case as the HTML generated via @html will lack the classes used for scoping, e.g.
.viewer :global(h1} { ... }
(If you use svelte-preprocess you can create entire :global blocks with nested rules to reduce redundancy, this may require an additional pre-processor  like PostCSS, SASS or LESS to be installed.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论