Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dancormier committed Jan 17, 2025
1 parent 9ba6f9a commit 8c7a3e0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
8 changes: 3 additions & 5 deletions src/rich-text/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,11 @@ const marks: {
sub: genHtmlInlineMarkSpec({}, "sub"),

escape: {
parseDOM: [
{ style: "span.escaped" }
],
parseDOM: [{ style: "span.escaped" }],
toDOM() {
return ["span", { class: "escaped" }];
}
}
},
},
};

// for *every* mark, add in support for the `markup` attribute
Expand Down
24 changes: 15 additions & 9 deletions src/shared/markdown-it/preserve-escape.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import MarkdownIt, {StateInline} from "markdown-it";
import MarkdownIt, { StateInline } from "markdown-it";

function buildPreserveEscapeFn(md: MarkdownIt): MarkdownIt.ParserInline.RuleInline {
const [escapeFn] = md.inline.ruler.getRules('')
.filter(r => r.name === "escape");
function buildPreserveEscapeFn(
md: MarkdownIt
): MarkdownIt.ParserInline.RuleInline {
const [escapeFn] = md.inline.ruler
.getRules("")
.filter((r) => r.name === "escape");

const noop = (): boolean => false;
//The "escape" rule has been disabled or otherwise removed; so there's nothing to replace here.
if(escapeFn.length === 0){
if (escapeFn.length === 0) {
return noop;
}
return function preserveEscapeFn(state: StateInline, silent: boolean): boolean {
return function preserveEscapeFn(
state: StateInline,
silent: boolean
): boolean {
const escRet = escapeFn(state, silent);

//If the rule did nothing (returned false or is running in silent mode) there's nothing to fix
if(silent || escRet === false) return escRet;
if (silent || escRet === false) return escRet;

//The escape rule, if executed, always adds a 'text_special' node to the end, and we're going to work on that.
const [escapeToken] = state.tokens.slice(-1);

//Now we want to retag the type so that
// - the escape token is ignored by the text_merge
// - We can enact custom rendering later
escapeToken.type = 'escape'
escapeToken.type = "escape";

return escRet;
}
};
}

export function preserve_escape(md: MarkdownIt): void {
Expand Down
8 changes: 4 additions & 4 deletions src/shared/markdown-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { stackLanguageComments } from "./markdown-it/stack-language-comments";
import { tagLinks } from "./markdown-it/tag-link";
import { tight_list } from "./markdown-it/tight-list";
import type { CommonmarkParserFeatures } from "./view";
import {preserve_escape} from "./markdown-it/preserve-escape";
import { preserve_escape } from "./markdown-it/preserve-escape";

// extend the default markdown parser's tokens and add our own
const customMarkdownParserTokens: MarkdownParser["tokens"] = {
Expand Down Expand Up @@ -152,10 +152,10 @@ const customMarkdownParserTokens: MarkdownParser["tokens"] = {
mark: "code",
},

escape : {
escape: {
mark: "escape",
noCloseToken: true
}
noCloseToken: true,
},
};

// add tag attribute support to all the marks like we did in schema
Expand Down
19 changes: 11 additions & 8 deletions src/shared/markdown-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,13 @@ const defaultMarkdownSerializerNodes: MarkdownSerializerNodes = {
) {
text = linkMark.attrs.href as string;
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const startOfLine: boolean = state.atBlank() || state.atBlockStart || state.closed;
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unsafe-assignment */
const startOfLine: boolean =
// @ts-expect-error
// eslint-disable-next-line
state.atBlank() || state.atBlockStart || state.closed;
/* eslint-enable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unsafe-assignment */

// escape the text using the built in escape code
let escapedText = state.esc(node.text, startOfLine);

Expand Down Expand Up @@ -671,11 +674,11 @@ const customMarkdownSerializerMarks: MarkdownSerializerMarks = {
expelEnclosingWhitespace: true,
}),
escape: {
open: '\\',
close: '',
open: "\\",
close: "",
mixable: false,
escape: false,// That's what we're doing!
}
escape: false, // That's what we're doing!
},
};

// export our custom serializer using the extended nodes/marks taken from the default schema
Expand Down

0 comments on commit 8c7a3e0

Please sign in to comment.