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

[Bug]: ID is missing when the title is used with the component #1361

Open
bosens-China opened this issue Aug 28, 2024 · 4 comments
Open

[Bug]: ID is missing when the title is used with the component #1361

bosens-China opened this issue Aug 28, 2024 · 4 comments
Labels
🐞 bug Something isn't working

Comments

@bosens-China
Copy link

版本信息

System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 76.27 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 128.0.6613.85
    Safari: 17.6
  npmPackages:
    rspress: ^1.28.0 => 1.28.2

问题详情

image
image

复现链接

none

复现步骤

不需要

@bosens-China bosens-China added the 🐞 bug Something isn't working label Aug 28, 2024
@Timeless0911
Copy link
Collaborator

Timeless0911 commented Aug 28, 2024

Try to write # inside your component code instead of outside.

@bosens-China
Copy link
Author

😀明天试一下这个

@bosens-China
Copy link
Author

bosens-China commented Aug 29, 2024

  • docs/components/TextVocabularyOutput/index.tsx
import { Root } from "../../types/textTypes";

interface Props {
  text: string;
  vocabulary: Root["vocabulary"];
}

const textContrast = (a: string, b: string) => {
  return new RegExp(a, "i").test(b);
};

/**
 * 输出文本,如果包含本课的新词会重点描述
 * @param param0
 */
export const TextVocabularyOutput: React.FC<Props> = ({ text, vocabulary }) => {
  const arr = text
    .split(/\s/g)
    .map((item, index, array) => {
      const isEnd = array.length === index - 1;
      const text = isEnd ? "" : " ";
      if (vocabulary.find((f) => textContrast(f.word, item))) {
        return [<span key={item}>{item}</span>, text];
      }
      return [item, text];
    })
    .flat(2);

  return <># {arr}</>;
};

在mdx中如果这样调用

# Lesson 1 Excuse me! 对不起!

<TextVocabularyOutput text={data.title} vocabulary={data.vocabulary} />

输出

image

@Timeless0911
Copy link
Collaborator

If you using .tsx, return <># {arr}</>; means return simple text.

The tsx component should not be used to generate markdown headers and more dynamic markdown/html structures, which will lack a lot of metadata information when analyzing at build time. The correct usage of the tsx component should be to write a component written by react that not return markdown structure.

If you use the original method in your issue screenshot, the id and href attributes cannot be generated when mdx is compiled.

@Timeless0911 Timeless0911 changed the title [Bug]: 标题配合组件使用,丢失id [Bug]: ID is missing when the title is used with the component Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants