Skip to content

Commit

Permalink
Sort of figure out what’s up with remark
Browse files Browse the repository at this point in the history
  • Loading branch information
irskep committed Sep 11, 2024
1 parent 2bf1caa commit 75b97cf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docs/src/foundations/markup_languages/commonmark.common.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
order: -2
---
# Using CommonMark

{#normal-italic-bold}
normal _italic_ **bold**
25 changes: 19 additions & 6 deletions src/input/parseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { basename } from "path";
import { fromPandoc, parse } from "@djot/djot";
import { mystParse } from "myst-parser";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import { unified } from "unified";
import yaml from "js-yaml";

import { DjockeyConfig, DjockeyDoc, PolyglotDoc } from "../types.js";
import {
DjockeyConfig,
DjockeyDoc,
PolyglotDoc,
PolyglotDoc_MDAST,
} from "../types.js";
import { getPandocAST } from "../pandoc.js";
import { getInputFormatForFileName } from "./fileExtensions.js";
import { LogCollector } from "../utils/logUtils.js";
import { fsbase, fsext, fsname, fssplit, refjoin } from "../utils/pathUtils.js";
import { Root } from "mdast";

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

let polyglotDoc: PolyglotDoc | undefined;

const remarkProcessor = unified().use(remarkParse); //.use(remarkGfm);

switch (getInputFormatForFileName(fsbase(fsPath), config, frontMatter)) {
case "djot":
polyglotDoc = {
Expand All @@ -63,12 +70,18 @@ export async function parseFile(
polyglotDoc = { kind: "djot", value: fromPandoc(ast as any) };
break;
case "commonmark":
const file = unified().use(remarkParse).use(remarkGfm).parse(text);
console.log(file);
// polyglotDoc = { kind: "mdast", value: file };
const file = remarkProcessor.parse(text);
polyglotDoc = {
kind: "mdast",
value: file as PolyglotDoc_MDAST["value"],
};
console.log(yaml.dump(polyglotDoc.value));
break;
case "myst":
polyglotDoc = { kind: "mdast", value: mystParse(text) };
polyglotDoc = {
kind: "mdast",
value: mystParse(text) as PolyglotDoc_MDAST["value"],
};
// console.log(yaml.dump(polyglotDoc.value));
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/autoTitlePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Heading } from "@djot/djot";
import { visit, EXIT } from "unist-util-visit";
import unist from "unist";

import { applyFilter } from "../engine/djotFiltersPlus.js";
import { DjockeyDoc, DjockeyPlugin } from "../types.js";
Expand All @@ -8,6 +9,7 @@ import { LogCollector } from "../utils/logUtils.js";
import {
djotASTToMystAST_Inline,
mystASTToDjotAST_Inline,
Visitable,
} from "../utils/astUtils.js";
import { toString } from "mdast-util-to-string";

Expand Down Expand Up @@ -38,7 +40,7 @@ export class AutoTitlePlugin implements DjockeyPlugin {
}));
break;
case "mdast":
visit(doc.docs.content.value, "heading", (node) => {
visit(doc.docs.content.value as Visitable, "heading", (node) => {
doc.title = toString(node);
doc.titleASTDjot = mystASTToDjotAST_Inline(node);
doc.titleASTMyst = node;
Expand Down
13 changes: 12 additions & 1 deletion src/renderers/htmlRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import {
} from "../utils/pathUtils.js";
import { LogCollector } from "../utils/logUtils.js";
import { mystToHtml } from "myst-to-html";
import { unified } from "unified";
import remarkRehype from "remark-rehype";
import rehypeFormat from "rehype-format";
import rehypeStringify from "rehype-stringify";

export class HTMLRenderer implements DjockeyRenderer {
identifier: DjockeyOutputFormat = "html";
Expand Down Expand Up @@ -180,7 +184,14 @@ export class HTMLRenderer implements DjockeyRenderer {
renderedDocs[k] = postprocessedHTML;
break;
case "mdast":
renderedDocs[k] = mystToHtml(doc.docs[k].value);
const tree = structuredClone(doc.docs[k].value);
const processor = await unified()
.use(remarkRehype)
.use(rehypeFormat)
.use(rehypeStringify);
const tree2 = await processor.run(tree as any);
const result = processor.stringify(tree2);
renderedDocs[k] = result as string;
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AstNode, Doc, Inline } from "@djot/djot";
import { Environment } from "nunjucks";
import { visit } from "unist-util-visit";
import { Parent, PhrasingContent, Root } from "mdast";

import { LogCollector } from "./utils/logUtils.js";
import { mystParse } from "myst-parser";
import { PhrasingContent } from "mdast";

export interface LinkMappingConfig {
path: string;
Expand Down Expand Up @@ -65,11 +66,10 @@ export interface DjockeyConfigResolved extends DjockeyConfig {
link_mappings: LinkMappingConfig[];
}

export type MystDoc = ReturnType<typeof mystParse>;
export type PolyglotDoc_Djot = { kind: "djot"; value: Doc };
export type PolyglotDoc_MDAST = {
kind: "mdast";
value: MystDoc;
value: Parameters<typeof visit>[0] & Parent;
};

export type PolyglotDoc = PolyglotDoc_Djot | PolyglotDoc_MDAST;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/astUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Block, Heading, Inline } from "@djot/djot";
import { Parent, PhrasingContent } from "mdast";
import { PhrasingContent } from "mdast";
import unist from "unist";
import { visit } from "unist-util-visit";
import { toString } from "mdast-util-to-string";
Expand All @@ -8,6 +8,8 @@ import { applyFilter } from "../engine/djotFiltersPlus.js";
import { DjockeyDoc, PolyglotDoc, PolyglotDoc_MDAST } from "../types.js";
import { djotASTToText } from "./djotUtils.js";

export type Visitable = Parameters<typeof visit>[0];

export function getDoesDocHaveContent(doc: PolyglotDoc): boolean {
switch (doc.kind) {
case "djot":
Expand Down

0 comments on commit 75b97cf

Please sign in to comment.