Skip to content

Commit

Permalink
[jscoq] Add markdown preview to document, connect to button.
Browse files Browse the repository at this point in the history
We add a rich markdown preview for the document.
  • Loading branch information
ejgallego committed Jul 30, 2023
1 parent ba72613 commit 862692d
Show file tree
Hide file tree
Showing 9 changed files with 2,053 additions and 27 deletions.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -179,7 +179,8 @@ distclean: clean
dist: dist-npm dist-tarball

BUILDOBJ = ${addprefix $(BUILDDIR)/./, \
jscoq.js coq-pkgs frontend backend dist examples docs}
jscoq.js coq-pkgs frontend backend dist examples docs \
node_modules/ocaml-wasm node_modules/@ocaml-wasm }
DISTOBJ = README.md index.html package.json package-lock.json $(BUILDOBJ)
DISTDIR = _build/dist

Expand Down Expand Up @@ -234,6 +235,9 @@ dist-npm-wacoq:
# is absolutely bothersome. I could not conjure a more sustainable way to emit
# two separate NPM packages from the same source tree, though.

# I think in our case makes sense to have a single package, that
# includes both workers build, and is selectable, as for editors.

########################################################################
# Externals
########################################################################
Expand Down
2 changes: 1 addition & 1 deletion backend/wasm/wacoq_worker.ts
Expand Up @@ -9,7 +9,7 @@ function postMessage(msg) {

async function main() {
/** @note when serving from source tree, the `node_modules` ref works by chance */
var icoq = new IcoqPod('../backend/wasm', '../../../node_modules');
var icoq = new IcoqPod('../backend/wasm', '../node_modules');

postMessage(['Starting']);
icoq.on('message', postMessage);
Expand Down
5 changes: 4 additions & 1 deletion esbuild.mjs
Expand Up @@ -65,7 +65,10 @@ var frontend = esbuild
format: "esm",
loader: {
'.png': 'binary',
'.svg': 'dataurl'
'.svg': 'dataurl',
'.ttf': 'dataurl',
'.woff': 'dataurl',
'.woff2': 'dataurl',
},
metafile: enableMeta,
outdir: "dist/frontend",
Expand Down
44 changes: 44 additions & 0 deletions frontend/classic/js/coq-editor-mdview.tsx
@@ -0,0 +1,44 @@
import { ICoqEditor, ICoqEditorConstructor } from "./coq-editor";
import React from 'react';
import MarkdownPreview from '@uiw/react-markdown-preview';
import { Diagnostic } from "../../../backend";
import { Root, createRoot } from 'react-dom/client';
import { CoqDocument } from "./coq-manager";
import rehypeKatex from 'rehype-katex';
import remarkMath from 'remark-math';
import 'katex/dist/katex.css';

export class CoqMdViewEditor implements ICoqEditor {
root : Root;

constructor(doc : CoqDocument, options) {
this.root = createRoot(doc.container);
this.root.render(<MarkdownPreview source={doc.area.value}
remarkPlugins={[remarkMath]}
rehypePlugins={[rehypeKatex]} />);
}
destroy(): void {
this.root.unmount();
}
getValue(): string {
return "";
}
getCursorOffset(): number {
return 0;
}
clearDiagnostics(): void {

}
markDiagnostic(diag: Diagnostic): void {

}
configure(opts: any): void {

}
openFile(file: File): void {

}
focus(): void {

}
}
11 changes: 6 additions & 5 deletions frontend/classic/js/coq-manager.ts
Expand Up @@ -31,6 +31,7 @@ import { CoqIdentifier } from '../../../backend/coq-identifier';
// Addons
// import { CoqContextualInfo } from './contextual-info.js';
import { CompanyCoq } from './addon/company-coq.js';
import { CoqMdViewEditor } from './coq-editor-mdview';

/**
* Coq Document Manager, client-side.
Expand All @@ -44,7 +45,7 @@ import { CompanyCoq } from './addon/company-coq.js';
export interface ManagerOptions {
backend: backend,
content_type: 'plain' | 'markdown',
frontend: 'cm5' | 'cm6' | 'pm',
frontend: 'cm5' | 'cm6' | 'pm' | 'mdview',
prelaunch: boolean,
prelude: boolean,
debug: boolean,
Expand Down Expand Up @@ -158,7 +159,7 @@ class ManagerEditor {
connect() {

// Setup the Coq editor.
const eIdx = { 'pm': CoqProseMirror, 'cm6': CoqCodeMirror6, 'cm5': CoqCodeMirror5 };
const eIdx = { 'pm': CoqProseMirror, 'cm6': CoqCodeMirror6, 'cm5': CoqCodeMirror5, 'mdview': CoqMdViewEditor };
const CoqEditor : ICoqEditorConstructor = eIdx[this.options.frontend];

if (!CoqEditor)
Expand Down Expand Up @@ -207,7 +208,7 @@ export class CoqManager {
content_type: 'markdown',
prelaunch: false,
prelude: true,
debug: true,
debug: false,
show: true,
replace: false,
wrapper_id: 'ide-wrapper',
Expand Down Expand Up @@ -557,7 +558,7 @@ export class CoqManager {
let init_opts : CoqInitOptions = {
implicit_libs: this.options.implicit_libs,
coq_options: this._parseOptions(this.options.coq || {}),
debug: true,
debug: this.options.debug,
lib_path: this.getLoadPath(),
lib_init: this.options.prelude ? [PKG_ALIASES.prelude] : []
};
Expand Down Expand Up @@ -791,7 +792,7 @@ export class CoqManager {

case 'editor':
this.editor.disconnect();
this.editor.options.frontend = (this.editor.options.frontend === 'cm5') ? 'cm6' : 'cm5';
this.editor.options.frontend = (this.editor.options.frontend === 'cm5') ? 'mdview' : 'cm5';
this.editor.connect();
break;
}
Expand Down
16 changes: 12 additions & 4 deletions index.html
Expand Up @@ -68,6 +68,14 @@ <h3>Welcome to the <span class="jscoq-name">jsCoq</span> Interactive Online Syst
- Coq will recognize the code snippets as Coq
- You will be able to save the document and link to other documents soon

We can also add some random math:

$$
\sum_i a_i * b_i
$$

I *love* random math $x \cdot y \approx y \cdot x$.

```coq
From Coq Require Import List.
Import ListNotations.
Expand Down Expand Up @@ -113,12 +121,12 @@ <h3>Welcome to the <span class="jscoq-name">jsCoq</span> Interactive Online Syst

var jscoq_ids = ['coqCode'];
var jscoq_opts = {
backend: sp.get('backend'),
frontend: sp.get('frontend'),
content_type: sp.get('content_type'),
backend: sp.get('backend') || "wa",
frontend: sp.get('frontend') || "cm5",
content_type: sp.get('content_type') || "markdown",
implicit_libs: false,
focus: false,
debug: true,
debug: false,
editor: { mode: { 'company-coq': false } },
init_pkgs: ['init', 'coq-base', 'coq-collections', 'coq-arith'],
all_pkgs: ['coq', 'mathcomp']
Expand Down

0 comments on commit 862692d

Please sign in to comment.