Skip to content

Commit 79f2982

Browse files
committed
Sort of figure out what’s up with remark
1 parent 2bf1caa commit 79f2982

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
order: -2
3+
---
4+
# Using CommonMark
5+
6+
{#normal-italic-bold}
7+
normal _italic_ **bold**

package-lock.json

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/input/parseFile.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import { basename } from "path";
55
import { fromPandoc, parse } from "@djot/djot";
66
import { mystParse } from "myst-parser";
77
import remarkParse from "remark-parse";
8-
import remarkGfm from "remark-gfm";
98
import { unified } from "unified";
109
import yaml from "js-yaml";
1110

12-
import { DjockeyConfig, DjockeyDoc, PolyglotDoc } from "../types.js";
11+
import {
12+
DjockeyConfig,
13+
DjockeyDoc,
14+
PolyglotDoc,
15+
PolyglotDoc_MDAST,
16+
} from "../types.js";
1317
import { getPandocAST } from "../pandoc.js";
1418
import { getInputFormatForFileName } from "./fileExtensions.js";
1519
import { LogCollector } from "../utils/logUtils.js";
1620
import { fsbase, fsext, fsname, fssplit, refjoin } from "../utils/pathUtils.js";
21+
import { Root } from "mdast";
1722

1823
function removeExtensionFromPath(path_: string): string {
1924
return path_.slice(0, path_.length - path.parse(path_).ext.length);
@@ -48,6 +53,8 @@ export async function parseFile(
4853

4954
let polyglotDoc: PolyglotDoc | undefined;
5055

56+
const remarkProcessor = unified().use(remarkParse); //.use(remarkGfm);
57+
5158
switch (getInputFormatForFileName(fsbase(fsPath), config, frontMatter)) {
5259
case "djot":
5360
polyglotDoc = {
@@ -63,12 +70,18 @@ export async function parseFile(
6370
polyglotDoc = { kind: "djot", value: fromPandoc(ast as any) };
6471
break;
6572
case "commonmark":
66-
const file = unified().use(remarkParse).use(remarkGfm).parse(text);
67-
console.log(file);
68-
// polyglotDoc = { kind: "mdast", value: file };
73+
const file = remarkProcessor.parse(text);
74+
polyglotDoc = {
75+
kind: "mdast",
76+
value: file as PolyglotDoc_MDAST["value"],
77+
};
78+
console.log(yaml.dump(polyglotDoc.value));
6979
break;
7080
case "myst":
71-
polyglotDoc = { kind: "mdast", value: mystParse(text) };
81+
polyglotDoc = {
82+
kind: "mdast",
83+
value: mystParse(text) as PolyglotDoc_MDAST["value"],
84+
};
7285
// console.log(yaml.dump(polyglotDoc.value));
7386
break;
7487
}

src/plugins/autoTitlePlugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Heading } from "@djot/djot";
22
import { visit, EXIT } from "unist-util-visit";
3+
import unist from "unist";
34

45
import { applyFilter } from "../engine/djotFiltersPlus.js";
56
import { DjockeyDoc, DjockeyPlugin } from "../types.js";
@@ -8,6 +9,7 @@ import { LogCollector } from "../utils/logUtils.js";
89
import {
910
djotASTToMystAST_Inline,
1011
mystASTToDjotAST_Inline,
12+
Visitable,
1113
} from "../utils/astUtils.js";
1214
import { toString } from "mdast-util-to-string";
1315

@@ -38,7 +40,7 @@ export class AutoTitlePlugin implements DjockeyPlugin {
3840
}));
3941
break;
4042
case "mdast":
41-
visit(doc.docs.content.value, "heading", (node) => {
43+
visit(doc.docs.content.value as Visitable, "heading", (node) => {
4244
doc.title = toString(node);
4345
doc.titleASTDjot = mystASTToDjotAST_Inline(node);
4446
doc.titleASTMyst = node;

src/renderers/htmlRenderer.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import {
2828
} from "../utils/pathUtils.js";
2929
import { LogCollector } from "../utils/logUtils.js";
3030
import { mystToHtml } from "myst-to-html";
31+
import { unified } from "unified";
32+
import remarkRehype from "remark-rehype";
33+
import rehypeFormat from "rehype-format";
34+
import rehypeStringify from "rehype-stringify";
3135

3236
export class HTMLRenderer implements DjockeyRenderer {
3337
identifier: DjockeyOutputFormat = "html";
@@ -180,7 +184,14 @@ export class HTMLRenderer implements DjockeyRenderer {
180184
renderedDocs[k] = postprocessedHTML;
181185
break;
182186
case "mdast":
183-
renderedDocs[k] = mystToHtml(doc.docs[k].value);
187+
const tree = structuredClone(doc.docs[k].value);
188+
const processor = await unified()
189+
.use(remarkRehype)
190+
.use(rehypeFormat)
191+
.use(rehypeStringify);
192+
const tree2 = await processor.run(tree as any);
193+
const result = processor.stringify(tree2);
194+
renderedDocs[k] = result as string;
184195
break;
185196
}
186197
}

src/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { AstNode, Doc, Inline } from "@djot/djot";
22
import { Environment } from "nunjucks";
3+
import { visit } from "unist-util-visit";
4+
import { Parent, PhrasingContent, Root } from "mdast";
5+
36
import { LogCollector } from "./utils/logUtils.js";
4-
import { mystParse } from "myst-parser";
5-
import { PhrasingContent } from "mdast";
67

78
export interface LinkMappingConfig {
89
path: string;
@@ -65,11 +66,10 @@ export interface DjockeyConfigResolved extends DjockeyConfig {
6566
link_mappings: LinkMappingConfig[];
6667
}
6768

68-
export type MystDoc = ReturnType<typeof mystParse>;
6969
export type PolyglotDoc_Djot = { kind: "djot"; value: Doc };
7070
export type PolyglotDoc_MDAST = {
7171
kind: "mdast";
72-
value: MystDoc;
72+
value: Parameters<typeof visit>[0] & Parent;
7373
};
7474

7575
export type PolyglotDoc = PolyglotDoc_Djot | PolyglotDoc_MDAST;

src/utils/astUtils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Block, Heading, Inline } from "@djot/djot";
2-
import { Parent, PhrasingContent } from "mdast";
2+
import { PhrasingContent } from "mdast";
33
import unist from "unist";
44
import { visit } from "unist-util-visit";
55
import { toString } from "mdast-util-to-string";
@@ -8,6 +8,8 @@ import { applyFilter } from "../engine/djotFiltersPlus.js";
88
import { DjockeyDoc, PolyglotDoc, PolyglotDoc_MDAST } from "../types.js";
99
import { djotASTToText } from "./djotUtils.js";
1010

11+
export type Visitable = Parameters<typeof visit>[0];
12+
1113
export function getDoesDocHaveContent(doc: PolyglotDoc): boolean {
1214
switch (doc.kind) {
1315
case "djot":

0 commit comments

Comments
 (0)