starlight-obsidian 0.8.0 → 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 +12 -0
- package/libs/markdown.ts +6 -1
- package/libs/remark.ts +39 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
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
|
+
|
|
9
|
+
## 0.8.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#47](https://github.com/HiDeoo/starlight-obsidian/pull/47) [`290b2ee`](https://github.com/HiDeoo/starlight-obsidian/commit/290b2ee81556be17dbd5ba01854696b6e6c540a2) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes a potential issue with autolinks in MDX by enforcing the use of resource links.
|
|
14
|
+
|
|
3
15
|
## 0.8.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/libs/markdown.ts
CHANGED
|
@@ -7,7 +7,12 @@ import { VFile } from 'vfile'
|
|
|
7
7
|
|
|
8
8
|
import { remarkStarlightObsidian, type TransformContext } from './remark'
|
|
9
9
|
|
|
10
|
-
const processor = remark()
|
|
10
|
+
const processor = remark()
|
|
11
|
+
.data('settings', { resourceLink: true })
|
|
12
|
+
.use(remarkGfm)
|
|
13
|
+
.use(remarkMath)
|
|
14
|
+
.use(remarkFrontmatter)
|
|
15
|
+
.use(remarkStarlightObsidian)
|
|
11
16
|
|
|
12
17
|
export async function transformMarkdownToString(
|
|
13
18
|
filePath: string,
|
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'
|
|
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
|