@luckyfishes/markdown-core 0.2.1 → 0.2.2

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/index.cjs CHANGED
@@ -701,6 +701,62 @@ var legacyGuardBlockHandler = {
701
701
 
702
702
  // src/remark/custom-syntax/handlers/tabs-block.ts
703
703
  var TABS_START_RE = /^::tabs\s*$/i;
704
+ var FOOTNOTE_REF_PLACEHOLDER_RE = /@@MARKDOWN_CORE_FOOTNOTE_REF:([^@\s]+)@@/g;
705
+ function protectFootnoteReferences(markdown) {
706
+ return markdown.replace(/\[\^([^\]\s]+)\](?!:)/g, (_, identifier) => {
707
+ return `@@MARKDOWN_CORE_FOOTNOTE_REF:${identifier}@@`;
708
+ });
709
+ }
710
+ function restoreFootnoteReferencesInValue(value) {
711
+ if (!FOOTNOTE_REF_PLACEHOLDER_RE.test(value)) {
712
+ FOOTNOTE_REF_PLACEHOLDER_RE.lastIndex = 0;
713
+ return null;
714
+ }
715
+ FOOTNOTE_REF_PLACEHOLDER_RE.lastIndex = 0;
716
+ const nextChildren = [];
717
+ let lastIndex = 0;
718
+ for (const match of value.matchAll(FOOTNOTE_REF_PLACEHOLDER_RE)) {
719
+ const start = match.index ?? 0;
720
+ const identifier = match[1];
721
+ if (start > lastIndex) {
722
+ nextChildren.push({
723
+ type: "text",
724
+ value: value.slice(lastIndex, start)
725
+ });
726
+ }
727
+ if (identifier) {
728
+ nextChildren.push({
729
+ type: "footnoteReference",
730
+ identifier,
731
+ label: identifier
732
+ });
733
+ }
734
+ lastIndex = start + match[0].length;
735
+ }
736
+ if (lastIndex < value.length) {
737
+ nextChildren.push({
738
+ type: "text",
739
+ value: value.slice(lastIndex)
740
+ });
741
+ }
742
+ return nextChildren;
743
+ }
744
+ function restoreFootnoteReferences(nodes) {
745
+ return nodes.flatMap((node) => {
746
+ if (node.type === "text" && typeof node.value === "string") {
747
+ return restoreFootnoteReferencesInValue(node.value) ?? [node];
748
+ }
749
+ if ("children" in node && Array.isArray(node.children)) {
750
+ return [
751
+ {
752
+ ...node,
753
+ children: restoreFootnoteReferences(node.children)
754
+ }
755
+ ];
756
+ }
757
+ return [node];
758
+ });
759
+ }
704
760
  function findTabsEnd(lines, startLine) {
705
761
  let cursor = startLine + 1;
706
762
  let nestedDepth = 0;
@@ -851,7 +907,9 @@ var tabsBlockHandler = {
851
907
  (tab) => context.createFlowElement(
852
908
  context.componentNames.tabsContent,
853
909
  { value: tab.value },
854
- [...context.transformFragment(tab.content)]
910
+ restoreFootnoteReferences(
911
+ context.transformFragment(protectFootnoteReferences(tab.content))
912
+ )
855
913
  )
856
914
  );
857
915
  return {
package/dist/index.js CHANGED
@@ -654,6 +654,62 @@ var legacyGuardBlockHandler = {
654
654
 
655
655
  // src/remark/custom-syntax/handlers/tabs-block.ts
656
656
  var TABS_START_RE = /^::tabs\s*$/i;
657
+ var FOOTNOTE_REF_PLACEHOLDER_RE = /@@MARKDOWN_CORE_FOOTNOTE_REF:([^@\s]+)@@/g;
658
+ function protectFootnoteReferences(markdown) {
659
+ return markdown.replace(/\[\^([^\]\s]+)\](?!:)/g, (_, identifier) => {
660
+ return `@@MARKDOWN_CORE_FOOTNOTE_REF:${identifier}@@`;
661
+ });
662
+ }
663
+ function restoreFootnoteReferencesInValue(value) {
664
+ if (!FOOTNOTE_REF_PLACEHOLDER_RE.test(value)) {
665
+ FOOTNOTE_REF_PLACEHOLDER_RE.lastIndex = 0;
666
+ return null;
667
+ }
668
+ FOOTNOTE_REF_PLACEHOLDER_RE.lastIndex = 0;
669
+ const nextChildren = [];
670
+ let lastIndex = 0;
671
+ for (const match of value.matchAll(FOOTNOTE_REF_PLACEHOLDER_RE)) {
672
+ const start = match.index ?? 0;
673
+ const identifier = match[1];
674
+ if (start > lastIndex) {
675
+ nextChildren.push({
676
+ type: "text",
677
+ value: value.slice(lastIndex, start)
678
+ });
679
+ }
680
+ if (identifier) {
681
+ nextChildren.push({
682
+ type: "footnoteReference",
683
+ identifier,
684
+ label: identifier
685
+ });
686
+ }
687
+ lastIndex = start + match[0].length;
688
+ }
689
+ if (lastIndex < value.length) {
690
+ nextChildren.push({
691
+ type: "text",
692
+ value: value.slice(lastIndex)
693
+ });
694
+ }
695
+ return nextChildren;
696
+ }
697
+ function restoreFootnoteReferences(nodes) {
698
+ return nodes.flatMap((node) => {
699
+ if (node.type === "text" && typeof node.value === "string") {
700
+ return restoreFootnoteReferencesInValue(node.value) ?? [node];
701
+ }
702
+ if ("children" in node && Array.isArray(node.children)) {
703
+ return [
704
+ {
705
+ ...node,
706
+ children: restoreFootnoteReferences(node.children)
707
+ }
708
+ ];
709
+ }
710
+ return [node];
711
+ });
712
+ }
657
713
  function findTabsEnd(lines, startLine) {
658
714
  let cursor = startLine + 1;
659
715
  let nestedDepth = 0;
@@ -804,7 +860,9 @@ var tabsBlockHandler = {
804
860
  (tab) => context.createFlowElement(
805
861
  context.componentNames.tabsContent,
806
862
  { value: tab.value },
807
- [...context.transformFragment(tab.content)]
863
+ restoreFootnoteReferences(
864
+ context.transformFragment(protectFootnoteReferences(tab.content))
865
+ )
808
866
  )
809
867
  );
810
868
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luckyfishes/markdown-core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Core Markdown/MDX parsing and AST transformation utilities extracted from noname_blog.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",