mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 17:46:28 +08:00
ui: drop vditor
This commit is contained in:
16 files changed
+399
-153
No files matched your search
@@ -0,0 +1,235 @@
|
||||
// Process 
|
||||
// ^^^^^^^^ this size specification
|
||||
function parseNextNumber(str, pos, max) {
|
||||
const start = pos;
|
||||
const result = {
|
||||
ok: false,
|
||||
pos,
|
||||
value: '',
|
||||
};
|
||||
let code = str.charCodeAt(pos);
|
||||
while (pos < max && (code >= 0x30 /* 0 */ && code <= 0x39 /* 9 */) || code === 0x25 /* % */) {
|
||||
code = str.charCodeAt(++pos);
|
||||
}
|
||||
result.ok = true;
|
||||
result.pos = pos;
|
||||
result.value = str.slice(start, pos);
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseImageSize(str, pos, max) {
|
||||
const result = {
|
||||
ok: false,
|
||||
pos: 0,
|
||||
width: '',
|
||||
height: '',
|
||||
};
|
||||
if (pos >= max) { return result; }
|
||||
let code = str.charCodeAt(pos);
|
||||
if (code !== 0x3d /* = */) { return result; }
|
||||
pos++;
|
||||
code = str.charCodeAt(pos);
|
||||
if (code !== 0x78 /* x */ && (code < 0x30 || code > 0x39) /* [0-9] */) {
|
||||
return result;
|
||||
}
|
||||
// parse width
|
||||
const resultW = parseNextNumber(str, pos, max);
|
||||
pos = resultW.pos;
|
||||
// next charactor must be 'x'
|
||||
code = str.charCodeAt(pos);
|
||||
if (code !== 0x78 /* x */) { return result; }
|
||||
pos++;
|
||||
// parse height
|
||||
const resultH = parseNextNumber(str, pos, max);
|
||||
pos = resultH.pos;
|
||||
result.width = resultW.value;
|
||||
result.height = resultH.value;
|
||||
result.pos = pos;
|
||||
result.ok = true;
|
||||
return result;
|
||||
}
|
||||
|
||||
export default function plugin(md) {
|
||||
md.inline.ruler.before('emphasis', 'image', (state, silent) => {
|
||||
let attrs;
|
||||
let code;
|
||||
let label;
|
||||
let pos;
|
||||
let ref;
|
||||
let res;
|
||||
let title;
|
||||
let width = '';
|
||||
let height = '';
|
||||
let token;
|
||||
let tokens;
|
||||
let start;
|
||||
let href = '';
|
||||
const oldPos = state.pos;
|
||||
const max = state.posMax;
|
||||
|
||||
if (state.src.charCodeAt(state.pos) !== 0x21/* ! */) { return false; }
|
||||
if (state.src.charCodeAt(state.pos + 1) !== 0x5B/* [ */) { return false; }
|
||||
|
||||
const labelStart = state.pos + 2;
|
||||
const labelEnd = md.helpers.parseLinkLabel(state, state.pos + 1, false);
|
||||
|
||||
// parser failed to find ']', so it's not a valid link
|
||||
if (labelEnd < 0) { return false; }
|
||||
|
||||
pos = labelEnd + 1;
|
||||
if (pos < max && state.src.charCodeAt(pos) === 0x28/* ( */) {
|
||||
//
|
||||
// Inline link
|
||||
//
|
||||
|
||||
// [link]( <href> "title" )
|
||||
// ^^ skipping these spaces
|
||||
pos++;
|
||||
for (; pos < max; pos++) {
|
||||
code = state.src.charCodeAt(pos);
|
||||
if (code !== 0x20 && code !== 0x0A) { break; }
|
||||
}
|
||||
if (pos >= max) { return false; }
|
||||
|
||||
// [link]( <href> "title" )
|
||||
// ^^^^^^ parsing link destination
|
||||
start = pos;
|
||||
res = md.helpers.parseLinkDestination(state.src, pos, state.posMax);
|
||||
if (res.ok) {
|
||||
href = state.md.normalizeLink(res.str);
|
||||
if (state.md.validateLink(href)) {
|
||||
pos = res.pos;
|
||||
} else {
|
||||
href = '';
|
||||
}
|
||||
}
|
||||
|
||||
// [link]( <href> "title" )
|
||||
// ^^ skipping these spaces
|
||||
start = pos;
|
||||
for (; pos < max; pos++) {
|
||||
code = state.src.charCodeAt(pos);
|
||||
if (code !== 0x20 && code !== 0x0A) { break; }
|
||||
}
|
||||
|
||||
// [link]( <href> "title" )
|
||||
// ^^^^^^^ parsing link title
|
||||
res = md.helpers.parseLinkTitle(state.src, pos, state.posMax);
|
||||
if (pos < max && start !== pos && res.ok) {
|
||||
title = res.str;
|
||||
pos = res.pos;
|
||||
|
||||
// [link]( <href> "title" )
|
||||
// ^^ skipping these spaces
|
||||
for (; pos < max; pos++) {
|
||||
code = state.src.charCodeAt(pos);
|
||||
if (code !== 0x20 && code !== 0x0A) { break; }
|
||||
}
|
||||
} else {
|
||||
title = '';
|
||||
}
|
||||
|
||||
// [link]( <href> "title" =WxH )
|
||||
// ^^^^ parsing image size
|
||||
if (pos - 1 >= 0) {
|
||||
code = state.src.charCodeAt(pos - 1);
|
||||
|
||||
// there must be at least one white spaces
|
||||
// between previous field and the size
|
||||
if (code === 0x20) {
|
||||
res = parseImageSize(state.src, pos, state.posMax);
|
||||
if (res.ok) {
|
||||
width = res.width;
|
||||
height = res.height;
|
||||
pos = res.pos;
|
||||
|
||||
// [link]( <href> "title" =WxH )
|
||||
// ^^ skipping these spaces
|
||||
for (; pos < max; pos++) {
|
||||
code = state.src.charCodeAt(pos);
|
||||
if (code !== 0x20 && code !== 0x0A) { break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pos >= max || state.src.charCodeAt(pos) !== 0x29/* ) */) {
|
||||
state.pos = oldPos;
|
||||
return false;
|
||||
}
|
||||
pos++;
|
||||
} else {
|
||||
//
|
||||
// Link reference
|
||||
//
|
||||
if (typeof state.env.references === 'undefined') { return false; }
|
||||
|
||||
// [foo] [bar]
|
||||
// ^^ optional whitespace (can include newlines)
|
||||
for (; pos < max; pos++) {
|
||||
code = state.src.charCodeAt(pos);
|
||||
if (code !== 0x20 && code !== 0x0A) { break; }
|
||||
}
|
||||
|
||||
if (pos < max && state.src.charCodeAt(pos) === 0x5B/* [ */) {
|
||||
start = pos + 1;
|
||||
pos = md.helpers.parseLinkLabel(state, pos);
|
||||
if (pos >= 0) {
|
||||
label = state.src.slice(start, pos++);
|
||||
} else {
|
||||
pos = labelEnd + 1;
|
||||
}
|
||||
} else {
|
||||
pos = labelEnd + 1;
|
||||
}
|
||||
|
||||
// covers label === '' and label === undefined
|
||||
// (collapsed reference link and shortcut reference link respectively)
|
||||
label ||= state.src.slice(labelStart, labelEnd);
|
||||
|
||||
ref = state.env.references[md.utils.normalizeReference(label)];
|
||||
if (!ref) {
|
||||
state.pos = oldPos;
|
||||
return false;
|
||||
}
|
||||
href = ref.href;
|
||||
title = ref.title;
|
||||
}
|
||||
|
||||
//
|
||||
// We found the end of the link, and know for a fact it's a valid link;
|
||||
// so all that's left to do is to call tokenizer.
|
||||
//
|
||||
if (!silent) {
|
||||
state.pos = labelStart;
|
||||
state.posMax = labelEnd;
|
||||
|
||||
const newState = new state.md.inline.State(
|
||||
state.src.slice(labelStart, labelEnd),
|
||||
state.md,
|
||||
state.env,
|
||||
tokens = [],
|
||||
);
|
||||
newState.md.inline.tokenize(newState);
|
||||
token = state.push('image', 'img', 0);
|
||||
token.attrs = attrs = [['src', href],
|
||||
['alt', '']];
|
||||
token.children = tokens;
|
||||
if (title) {
|
||||
attrs.push(['title', title]);
|
||||
}
|
||||
|
||||
if (width !== '') {
|
||||
attrs.push(['width', width]);
|
||||
}
|
||||
|
||||
if (height !== '') {
|
||||
attrs.push(['height', height]);
|
||||
}
|
||||
}
|
||||
|
||||
state.pos = pos;
|
||||
state.posMax = max;
|
||||
return true;
|
||||
});
|
||||
};
|
||||
+6
-5
@@ -1,7 +1,8 @@
|
||||
// https://github.com/waylonflinn/markdown-it-katex/blob/master/index.js
|
||||
const katex = require('katex');
|
||||
import { Logger } from 'hydrooj';
|
||||
import katex from 'katex';
|
||||
|
||||
const logger = new global.Hydro.Logger('katex');
|
||||
const logger = new Logger('katex');
|
||||
|
||||
function isValidDelim(state, pos) {
|
||||
const max = state.posMax;
|
||||
@@ -109,8 +110,8 @@ function block(state, start, end, silent) {
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = function plugin(md) {
|
||||
const options = { throwOnError: false, strict: 'ignore' };
|
||||
export default function plugin(md) {
|
||||
const options = { throwOnError: false, strict: 'ignore', displayMode: false };
|
||||
const render = (latex: string, displayMode = false) => {
|
||||
options.displayMode = displayMode;
|
||||
try {
|
||||
@@ -136,4 +137,4 @@ module.exports = function plugin(md) {
|
||||
});
|
||||
md.renderer.rules.math_inline = inlineRenderer;
|
||||
md.renderer.rules.math_block = blockRenderer;
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
/* eslint-disable max-len */
|
||||
/* eslint-disable prefer-destructuring */
|
||||
|
||||
import { randomUUID } from 'crypto';
|
||||
import MarkdownIt from 'markdown-it';
|
||||
import type MarkdownIt from 'markdown-it';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
const allowFullScreen = ' webkitallowfullscreen mozallowfullscreen allowfullscreen';
|
||||
|
||||
@@ -110,7 +110,7 @@ export function Media(md: MarkdownIt) {
|
||||
if (service === 'pdf') {
|
||||
if (src.startsWith('file://') || src.startsWith('./')) src += src.includes('?') ? '&noDisposition=1' : '?noDisposition=1';
|
||||
return `\
|
||||
<object classid="clsid:${randomUUID().toUpperCase()}">
|
||||
<object classid="clsid:${uuid().toUpperCase()}">
|
||||
<param name="SRC" value="${src}" >
|
||||
<embed width="100%" style="min-height: 100vh;border: none;" fullscreen="yes" src="${src}">
|
||||
<noembed></noembed>
|
||||
|
||||
@@ -23,7 +23,7 @@ const MergeCells = require('markdown-it-merge-cells');
|
||||
// @[youtube](https://youtube.com/watch?v=xxx)
|
||||
// @[pdf](https://foo.com/bar.pdf)
|
||||
const { Media } = require('./markdown-it-media');
|
||||
const Katex = require('./markdown-it-katex');
|
||||
const { default: Katex } = require('./markdown-it-katex');
|
||||
const { xssProtector } = require('./markdown-it-xss');
|
||||
|
||||
class Markdown extends MarkdownIt {
|
||||
|
||||
Reference in new issue
Block a user