starlight-obsidian 0.4.2 → 0.4.3

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/libs/markdown.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { fromMarkdown } from 'mdast-util-from-markdown'
1
2
  import { remark } from 'remark'
2
3
  import remarkFrontmatter from 'remark-frontmatter'
3
4
  import remarkGfm from 'remark-gfm'
@@ -23,8 +24,10 @@ export async function transformMarkdownToString(
23
24
  }
24
25
  }
25
26
 
26
- export function transformMarkdownToAST(filePath: string, markdown: string, context: TransformContext) {
27
- return processor.parse(getVFile(filePath, markdown, context))
27
+ export async function transformMarkdownToAST(filePath: string, markdown: string, context: TransformContext) {
28
+ const { content } = await transformMarkdownToString(filePath, markdown, context)
29
+
30
+ return fromMarkdown(content)
28
31
  }
29
32
 
30
33
  function getVFile(filePath: string, markdown: string, context: TransformContext) {
package/libs/obsidian.ts CHANGED
@@ -2,6 +2,7 @@ import fs from 'node:fs/promises'
2
2
  import path from 'node:path'
3
3
 
4
4
  import { z } from 'astro/zod'
5
+ import decodeUriComponent from 'decode-uri-component'
5
6
  import { slug } from 'github-slugger'
6
7
  import { globby } from 'globby'
7
8
  import yaml from 'yaml'
@@ -108,14 +109,14 @@ export function slugifyObsidianPath(obsidianPath: string) {
108
109
  const isLastSegment = index === segments.length - 1
109
110
 
110
111
  if (!isLastSegment) {
111
- return slug(decodeURIComponent(segment))
112
+ return slug(decodeUriComponent(segment))
112
113
  } else if (isObsidianFile(segment) && !isAssetFile(segment)) {
113
- return decodeURIComponent(segment)
114
+ return decodeUriComponent(segment)
114
115
  } else if (isAssetFile(segment)) {
115
- return `${slug(decodeURIComponent(stripExtension(segment)))}${getExtension(segment)}`
116
+ return `${slug(decodeUriComponent(stripExtension(segment)))}${getExtension(segment)}`
116
117
  }
117
118
 
118
- return slug(decodeURIComponent(stripExtension(segment)))
119
+ return slug(decodeUriComponent(stripExtension(segment)))
119
120
  })
120
121
  .join('/')
121
122
  }
package/libs/remark.ts CHANGED
@@ -53,6 +53,7 @@ export function remarkStarlightObsidian() {
53
53
 
54
54
  handleReplacements(tree, file)
55
55
  await handleMermaid(tree, file)
56
+ await handleImagesAndNoteEmbeds(tree, file)
56
57
 
57
58
  visit(tree, (node, index, parent) => {
58
59
  const context: VisitorContext = { file, index, parent }
@@ -65,9 +66,6 @@ export function remarkStarlightObsidian() {
65
66
  case 'link': {
66
67
  return handleLinks(node, context)
67
68
  }
68
- case 'image': {
69
- return handleImages(node, context)
70
- }
71
69
  case 'blockquote': {
72
70
  return handleBlockquotes(node, context)
73
71
  }
@@ -77,7 +75,10 @@ export function remarkStarlightObsidian() {
77
75
  }
78
76
  })
79
77
 
80
- handleFrontmatter(tree, file, obsidianFrontmatter)
78
+ if (!file.data.embedded) {
79
+ handleFrontmatter(tree, file, obsidianFrontmatter)
80
+ }
81
+
81
82
  handleImports(tree, file)
82
83
  }
83
84
  }
@@ -275,7 +276,7 @@ function handleLinks(node: Link, { file }: VisitorContext) {
275
276
  return SKIP
276
277
  }
277
278
 
278
- function handleImages(node: Image, context: VisitorContext) {
279
+ async function handleImages(node: Image, context: VisitorContext) {
279
280
  const { file } = context
280
281
 
281
282
  ensureTransformContext(file)
@@ -299,7 +300,7 @@ function handleImages(node: Image, context: VisitorContext) {
299
300
  }
300
301
 
301
302
  if (isMarkdownFile(node.url, file)) {
302
- replaceNode(context, getMarkdownFileNode(file, node.url))
303
+ replaceNode(context, await getMarkdownFileNode(file, node.url))
303
304
  return SKIP
304
305
  }
305
306
 
@@ -419,6 +420,21 @@ async function handleMermaid(tree: Root, file: VFile) {
419
420
  )
420
421
  }
421
422
 
423
+ async function handleImagesAndNoteEmbeds(tree: Root, file: VFile) {
424
+ const imageNodes: [node: Image, context: VisitorContext][] = []
425
+
426
+ visit(tree, 'image', (node, index, parent) => {
427
+ imageNodes.push([node, { file, index, parent }])
428
+ return SKIP
429
+ })
430
+
431
+ await Promise.all(
432
+ imageNodes.map(async ([node, context]) => {
433
+ await handleImages(node, context)
434
+ }),
435
+ )
436
+ }
437
+
422
438
  function getFrontmatterNodeValue(file: VFile, obsidianFrontmatter?: ObsidianFrontmatter) {
423
439
  let frontmatter: Frontmatter = {
424
440
  title: file.stem,
@@ -587,7 +603,7 @@ function getCustomFileNode(filePath: string): RootContent {
587
603
  }
588
604
  }
589
605
 
590
- function getMarkdownFileNode(file: VFile, fileUrl: string): RootContent {
606
+ async function getMarkdownFileNode(file: VFile, fileUrl: string): Promise<RootContent> {
591
607
  ensureTransformContext(file)
592
608
 
593
609
  const fileExt = file.data.vault.options.linkSyntax === 'wikilink' ? '.md' : ''
@@ -604,7 +620,7 @@ function getMarkdownFileNode(file: VFile, fileUrl: string): RootContent {
604
620
  }
605
621
 
606
622
  const content = fs.readFileSync(matchingFile.fsPath, 'utf8')
607
- const root = transformMarkdownToAST(matchingFile.fsPath, content, file.data)
623
+ const root = await transformMarkdownToAST(matchingFile.fsPath, content, { ...file.data, embedded: true })
608
624
 
609
625
  return {
610
626
  type: 'blockquote',
@@ -644,6 +660,7 @@ export interface TransformContext {
644
660
  aliases?: string[]
645
661
  assetImports?: [id: string, path: string][]
646
662
  copyStarlightFrontmatter?: boolean
663
+ embedded?: boolean
647
664
  files: VaultFile[]
648
665
  includeKatexStyles?: boolean
649
666
  includeTwitterComponent?: boolean
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-obsidian",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "license": "MIT",
5
5
  "description": "Starlight plugin to publish Obsidian vaults.",
6
6
  "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
@@ -20,6 +20,7 @@
20
20
  "@astro-community/astro-embed-twitter": "0.5.3",
21
21
  "@astro-community/astro-embed-youtube": "0.4.4",
22
22
  "@astrojs/markdown-remark": "4.2.1",
23
+ "decode-uri-component": "0.4.1",
23
24
  "github-slugger": "2.0.0",
24
25
  "globby": "14.0.0",
25
26
  "hast-util-to-html": "9.0.0",
@@ -28,6 +29,7 @@
28
29
  "html-escaper": "3.0.3",
29
30
  "is-absolute-url": "4.0.1",
30
31
  "mdast-util-find-and-replace": "3.0.1",
32
+ "mdast-util-from-markdown": "2.0.0",
31
33
  "mdast-util-to-hast": "13.1.0",
32
34
  "nanoid": "5.0.4",
33
35
  "rehype": "13.0.1",