@rspress/core 1.7.5 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/loader.js CHANGED
@@ -3658,11 +3658,26 @@ function loadFrontMatter(source, filepath, root, outputWarning = false) {
3658
3658
  return { frontmatter, content };
3659
3659
  }
3660
3660
 
3661
+ // src/node/utils/extractTextAndId.ts
3662
+ var extractTextAndId = (title) => {
3663
+ const customIdReg = /{#.*}/;
3664
+ const text = title.replace(customIdReg, "").trimEnd();
3665
+ const customId = title.match(customIdReg)?.[0]?.slice(2, -1) || "";
3666
+ return [text, customId];
3667
+ };
3668
+
3669
+ // src/node/utils/escapeHeadingIds.ts
3670
+ function escapeMarkdownHeadingIds(content) {
3671
+ const markdownHeadingRegexp = /(?:^|\n)#{1,6}(?!#).*/g;
3672
+ return content.replace(
3673
+ markdownHeadingRegexp,
3674
+ (substring) => substring.replace("{#", "\\{#").replace("\\\\{#", "\\{#")
3675
+ );
3676
+ }
3677
+
3661
3678
  // src/node/mdx/options.ts
3662
3679
  import path6 from "path";
3663
3680
  import remarkGFM from "remark-gfm";
3664
- import rehypeSlug from "rehype-slug";
3665
- import rehypeAutoLink from "rehype-autolink-headings";
3666
3681
  import rehypePluginExternalLinks from "rehype-external-links";
3667
3682
 
3668
3683
  // src/node/mdx/remarkPlugins/toc.ts
@@ -3680,16 +3695,19 @@ var parseToc = (tree) => {
3680
3695
  title = node.children[0].value;
3681
3696
  }
3682
3697
  if (node.depth > 1 && node.depth < 5) {
3683
- const originText = node.children.map((child) => {
3698
+ let customId = "";
3699
+ const text = node.children.map((child) => {
3684
3700
  if (child.type === "link") {
3685
3701
  return child.children?.map((item) => item.value).join("");
3686
3702
  } else {
3687
- return child.value;
3703
+ const [textPart, idPart] = extractTextAndId(child.value);
3704
+ customId = idPart;
3705
+ return textPart;
3688
3706
  }
3689
3707
  }).join("");
3690
- const id = slugger.slug(originText);
3708
+ const id = customId ? customId : slugger.slug(text);
3691
3709
  const { depth } = node;
3692
- toc.push({ id, text: originText, depth });
3710
+ toc.push({ id, text, depth });
3693
3711
  }
3694
3712
  })(tree);
3695
3713
  return {
@@ -3722,13 +3740,69 @@ var rehypePluginCodeMeta = () => {
3722
3740
  };
3723
3741
  };
3724
3742
 
3743
+ // src/node/mdx/rehypePlugins/headerAnchor.ts
3744
+ import { headingRank } from "hast-util-heading-rank";
3745
+ import { visit as visit2 } from "unist-util-visit";
3746
+ import GithubSlugger from "github-slugger";
3747
+ var rehypeHeaderAnchor = () => {
3748
+ const slugger = new GithubSlugger();
3749
+ return function(tree) {
3750
+ visit2(tree, "element", function(node) {
3751
+ if (headingRank(node)) {
3752
+ if (!node.properties.id) {
3753
+ const [text, customId] = collectHeaderText(node);
3754
+ node.properties.id = customId || slugger.slug(text);
3755
+ }
3756
+ node.children.unshift(create(node));
3757
+ }
3758
+ });
3759
+ };
3760
+ };
3761
+ var collectHeaderText = (node) => {
3762
+ let text = "";
3763
+ let id = "";
3764
+ node.children.forEach((child) => {
3765
+ if (child.type === "text") {
3766
+ const [textPart, idPart] = extractTextAndId(child.value);
3767
+ child.value = textPart;
3768
+ text += textPart;
3769
+ id = idPart;
3770
+ }
3771
+ if (child.type === "element") {
3772
+ child.children.forEach((c) => {
3773
+ if (c.type === "text") {
3774
+ text += c.value;
3775
+ }
3776
+ });
3777
+ }
3778
+ });
3779
+ return [text, id];
3780
+ };
3781
+ function create(node) {
3782
+ return {
3783
+ type: "element",
3784
+ tagName: "a",
3785
+ properties: {
3786
+ class: "header-anchor",
3787
+ ariaHidden: "true",
3788
+ href: `#${node.properties.id}`
3789
+ },
3790
+ children: [
3791
+ {
3792
+ type: "text",
3793
+ value: "#"
3794
+ }
3795
+ ]
3796
+ };
3797
+ }
3798
+
3725
3799
  // src/node/mdx/remarkPlugins/normalizeLink.ts
3726
3800
  import path3 from "path";
3727
- import { visit as visit2 } from "unist-util-visit";
3801
+ import { visit as visit3 } from "unist-util-visit";
3728
3802
  import { normalizeHref, parseUrl, isExternalUrl, slash as slash2 } from "@rspress/shared";
3729
3803
  var remarkPluginNormalizeLink = ({ base, root, cleanUrls }) => (tree, file) => {
3730
3804
  const images = [];
3731
- visit2(
3805
+ visit3(
3732
3806
  tree,
3733
3807
  (node) => node.type === "link",
3734
3808
  (node) => {
@@ -3790,7 +3864,7 @@ var remarkPluginNormalizeLink = ({ base, root, cleanUrls }) => (tree, file) => {
3790
3864
  }
3791
3865
  };
3792
3866
  };
3793
- visit2(tree, "image", (node) => {
3867
+ visit3(tree, "image", (node) => {
3794
3868
  const { url } = node;
3795
3869
  if (!url) {
3796
3870
  return;
@@ -3815,7 +3889,7 @@ var remarkPluginNormalizeLink = ({ base, root, cleanUrls }) => (tree, file) => {
3815
3889
  });
3816
3890
  images.push(getASTNodeImport(tempVariableName, imagePath));
3817
3891
  });
3818
- visit2(tree, ["mdxJsxFlowElement", "mdxJsxTextElement"], (node) => {
3892
+ visit3(tree, ["mdxJsxFlowElement", "mdxJsxTextElement"], (node) => {
3819
3893
  if (node.name !== "img") {
3820
3894
  return;
3821
3895
  }
@@ -3836,7 +3910,7 @@ var remarkPluginNormalizeLink = ({ base, root, cleanUrls }) => (tree, file) => {
3836
3910
 
3837
3911
  // src/node/mdx/remarkPlugins/checkDeadLink.ts
3838
3912
  import path4 from "path";
3839
- import { visit as visit3 } from "unist-util-visit";
3913
+ import { visit as visit4 } from "unist-util-visit";
3840
3914
  import logUtils from "@modern-js/utils/logger";
3841
3915
  import { cleanUrl, isProduction } from "@rspress/shared";
3842
3916
 
@@ -3873,7 +3947,7 @@ var remarkCheckDeadLinks = (checkLink) => {
3873
3947
  const { root, routeService } = checkLink;
3874
3948
  return (tree, vfile) => {
3875
3949
  const internalLinks = /* @__PURE__ */ new Set();
3876
- visit3(tree, "link", (node) => {
3950
+ visit4(tree, "link", (node) => {
3877
3951
  const { url } = node;
3878
3952
  if (!url) {
3879
3953
  return;
@@ -3963,20 +4037,7 @@ async function createMDXOptions(docDirectory, config, checkDeadLinks, routeServi
3963
4037
  ...remarkPluginsFromPlugins
3964
4038
  ].filter(Boolean),
3965
4039
  rehypePlugins: [
3966
- rehypeSlug,
3967
- [
3968
- rehypeAutoLink,
3969
- {
3970
- properties: {
3971
- class: "header-anchor",
3972
- ariaHidden: "true"
3973
- },
3974
- content: {
3975
- type: "text",
3976
- value: "#"
3977
- }
3978
- }
3979
- ],
4040
+ rehypeHeaderAnchor,
3980
4041
  rehypePluginCodeMeta,
3981
4042
  [
3982
4043
  rehypePluginExternalLinks,
@@ -4014,6 +4075,7 @@ async function mdxLoader(context, source, callback) {
4014
4075
  docDirectory,
4015
4076
  true
4016
4077
  );
4078
+ const preprocessedContent = escapeMarkdownHeadingIds(content);
4017
4079
  const isHomePage = frontmatter.pageType === "home";
4018
4080
  try {
4019
4081
  let compileResult;
@@ -4032,7 +4094,7 @@ async function mdxLoader(context, source, callback) {
4032
4094
  title: ""
4033
4095
  });
4034
4096
  const vFile = await compiler.process({
4035
- value: content,
4097
+ value: preprocessedContent,
4036
4098
  path: filepath
4037
4099
  });
4038
4100
  compileResult = String(vFile);
@@ -4043,7 +4105,7 @@ async function mdxLoader(context, source, callback) {
4043
4105
  } else {
4044
4106
  const { compile } = __require("@rspress/mdx-rs");
4045
4107
  const { toc, links, title, code } = await compile({
4046
- value: content,
4108
+ value: preprocessedContent,
4047
4109
  filepath,
4048
4110
  root: docDirectory,
4049
4111
  development: process.env.NODE_ENV !== "production"