Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
humyfred committed Jul 1, 2022
2 parents c18f01b + 95a3e09 commit e628b1b
Show file tree
Hide file tree
Showing 38 changed files with 1,022 additions and 560 deletions.
109 changes: 109 additions & 0 deletions build/addons.build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

const babel = require('@rollup/plugin-babel').default;
// node-resolve升级会导致出现新问题
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const eslint = require('@rollup/plugin-eslint');
const alias = require('@rollup/plugin-alias');
const json = require('@rollup/plugin-json');
const typescript = require('rollup-plugin-typescript2');

const path = require('path');
const PROJECT_ROOT_PATH = path.resolve(__dirname, '../');
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const fs = require('fs');

const glob = require('glob');

glob('src/addons/**/*-plugin.js', {
cwd: PROJECT_ROOT_PATH,
}, (error, matches) => {
if (error) {
throw error;
}
buildAddons(matches);
});

const rollup = require('rollup');
const { terser } = require('rollup-plugin-terser');

/**
*
* @param {string[]} entries
*/
function buildAddons(entries) {
entries.forEach(async (entry) => {
const fullEntryPath = path.resolve(PROJECT_ROOT_PATH, entry);

const outputFileName = fullEntryPath.replace(path.resolve(PROJECT_ROOT_PATH, 'src/addons/'), '');
const outputFile = path.join(PROJECT_ROOT_PATH, 'dist/addons', outputFileName);
const declarationDir = path.dirname(outputFile);
const inputFileName = path.basename(entry);
const inputFileExt = path.extname(entry);
const fileNameWithoutExt = inputFileName.replace(inputFileExt, ''); // 简单处理
const camelCaseModuleName = fileNameWithoutExt.split('-').map(segment => segment.replace(/^./, (char) => char.toUpperCase())).join('');

const addonBundle = await rollup.rollup({
input: fullEntryPath,
plugins: [
eslint({
exclude: ['node_modules/**', 'src/sass/**', 'src/libs/**'],
}),
json(),
// envReplacePlugin(),
alias({
entries: [{
find: '@',
replacement: path.resolve(PROJECT_ROOT_PATH, 'src'),
}],
}),
resolve({
ignoreGlobal: false,
browser: true,
}),
typescript({
include: ['*.js', '*.ts'],
tsconfig: path.resolve(PROJECT_ROOT_PATH, 'tsconfig.addons.json'),
tsconfigOverride: {
include: [fullEntryPath], // FIXME: 临时方案确保不会重复生成其他插件的 d.ts
// outDir: declarationDir,
},
}),
commonjs({
include: [/node_modules/, /src[\\/]libs/], // Default: undefined
extensions: ['.js'], // Default: [ '.js' ]
ignoreGlobal: false, // Default: false
sourceMap: !IS_PRODUCTION, // Default: true
}),
babel({
babelHelpers: 'runtime',
exclude: [/node_modules[\\/](?!codemirror[\\/]src[\\/]|parse5)/],
}),
]
});

console.log('[addons build] generating bundle %s', outputFile);

// generate code and a sourcemap
const { output: outputs } = await addonBundle.generate({
// dir: declarationDir,
file: outputFile,
format: 'umd',
name: camelCaseModuleName,
plugins: [
terser(),
]
});

// TODO: ts declaration 生成的目录不符合预期,以下为临时处理方案
outputs.forEach(output => {
const fileNameOnly = path.basename(output.fileName);
const targetPath = path.join(declarationDir, fileNameOnly);
console.log('[addons build] writing %s %s', output.type, targetPath);
fs.mkdirSync(declarationDir, {
recursive: true,
});
fs.writeFileSync(targetPath, output.code || output.source || '', { encoding: 'utf-8' });
});
});
}
2 changes: 2 additions & 0 deletions build/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import eslint from '@rollup/plugin-eslint';
import alias from '@rollup/plugin-alias';
import json from '@rollup/plugin-json';
import envReplacePlugin from './env';
import dartSass from 'sass';

const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const PROJECT_ROOT_PATH = path.resolve(__dirname, '..');
Expand Down Expand Up @@ -101,6 +102,7 @@ export default {
...(IS_PRODUCTION && {
outputStyle: 'compressed',
}),
sass: dartSass,
}),
babel({
babelHelpers: 'runtime',
Expand Down
2 changes: 2 additions & 0 deletions examples/preview_only.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"
integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
<link href="./markdown/basic.md" rel="preload">
<link rel="stylesheet" href="https://fengyuanchen.github.io/viewerjs/css/viewer.css">
</head>

<body>
Expand All @@ -28,6 +29,7 @@
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@4.6.0/dist/echarts.js"></script>
<!--<script src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>-->
<script src="../dist/cherry-markdown.js"></script>
<script src="https://fengyuanchen.github.io/viewerjs/js/viewer.js"></script>
<script src="./scripts/preview-demo.js"></script>
</body>

Expand Down
16 changes: 12 additions & 4 deletions examples/scripts/multiple-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ var cherryConfig1 = {
'header',
'|',
'list',
{ insert: ['image', 'audio', 'video', 'link', 'hr', 'br', 'code', 'formula', 'toc', 'table', 'pdf', 'word'] },
'image',
'audio',
'video',
'link',
'hr',
'br',
'code',
'formula',
'toc',
'table',
'pdf',
'word',
'graph',
'fullScreen',
'settings',
'export'
],
},
editor: {
Expand Down
14 changes: 14 additions & 0 deletions examples/scripts/preview-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ var cherryConfig = {
editor: {
defaultModel: 'previewOnly',
},
callback: {
onClickPreview: function(e) {
const {target} = e;
if(target.tagName === 'IMG') {
console.log('click img', target);
const tmp = new Viewer(target, {
button: false,
navbar: false,
title: [1, (image, imageData) => `${image.alt.replace(/#.+$/, '')} (${imageData.naturalWidth} × ${imageData.naturalHeight})`],
});
tmp.show();
}
}
},
previewer: {
// 自定义markdown预览区域class
// className: 'markdown'
Expand Down
36 changes: 19 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"build:engine": "cross-env CORE_BUILD=true NODE_ENV=production rollup --config build/rollup.engine.config.js",
"build:engine-cjs": "cross-env BUILD_TARGET=commonjs CORE_BUILD=true NODE_ENV=production rollup --config build/rollup.engine.config.js",
"build:debug": "cross-env NODE_ENV=development HOT_RELOAD=false rollup --config build/dev.js",
"build:addons": "tsc --project tsconfig.addons.json",
"build:addons": "node build/addons.build.js",
"build:types": "tsc --project tsconfig.json",
"clean": "rimraf ./dist",
"check-bundle": "es-check es5 dist/cherry-markdown.js dist/cherry-markdown.min.js",
Expand Down Expand Up @@ -73,40 +73,47 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"@rollup/plugin-replace": "^2.3.1",
"@types/codemirror": "^0.0.108",
"@types/dompurify": "^2.2.3",
"@types/echarts": "4.9.15",
"@types/glob": "^7.2.0",
"@types/jest": "^27.0.3",
"@types/jsdom": "^16.2.13",
"@types/lodash": "^4.14.170",
"@types/mermaid": "^8.2.6",
"@types/node": "^17.0.42",
"@types/virtual-dom": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^4.27.0",
"@typescript-eslint/parser": "^4.27.0",
"anywhere": "^1.5.0",
"async": "^3.1.0",
"babel-jest": "^28.1.0",
"babel-plugin-prismjs": "^2.1.0",
"codemirror": "5.58.2",
"commitizen": "^4.0.5",
"core-js-pure": "^3.8.2",
"cross-env": "^7.0.2",
"cz-customizable": "^6.2.0",
"dom-parser": "^0.1.6",
"dompurify": "^2.0.11",
"es-check": "^5.1.0",
"eslint": "^7.17.0",
"eslint-config-prettier": "^8.1.0",
"eslint-config-tencent": "^1.0.2",
"eslint-plugin-jest": "^25.3.0",
"eslint-plugin-prettier": "^4.0.0",
"glob": "^8.0.3",
"gulp": "^4.0.2",
"gulp-consolidate": "^0.2.0",
"gulp-iconfont": "^11.0.0",
"gulp-rename": "^2.0.0",
"html2canvas": "^1.1.3",
"jest": "^27.5.1",
"jest-diff": "^27.5.1",
"node-sass": "^7.0.0",
"juice": "^8.0.0",
"lodash": "^4.17.20",
"md5": "^2.2.1",
"npm-run-all": "^4.1.5",
"postcss": "^8.2.15",
"prettier": "2.6.2",
"prismjs": "^1.24.1",
"rimraf": "^3.0.2",
"rollup": "^1.27.5",
"rollup-plugin-livereload": "^1.3.0",
Expand All @@ -116,24 +123,19 @@
"rollup-plugin-serve": "^1.0.1",
"rollup-plugin-serve-proxy": "^1.1.4",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.32.1",
"sass": "^1.52.3",
"standard-version": "^9.3.2",
"ts-jest": "^27.1.1",
"ts-node": "^10.7.0",
"typescript": "4.3.5"
"typescript": "4.3.5",
"virtual-dom": "^2.1.1"
},
"dependencies": {
"codemirror": "5.58.2",
"core-js-pure": "^3.8.2",
"dom-parser": "^0.1.6",
"dompurify": "^2.0.11",
"html2canvas": "^1.1.3",
"@types/codemirror": "^0.0.108",
"@types/dompurify": "^2.2.3",
"jsdom": "~19.0.0",
"juice": "^8.0.0",
"lodash": "^4.17.20",
"md5": "^2.2.1",
"mitt": "^3.0.0",
"prismjs": "^1.24.1",
"virtual-dom": "^2.1.1"
"mitt": "^3.0.0"
},
"resolutions": {
"glob-parent": "5.1.2",
Expand Down
3 changes: 3 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const callbacks = {
afterChange: (text, html) => {},
afterInit: (text, html) => {},
beforeImageMounted: (srcProp, src) => ({ srcProp, src }),
onClickPreview: (event) => {},
};

/** @type {Partial<import('~types/cherry').CherryOptions>} */
Expand Down Expand Up @@ -215,6 +216,8 @@ const defaultConfig = {
afterChange: callbacks.afterChange,
afterInit: callbacks.afterInit,
beforeImageMounted: callbacks.beforeImageMounted,
// 预览区域点击事件,previewer.enablePreviewerBubble = true 时生效
onClickPreview: callbacks.onClickPreview,
},
previewer: {
dom: false,
Expand Down
2 changes: 1 addition & 1 deletion src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class Cherry extends CherryStatic {
editor: 'show',
};

if (this.options.isPreviewOnly) {
if (this.options.isPreviewOnly || this.options.editor.defaultModel === 'previewOnly') {
this.options.toolbars.showToolbar = false;
this.options.editor.defaultModel = 'previewOnly';
this.status.editor = 'hide';
Expand Down
8 changes: 6 additions & 2 deletions src/addons/cherry-code-block-plantuml-plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/**
* Copyright (C) 2021 THL A29 Limited, a Tencent company.
*
Expand Down Expand Up @@ -72,13 +73,16 @@ function compress(s1, url) {
}

export default class PlantUMLCodeEngine {
static install(cherryOptions, ...args) {
static install(cherryOptions, args) {
mergeWith(cherryOptions, {
engine: {
syntax: {
codeBlock: {
customRenderer: {
plantuml: new PlantUMLCodeEngine(...args),
plantuml: new PlantUMLCodeEngine({
...args,
...(cherryOptions.engine.syntax.plantuml ?? {}),
}),
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions src/core/hooks/Emphasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
compileRegExp,
DO_NOT_STARTS_AND_END_WITH_SPACES_MULTILINE_ALLOW_EMPTY,
ALLOW_WHITESPACE_MULTILINE,
UNDERSCORE_EMPHASIS_BORDER,
UNDERSCORE_EMPHASIS_BOUNDARY,
} from '@/utils/regexp';

export default class Emphasis extends SyntaxBase {
Expand Down Expand Up @@ -97,9 +97,9 @@ export default class Emphasis extends SyntaxBase {

// UNDERSCORE_EMPHASIS_BORDER:允许除下划线以外的「标点符号」和空格出现,使用[^\w\S \t]或[\W\s]会有性能问题
const underscore = {
begin: `(^|${UNDERSCORE_EMPHASIS_BORDER})(_+)`, // ?<leading>, ?<symbol>
begin: `(^|${UNDERSCORE_EMPHASIS_BOUNDARY})(_+)`, // ?<leading>, ?<symbol>
content: `(${REGEX})`, // ?<text>
end: `\\2(?=${UNDERSCORE_EMPHASIS_BORDER}|$)`,
end: `\\2(?=${UNDERSCORE_EMPHASIS_BOUNDARY}|$)`,
};

asterisk.reg = compileRegExp(asterisk, 'g');
Expand Down
17 changes: 17 additions & 0 deletions src/sass/icons/cherry-table-delete.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/sass/icons/cherry-table-insert-bottom.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e628b1b

Please sign in to comment.