starlight-obsidian 0.8.1 → 0.8.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # starlight-obsidian
2
2
 
3
+ ## 0.8.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#51](https://github.com/HiDeoo/starlight-obsidian/pull/51) [`b067e02`](https://github.com/HiDeoo/starlight-obsidian/commit/b067e026613abe8aaed9b3de673a5ae93e70525e) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes an issue with embedded note sections not being rendered correctly.
8
+
3
9
  ## 0.8.1
4
10
 
5
11
  ### Patch Changes
package/libs/remark.ts CHANGED
@@ -20,7 +20,7 @@ import type {
20
20
  import { findAndReplace } from 'mdast-util-find-and-replace'
21
21
  import { toHast } from 'mdast-util-to-hast'
22
22
  import { customAlphabet } from 'nanoid'
23
- import { CONTINUE, SKIP, visit } from 'unist-util-visit'
23
+ import { CONTINUE, EXIT, SKIP, visit } from 'unist-util-visit'
24
24
  import type { VFile } from 'vfile'
25
25
  import yaml from 'yaml'
26
26
 
@@ -669,9 +669,13 @@ function getCustomFileNode(filePath: string): RootContent {
669
669
  async function getMarkdownFileNode(file: VFile, fileUrl: string): Promise<RootContent> {
670
670
  ensureTransformContext(file)
671
671
 
672
+ const [fileName, ...anchorSegments] = fileUrl.split('#')
673
+ const fileAnchor = anchorSegments.join('#')
672
674
  const fileExt = file.data.vault.options.linkSyntax === 'wikilink' ? '.md' : ''
673
675
  const filePath = decodeURIComponent(
674
- file.data.vault.options.linkFormat === 'relative' ? getRelativeFilePath(file, fileUrl) : fileUrl,
676
+ file.data.vault.options.linkFormat === 'relative'
677
+ ? getRelativeFilePath(file, fileName ?? fileUrl)
678
+ : (fileName ?? fileUrl),
675
679
  )
676
680
  const url = path.posix.join(path.posix.sep, `${filePath}${fileExt}`)
677
681
  const matchingFile = file.data.files.find(
@@ -685,6 +689,10 @@ async function getMarkdownFileNode(file: VFile, fileUrl: string): Promise<RootCo
685
689
  const content = fs.readFileSync(matchingFile.fsPath, 'utf8')
686
690
  const root = await transformMarkdownToAST(matchingFile.fsPath, content, { ...file.data, embedded: true })
687
691
 
692
+ if (fileAnchor) {
693
+ root.children = extractMarkdownSection(root, fileAnchor)
694
+ }
695
+
688
696
  return {
689
697
  type: 'blockquote',
690
698
  children: [
@@ -705,6 +713,35 @@ function replaceNode({ index, parent }: VisitorContext, replacement: RootContent
705
713
  parent.children.splice(index, 1, ...(Array.isArray(replacement) ? replacement : [replacement]))
706
714
  }
707
715
 
716
+ function extractMarkdownSection(root: Root, sectionAnchor: string) {
717
+ const children: Root['children'] = []
718
+
719
+ visit(root, (node, index, parent) => {
720
+ switch (node.type) {
721
+ case 'heading': {
722
+ if (!parent || index === undefined) return CONTINUE
723
+ const headingText = node.children.find((child) => child.type === 'text')?.value
724
+ if (headingText !== sectionAnchor) return CONTINUE
725
+
726
+ children.push(node)
727
+
728
+ let nextNode = parent.children[index + 1]
729
+ while (nextNode && (nextNode.type !== 'heading' || nextNode.depth > node.depth)) {
730
+ children.push(nextNode)
731
+ nextNode = parent.children[index + children.length]
732
+ }
733
+
734
+ return EXIT
735
+ }
736
+ default: {
737
+ return CONTINUE
738
+ }
739
+ }
740
+ })
741
+
742
+ return children
743
+ }
744
+
708
745
  // We are using `Html` node instead of real MDX nodes because we are not using `remark-mdx` due to the fact that it
709
746
  // makes the parsing step way more strict. During our inital testing round, we found out that a few users had pretty
710
747
  // poorly formatted Markdown files (usually the result of various Obisidian migration tools) and we wanted to make sure
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-obsidian",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "license": "MIT",
5
5
  "description": "Starlight plugin to publish Obsidian vaults.",
6
6
  "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",