Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor noteworthy into subpackages #59

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions noteworthy-electron/electron-builder.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
appId: com.electron.app
productName: noteworthy-vite
productName: noteworthy
directories:
buildResources: build
files:
Expand All @@ -13,9 +13,9 @@ asarUnpack:
- resources/**
afterSign: build/notarize.js
win:
executableName: noteworthy-vite
executableName: noteworthy
nsis:
artifactName: ${name}-${version}-setup.${ext}
artifactName: ${produceName}-${version}-setup.${ext}
shortcutName: ${productName}
uninstallDisplayName: ${productName}
createDesktopShortcut: always
Expand All @@ -27,16 +27,20 @@ mac:
- NSDocumentsFolderUsageDescription: Application requests access to the user's Documents folder.
- NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder.
dmg:
artifactName: ${name}-${version}.${ext}
artifactName: ${productName}-${version}.${ext}
linux:
target:
- AppImage
- snap
- deb
maintainer: electronjs.org
category: Utility
snap:
artifactName: ${productName}-${version}.${ext}
deb:
artifactName: ${productName}-${version}.${ext}
appImage:
artifactName: ${name}-${version}.${ext}
artifactName: ${productName}-${version}.${ext}
npmRebuild: false
publish:
provider: generic
Expand Down
3 changes: 2 additions & 1 deletion noteworthy-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@noteworthy/electron",
"name": "noteworthy-electron",
"version": "0.0.1",
"description": "Markdown editor with excellent math support!",
"repository": {
Expand Down Expand Up @@ -55,6 +55,7 @@
"@electron-toolkit/preload": "^1.0.3",
"@electron-toolkit/utils": "^1.0.2",
"@noteworthy/editor": "workspace:*",
"@noteworthy/markdown": "workspace:*",
"buffer": "^6.0.3",
"chokidar": "^3.5.3",
"citation-js": "^0.5.6",
Expand Down
12 changes: 5 additions & 7 deletions noteworthy-electron/src/common/doctypes/markdown-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { IMetadataProvider, IMetadata } from "@main/plugins/metadata-plugin
import type { Citation, ICitationProvider } from "@main/plugins/citation-plugin";

// markdown / mdast
import * as Md from "@common/markdown/markdown-ast";
import { Md, mdastTextContent, UnistUtils } from "@noteworthy/markdown";

// node extensions
import {
Expand All @@ -34,9 +34,7 @@ import {
//TagExtension
} from "@common/extensions/mark-extensions";

import { unistPredicate, visit, visitNodeType } from "@common/markdown/unist-utils";
import { SyntaxExtension } from "@common/extensions/extension";
import { mdastTextContent } from "@common/markdown/mdast-to-string";
import { pick } from "@common/util/pick";
import { parseDate, formatDate } from "@common/util/date";

Expand Down Expand Up @@ -154,7 +152,7 @@ export class MarkdownAst implements IDoc, ICrossRefProvider, IOutlineProvider, I
let entries:IOutlineEntry[] = [];

// find all headings
visitNodeType<Md.Heading>(this._root, "heading", node => {
UnistUtils.visitNodeType<Md.Heading>(this._root, "heading", node => {
// heading text content
let headingText: string = mdastTextContent(node);

Expand Down Expand Up @@ -270,10 +268,10 @@ export class MarkdownAst implements IDoc, ICrossRefProvider, IOutlineProvider, I

// find all wikilinks and citations
// TODO: (2021-05-30) restore #tag syntax?
visit(this._root, node => {
if(unistPredicate<Md.Wikilink>(node, "wikiLink")) {
UnistUtils.visit(this._root, node => {
if(UnistUtils.unistPredicate<Md.Wikilink>(node, "wikiLink")) {
tags.push(node.value);
} else if(unistPredicate<Md.Cite>(node, "cite")) {
} else if(UnistUtils.unistPredicate<Md.Cite>(node, "cite")) {
node.data.citeItems.forEach(item => {
tags.push(item.key);
});
Expand Down
19 changes: 11 additions & 8 deletions noteworthy-electron/src/common/extensions/editor-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ import { markMapBasic, markMapStringLiteral, MdParser, nodeMapBasic, nodeMapLeaf

// unist / unified
import * as Uni from "unist";
import * as Md from "@common/markdown/markdown-ast";
import unified, { Processor } from "unified";
import { MdSerializer } from "@common/markdown/prose2mdast";
import { MdError } from "@common/markdown/remark-plugins/error/remark-error";
import { unistIsStringLiteral } from "@common/markdown/unist-utils";

// remark and remark plugins
import remark from "remark-parse";
Expand All @@ -30,10 +27,16 @@ import remarkDirective from "remark-directive"; // commonmark directives

// custom remark plugins
import { citePlugin as remarkCitePlugin } from "@benrbray/remark-cite";
import { wikiLinkPlugin as remarkWikilinkPlugin } from '@common/markdown/remark-plugins/wikilink/remark-wikilink';
import { remarkErrorPlugin } from "@common/markdown/remark-plugins/error/remark-error";
import { remarkConcretePlugin } from "@common/markdown/remark-plugins/concrete/remark-concrete";
import { remarkUnwrapImagePlugin } from "@common/markdown/remark-plugins/unwrap-image/remark-unwrap-image";

// noteworthy
import {
Md, type MdError,
wikiLinkPlugin as remarkWikilinkPlugin,
remarkErrorPlugin,
remarkConcretePlugin,
remarkUnwrapImagePlugin,
UnistUtils
} from "@noteworthy/markdown";

// project imports
import { DefaultMap } from "@common/util/DefaultMap";
Expand Down Expand Up @@ -448,7 +451,7 @@ export class EditorConfig<S extends ProseSchema = ProseSchema> {
result.marks.get("error_inline").push({
map: (mark: ProseMark, node: Uni.Node): MdError => {
// expect literal node
if(!unistIsStringLiteral(node)) { throw new Error("expected error_inline to be a text node"); }
if(!UnistUtils.unistIsStringLiteral(node)) { throw new Error("expected error_inline to be a text node"); }

return {
type: "error",
Expand Down
33 changes: 18 additions & 15 deletions noteworthy-electron/src/common/extensions/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { Schema as ProseSchema, Node as ProseNode, NodeSpec, MarkSpec } from "pr
import { InputRule } from "prosemirror-inputrules"
import { NodeViewConstructor } from "prosemirror-view";

// project imports
import { ProseNodeType, ProseMarkType, ProseKeymap } from "@common/types";

// unist
import * as Uni from "unist";
import * as Md from "@common/markdown/markdown-ast";

// noteworthy
import { Md } from "@noteworthy/markdown";

// project imports
import { ProseNodeType, ProseMarkType, ProseKeymap } from "@common/types";
import { ProseMarkMap, ProseNodeMap } from "./editor-config";


////////////////////////////////////////////////////////////

// Extension Types inspired by ReMirror
Expand All @@ -22,7 +25,7 @@ export interface ExtensionStore<S extends ProseSchema> {

export abstract class SyntaxExtension<S extends ProseSchema = ProseSchema, N extends string = string> {
abstract get name(): N;

/**
* Throw an error if the extension's `_store` field hasn't yet been populated.
* (useful as a type guard to avoid null-checking boilerplate in syntax extensions.)
Expand All @@ -33,37 +36,37 @@ export abstract class SyntaxExtension<S extends ProseSchema = ProseSchema, N ext
}
return true;
}

// properties that will be added later
_store:ExtensionStore<S>|null = null;
get store():ExtensionStore<S> {
if(!this.ensureInitialized()) { throw new Error("not iniialized"); }
return this._store;
}

createInputRules(): InputRule[] { return []; }
createKeymap(): ProseKeymap { return {}; }
}

export abstract class NodeExtension<N extends string = string> extends SyntaxExtension<ProseSchema<N,string>, N> {
/**
/**
* Returns the ProseMirror NodeType defined by this extension.
* (not available until the extension has been used to create a schema)
* (not available until the extension has been used to create a schema)
*/
get nodeType(): ProseNodeType {
let type = this.store.schema.nodes[this.name];
if(type === undefined) { throw new Error(`error retrieving node type for extension ${this.name}`); }
return type;
}

abstract createNodeSpec(): NodeSpec;
createNodeView(): NodeViewConstructor|null { return null; }
}

export abstract class MarkExtension<M extends string = string> extends SyntaxExtension<ProseSchema<string,M>, M> {
/**
/**
* Returns the ProseMirror MarkType defined by this extension.
* (not available until the extension has been used to create a schema)
* (not available until the extension has been used to create a schema)
*/
get markType(): ProseMarkType {
let type = this.store.schema.marks[this.name];
Expand All @@ -81,7 +84,7 @@ export abstract class NodeSyntaxExtension<T extends Md.Node, N extends string =
* Mapping from Mdast node node to ProseMirror node.
*/
abstract createMdastMap() : MdastNodeMap<T>;

/**
* Extensions provide this method to impose additional
* conditions on the incoming Mdast node type, beyond
Expand All @@ -97,7 +100,7 @@ export abstract class NodeSyntaxExtension<T extends Md.Node, N extends string =

export abstract class MarkSyntaxExtension<T extends Md.Node, M extends string = string> extends MarkExtension<M> {
abstract get mdastNodeType(): T["type"];

/**
* Mapping from Mdast node node to ProseMirror node.
*/
Expand Down Expand Up @@ -181,4 +184,4 @@ export enum Prose2Mdast_MarkMap_Presets {

export type Prose2Mdast_MarkMap
= Prose2Mdast_MarkMap_Presets
| { create : ProseMarkMap };
| { create : ProseMarkMap };
7 changes: 4 additions & 3 deletions noteworthy-electron/src/common/extensions/mark-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { InputRule } from "prosemirror-inputrules"
import { toggleMark } from "prosemirror-commands"

// project imports
import * as Md from "@common/markdown/markdown-ast";
import { markActive, markInputRule } from "@common/prosemirror/util/mark-utils";
import { openPrompt, TextField } from "@common/prompt/prompt";
import { MarkSyntaxExtension, MdastMarkMap, MdastMarkMapType, Prose2Mdast_MarkMap_Presets } from "@common/extensions/extension";

// mdast
import * as Uni from "unist";
import { AnyChildren, markMapBasic } from "@common/markdown/mdast2prose";
import { unistIsStringLiteral } from "@common/markdown/unist-utils";
import { ProseKeymap } from "@common/types";

// noteworthy
import { Md, UnistUtils } from "@noteworthy/markdown";

//// MARK EXTENSIONS ///////////////////////////////////////

/* -- Bold ---------------------------------------------- */
Expand Down Expand Up @@ -260,7 +261,7 @@ export class WikilinkExtension extends MarkSyntaxExtension<Md.Wikilink> {
prose2mdast() { return {
create: <N extends Uni.Node>(mark: ProseMark, node: N): Md.Wikilink|N => {
// expect string literal node
if(!unistIsStringLiteral(node)) {
if(!UnistUtils.unistIsStringLiteral(node)) {
console.error(`mark type ${this.name} can only wrap Literal node ; skipping`);
return node;
}
Expand Down
4 changes: 3 additions & 1 deletion noteworthy-electron/src/common/extensions/node-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
// unist imports
import * as Uni from "unist";

// noteworthy
import { Md } from "@noteworthy/markdown";

// project imports
import * as Md from "@common/markdown/markdown-ast";
import { incrHeadingLevelCmd } from "@common/prosemirror/commands/demoteHeadingCmd";
import { ExtensionNodeAttrs, MdastNodeMap, MdastNodeMapType, NodeSyntaxExtension, Prose2Mdast_NodeMap, Prose2Mdast_NodeMap_Presets } from "@common/extensions/extension";
import {
Expand Down
9 changes: 5 additions & 4 deletions noteworthy-electron/src/common/markdown/mdast2prose.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// unist / mdast / remark
import { Processor } from "unified";
import * as Uni from "unist";
import * as Md from "@common/markdown/markdown-ast";
import { unistIsParent, unistSource } from "./unist-utils";

// prosemirror imports
import { Schema as ProseSchema, Node as ProseNode } from "prosemirror-model";
Expand All @@ -14,6 +12,9 @@ import { UnistMapper } from "@common/extensions/editor-config";
// yaml / toml
import YAML from "yaml";

// noteworthy
import { Md, UnistUtils } from "@noteworthy/markdown";

////////////////////////////////////////////////////////////////////////////////

/**
Expand Down Expand Up @@ -207,7 +208,7 @@ export function treeMap<S extends ProseSchema, Ctx, St>(
let nodeContents: ProseNode[] = [];
let newParseState = parseState;

if(unistIsParent(node)) {
if(UnistUtils.unistIsParent(node)) {
// flatmap the results of traversing this node's children
for(let idx = 0; idx < node.children.length; idx++) {
let [child, state] = treeMap(
Expand Down Expand Up @@ -377,7 +378,7 @@ export const makeParser = <S extends ProseSchema<"error_block","error_inline">>(
let errorMap = makeNodeErrorHandler<S>(
proseSchema.marks.error_inline,
proseSchema.nodes.error_block,
node => unistSource(node, markdown)
node => UnistUtils.unistSource(node, markdown)
);

// map over root's children
Expand Down
15 changes: 8 additions & 7 deletions noteworthy-electron/src/common/markdown/prose2mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Node as ProseNode, Mark as ProseMark } from "prosemirror-model";

// unist
import * as Uni from "unist";
import * as Md from "@common/markdown/markdown-ast";
import { Processor } from "unified";

// noteworthy
import { Md, UnistUtils } from "@noteworthy/markdown";

// project imports
import { ProseMapper, ProseMarkMap, ProseMarkTest, ProseNodeMap } from "@common/extensions/editor-config";
import { StringLiteral, unistIsParent, unistIsStringLiteral } from "./unist-utils";

////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -50,13 +51,13 @@ export const nodeMapLiftLiteral: (type:string) => ProseNodeMap
= (type: string) => (
node: ProseNode,
children: Uni.Node[]
): StringLiteral[] => {
): UnistUtils.StringLiteral[] => {
// expect unique child literal
if(children.length !== 1) { throw new Error(`[prose2mdast] expected exactly one child, found ${children.length}`); }

const child = children[0];
if(unistIsParent(child)) { throw new Error("[prose2mdast] expected leaf literal node"); }
if(!unistIsStringLiteral(child)) { throw new Error("[prose2mdast] expected leaf literal node") }
if(UnistUtils.unistIsParent(child)) { throw new Error("[prose2mdast] expected leaf literal node"); }
if(!UnistUtils.unistIsStringLiteral(child)) { throw new Error("[prose2mdast] expected leaf literal node") }
if(!child.value) { throw new Error("[prose2mdast] expected non-empty literal node"); }

// create literal node
Expand Down Expand Up @@ -91,10 +92,10 @@ export const markMapLiteral: <V=unknown>(type:string) => ProseMarkMap
= <V=unknown>(type: string) => (
mark: ProseMark,
node: Uni.Node
): StringLiteral => {
): UnistUtils.StringLiteral => {
// expect node to be a text node
// TODO (2021-05-19) what if value is not a string?
if(!unistIsStringLiteral(node)) { throw new Error(`[prose2mdast] expected Unist.Literal node when wrapping with mark '${mark.type.name}'`); }
if(!UnistUtils.unistIsStringLiteral(node)) { throw new Error(`[prose2mdast] expected Unist.Literal node when wrapping with mark '${mark.type.name}'`); }

// create literal node
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// prosemirror imports
import { EditorView as ProseEditorView, EditorView } from "prosemirror-view";
import { Mark, Slice, Node as ProseNode } from "prosemirror-model";
import { Mark, Node as ProseNode } from "prosemirror-model";
import { chainCommands, joinUp, joinDown, lift, selectParentNode } from "prosemirror-commands";
import { EditorState as ProseEditorState, Transaction, Plugin as ProsePlugin } from "prosemirror-state";
import { history, undo, redo } from "prosemirror-history";
Expand All @@ -11,9 +11,8 @@ import { undoInputRule } from "prosemirror-inputrules";
import { IPossiblyUntitledFile } from "@common/files";
import { Editor } from "./editor";

// markdown
import * as Uni from "unist";
import * as Md from "@common/markdown/markdown-ast";
// noteworthy
import { Md } from "@noteworthy/markdown";

// solidjs
import { render } from "solid-js/web";
Expand Down
Loading