-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
third work session, make remark plugin which extracts text by visitin…
…g mdast and hast
- Loading branch information
1 parent
a8d993c
commit 9fccff9
Showing
8 changed files
with
314 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
title: starlight-spell-checker | ||
description: Check your documentation for spelling mistakes; multilingual support. | ||
head: | ||
- tag: title | ||
content: starlight-spell-checker | ||
template: splash | ||
editUrl: false | ||
hero: | ||
tagline: Check your documentation for spelling mistakes; multilingual support. | ||
image: | ||
file: ../../../assets/houston.webp | ||
actions: | ||
- text: Get Started | ||
link: /getting-started/ | ||
icon: right-arrow | ||
draft: true | ||
--- | ||
|
||
import { Card, CardGrid } from '@astrojs/starlight/components' | ||
|
||
## Next steps | ||
|
||
<CardGrid stagger> | ||
<Card title="Install the plugin" icon="puzzle"> | ||
In der [Startanleitung](/getting-started/) findest du Anweisungen zur Installation. | ||
</Card> | ||
<Card title="Configure the plugin" icon="setting"> | ||
Bearbeite deine Konfiguration in der Datei `astro.config.mjs`. | ||
</Card> | ||
</CardGrid> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import "mdast-util-mdx-jsx"; | ||
|
||
import nodePath from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
import { hasProperty } from "hast-util-has-property"; | ||
import type { Nodes } from "hast"; | ||
import { fromHtml } from "hast-util-from-html"; | ||
import { slug } from "github-slugger"; | ||
import type { Root } from "mdast"; | ||
import { unified, type Plugin } from "unified"; | ||
import { visit } from "unist-util-visit"; | ||
|
||
import { ensureTrailingSlash, stripLeadingSlash } from "./path"; | ||
|
||
// All the text content keyed by file path. | ||
const contents: Contents = new Map(); | ||
|
||
export const remarkStarlightSpellChecker: Plugin< | ||
[{ base: string; srcDir: URL }], | ||
Root | ||
> = function ({ base, srcDir }) { | ||
return async (tree, file) => { | ||
if (file.data.astro?.frontmatter?.["draft"]) return; | ||
|
||
const filePath = normalizeFilePath(base, srcDir, file.history[0]); | ||
const slug: string | undefined = | ||
typeof file.data.astro?.frontmatter?.["slug"] === "string" | ||
? file.data.astro.frontmatter["slug"] | ||
: undefined; | ||
|
||
let fileContent: string = ""; | ||
|
||
// https://github.com/syntax-tree/mdast#nodes | ||
// https://github.com/syntax-tree/mdast-util-mdx-jsx#nodes | ||
visit( | ||
tree, | ||
["text", "inlineCode", "paragraph", "heading", "html"], | ||
(node) => { | ||
switch (node.type) { | ||
case "text": | ||
fileContent += node.value; | ||
break; | ||
case "inlineCode": | ||
fileContent += "`" + node.value + "`"; | ||
break; | ||
case "paragraph": | ||
fileContent += "\n"; | ||
break; | ||
case "heading": | ||
fileContent += "\n"; | ||
break; | ||
// case "html": { | ||
// const htmlTree = fromHtml(node.value, { fragment: true }); | ||
|
||
// visit(htmlTree, ["text"], (htmlNode: Nodes) => { | ||
// fileContent += htmlNode.value; | ||
// }); | ||
|
||
// break; | ||
// } | ||
} | ||
} | ||
); | ||
|
||
contents.set(getFilePath(base, filePath, slug), fileContent); | ||
}; | ||
}; | ||
|
||
export function getValidationData() { | ||
return { contents }; | ||
} | ||
|
||
function getFilePath(base: string, filePath: string, slug: string | undefined) { | ||
if (slug) { | ||
return nodePath.posix.join( | ||
stripLeadingSlash(base), | ||
stripLeadingSlash(ensureTrailingSlash(slug)) | ||
); | ||
} | ||
|
||
return filePath; | ||
} | ||
|
||
function normalizeFilePath(base: string, srcDir: URL, filePath?: string) { | ||
if (!filePath) { | ||
throw new Error("Missing file path to validate links."); | ||
} | ||
|
||
const path = nodePath | ||
.relative(nodePath.join(fileURLToPath(srcDir), "content/docs"), filePath) | ||
.replace(/\.\w+$/, "") | ||
.replace(/(^|[/\\])index$/, "") | ||
.replace(/[/\\]?$/, "/") | ||
.split(/[/\\]/) | ||
.map((segment) => slug(segment)) | ||
.join("/"); | ||
|
||
if (base !== "/") { | ||
return nodePath.posix.join(stripLeadingSlash(base), path); | ||
} | ||
|
||
return path; | ||
} | ||
|
||
// function isMdxIdAttribute( | ||
// attribute: MdxJsxAttribute | MdxJsxExpressionAttribute | ||
// ): attribute is MdxIdAttribute { | ||
// return ( | ||
// attribute.type === "mdxJsxAttribute" && | ||
// attribute.name === "id" && | ||
// typeof attribute.value === "string" | ||
// ); | ||
// } | ||
|
||
export type Contents = Map<string, string>; | ||
|
||
// interface MdxIdAttribute { | ||
// name: "id"; | ||
// type: "mdxJsxAttribute"; | ||
// value: string; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.