feat: 解析md显示,js模块化

master
LCJ-MinYa 11 months ago
parent ef595d195a
commit 834cdfdf6e

@ -34,6 +34,7 @@
"element-plus": "^2.8.0", "element-plus": "^2.8.0",
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"localforage": "^1.10.0", "localforage": "^1.10.0",
"marked": "^15.0.7",
"mitt": "^3.0.1", "mitt": "^3.0.1",
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"path": "^0.12.7", "path": "^0.12.7",

@ -47,6 +47,9 @@ importers:
localforage: localforage:
specifier: ^1.10.0 specifier: ^1.10.0
version: 1.10.0 version: 1.10.0
marked:
specifier: ^15.0.7
version: 15.0.7
mitt: mitt:
specifier: ^3.0.1 specifier: ^3.0.1
version: 3.0.1 version: 3.0.1
@ -2206,6 +2209,11 @@ packages:
magic-string@0.30.11: magic-string@0.30.11:
resolution: {integrity: sha512-+Wri9p0QHMy+545hKww7YAu5NyzF8iomPL/RQazugQ9+Ez4Ic3mERMd8ZTX5rfK944j+560ZJi8iAwgak1Ac7A==} resolution: {integrity: sha512-+Wri9p0QHMy+545hKww7YAu5NyzF8iomPL/RQazugQ9+Ez4Ic3mERMd8ZTX5rfK944j+560ZJi8iAwgak1Ac7A==}
marked@15.0.7:
resolution: {integrity: sha512-dgLIeKGLx5FwziAnsk4ONoGwHwGPJzselimvlVskE9XLN4Orv9u2VA3GWw/lYUqjfA0rUT/6fqKwfZJapP9BEg==}
engines: {node: '>= 18'}
hasBin: true
mathml-tag-names@2.1.3: mathml-tag-names@2.1.3:
resolution: {integrity: sha512-APMBEanjybaPzUrfqU0IMU5I0AswKMH7k8OTLs0vvV4KZpExkTkY87nR/zpbuTPj+gARop7aGUbl11pnDfW6xg==} resolution: {integrity: sha512-APMBEanjybaPzUrfqU0IMU5I0AswKMH7k8OTLs0vvV4KZpExkTkY87nR/zpbuTPj+gARop7aGUbl11pnDfW6xg==}
@ -5693,6 +5701,8 @@ snapshots:
dependencies: dependencies:
'@jridgewell/sourcemap-codec': 1.5.0 '@jridgewell/sourcemap-codec': 1.5.0
marked@15.0.7: {}
mathml-tag-names@2.1.3: {} mathml-tag-names@2.1.3: {}
mdn-data@2.0.14: {} mdn-data@2.0.14: {}

@ -0,0 +1,36 @@
CommonJS、AMD、CMD 和 ESMES6 Modules是 JavaScript 中的不同模块化规范。它们之间有一定的关系和区别,以下是对它们的概述及相互关系的说明:
### CommonJS
- **定义**:主要用于服务器端的模块化,广泛应用于 Node.js。
- **特点**
- **同步加载**:模块是同步加载,适合服务器环境。
- **导入导出**:使用 `require()` 导入模块,使用 `module.exports` 导出模块。
### AMD (Asynchronous Module Definition)
- **定义**:主要用于浏览器端的模块化,支持异步加载。
- **特点**
- **异步加载**:模块可以异步加载,适合在浏览器中使用。
- **导入导出**:使用 `define()` 定义模块,使用 `require()` 加载模块。
### CMD (Common Module Definition)
- **定义**:对 AMD 的一种改进,主要由 SeaJS 提出。
- **特点**
- **按需加载**:支持动态依赖,模块可以在需要时才加载。
- **导入导出**:使用 `define()` 定义模块,依赖在使用时声明。
### ESM (ECMAScript Modules)
- **定义**ES6 引入的模块化标准,现已成为 JavaScript 的官方模块系统。
- **特点**
- **静态解析**:在编译时解析模块依赖,支持树摇优化。
- **导入导出**:使用 `import` 导入模块,使用 `export` 导出模块。
- **异步加载**:通过 `<script type="module">` 标签支持异步加载。
### 关系总结
- **相似性**:所有这些规范的目的都是实现模块化,促进代码的组织和复用。
- **差异性**
- **加载方式**CommonJS 是同步加载AMD 和 CMD 支持异步加载,而 ESM 也支持异步加载且具有静态结构。
- **使用场景**CommonJS 主要在服务器端AMD 和 CMD 主要用于浏览器端ESM 在浏览器和 Node.js 中都得到支持。
- **发展顺序**CommonJS 和 AMD 是较早的模块化方案,而 ESM 是现代 JavaScript 的标准模块化方式,逐渐取代了前者。
### 结论
在现代 JavaScript 开发中ESM 是推荐的标准能够提供更好的性能和兼容性。CommonJS、AMD 和 CMD 主要在旧项目和特定场景中被使用。

@ -189,5 +189,6 @@ const transitionMain = defineComponent({
.main-content { .main-content {
margin: 0; margin: 0;
background: #fff;
} }
</style> </style>

@ -56,6 +56,10 @@ const titleArr = [
key: 'cookie', key: 'cookie',
title: '相同顶级域名不同子域名共享cookie', title: '相同顶级域名不同子域名共享cookie',
}, },
{
key: 'module',
title: 'CommonJS AMD CMD ESM总结',
},
]; ];
// @/views/demo/**/*.vue // @/views/demo/**/*.vue

@ -1,91 +1,89 @@
*, *,
::before, ::before,
::after { ::after {
box-sizing: border-box; box-sizing: border-box;
border-color: currentColor; border-color: currentColor;
border-style: solid; border-style: solid;
border-width: 0; border-width: 0;
} }
#app { #app {
width: 100%; width: 100%;
height: 100%; height: 100%;
} }
html { html {
box-sizing: border-box; box-sizing: border-box;
width: 100%; width: 100%;
height: 100%; height: 100%;
line-height: 1.5; line-height: 1.5;
tab-size: 4; tab-size: 4;
text-size-adjust: 100%; text-size-adjust: 100%;
} }
body { body {
width: 100%; width: 100%;
height: 100%; height: 100%;
margin: 0; margin: 0;
font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
"Microsoft YaHei", "微软雅黑", Arial, sans-serif; line-height: inherit;
line-height: inherit; -moz-osx-font-smoothing: grayscale;
-moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased;
-webkit-font-smoothing: antialiased; text-rendering: optimizelegibility;
text-rendering: optimizelegibility;
} }
hr { hr {
height: 0; height: 0;
color: inherit; color: inherit;
border-top-width: 1px; border-top-width: 1px;
} }
abbr:where([title]) { abbr:where([title]) {
text-decoration: underline dotted; text-decoration: underline dotted;
} }
a { a {
color: inherit; color: inherit;
text-decoration: inherit; text-decoration: inherit;
} }
b, b,
strong { strong {
font-weight: bolder; font-weight: bolder;
} }
code, code,
kbd, kbd,
samp, samp,
pre { pre {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
"Liberation Mono", "Courier New", monospace; font-size: 1em;
font-size: 1em;
} }
small { small {
font-size: 80%; font-size: 80%;
} }
sub, sub,
sup { sup {
position: relative; position: relative;
font-size: 75%; font-size: 75%;
line-height: 0; line-height: 0;
vertical-align: baseline; vertical-align: baseline;
} }
sub { sub {
bottom: -0.25em; bottom: -0.25em;
} }
sup { sup {
top: -0.5em; top: -0.5em;
} }
table { table {
text-indent: 0; text-indent: 0;
border-collapse: collapse; border-collapse: collapse;
border-color: inherit; border-color: inherit;
} }
button, button,
@ -93,53 +91,53 @@ input,
optgroup, optgroup,
select, select,
textarea { textarea {
padding: 0; padding: 0;
margin: 0; margin: 0;
font-family: inherit; font-family: inherit;
font-size: 100%; font-size: 100%;
line-height: inherit; line-height: inherit;
color: inherit; color: inherit;
} }
button, button,
select { select {
text-transform: none; text-transform: none;
} }
button, button,
[type="button"], [type='button'],
[type="reset"], [type='reset'],
[type="submit"] { [type='submit'] {
background-image: none; background-image: none;
} }
:-moz-focusring { :-moz-focusring {
outline: auto; outline: auto;
} }
:-moz-ui-invalid { :-moz-ui-invalid {
box-shadow: none; box-shadow: none;
} }
progress { progress {
vertical-align: baseline; vertical-align: baseline;
} }
::-webkit-inner-spin-button, ::-webkit-inner-spin-button,
::-webkit-outer-spin-button { ::-webkit-outer-spin-button {
height: auto; height: auto;
} }
[type="search"] { [type='search'] {
outline-offset: -2px; outline-offset: -2px;
} }
::-webkit-file-upload-button { ::-webkit-file-upload-button {
font: inherit; font: inherit;
} }
summary { summary {
display: list-item; display: list-item;
} }
blockquote, blockquote,
@ -155,43 +153,43 @@ hr,
figure, figure,
p, p,
pre { pre {
margin: 0; margin: 0;
} }
fieldset { fieldset {
padding: 0; padding: 0;
margin: 0; margin: 0;
} }
legend { legend {
padding: 0; padding: 0;
} }
ol, ol,
ul, ul,
menu { menu {
padding: 0; padding: 0;
margin: 0; margin: 0;
list-style: none; list-style: none;
} }
textarea { textarea {
resize: vertical; resize: vertical;
} }
input::placeholder, input::placeholder,
textarea::placeholder { textarea::placeholder {
color: #9ca3af; color: #9ca3af;
opacity: 1; opacity: 1;
} }
button, button,
[role="button"] { [role='button'] {
cursor: pointer; cursor: pointer;
} }
:disabled { :disabled {
cursor: default; cursor: default;
} }
svg, svg,
@ -201,57 +199,74 @@ audio,
iframe, iframe,
embed, embed,
object { object {
display: block; display: block;
} }
img, img,
video { video {
max-width: 100%; max-width: 100%;
height: auto; height: auto;
} }
[hidden] { [hidden] {
display: none; display: none;
} }
.dark { .dark {
color-scheme: dark; color-scheme: dark;
} }
label { label {
font-weight: 700; font-weight: 700;
} }
*, *,
*::before, *::before,
*::after { *::after {
box-sizing: inherit; box-sizing: inherit;
} }
a:focus, a:focus,
a:active { a:active {
outline: none; outline: none;
} }
a, a,
a:focus, a:focus,
a:hover { a:hover {
color: inherit; color: inherit;
text-decoration: none; text-decoration: none;
cursor: pointer; cursor: pointer;
} }
div:focus { div:focus {
outline: none; outline: none;
} }
.clearfix { .clearfix {
&::after { &::after {
display: block; display: block;
height: 0; height: 0;
clear: both; clear: both;
font-size: 0; font-size: 0;
visibility: hidden; visibility: hidden;
content: " "; content: ' ';
} }
}
// markdown
.md-main {
p,
ul,
ol {
margin: 16px 0;
}
h3 {
margin: 18px 0;
}
ul,
ol {
padding-left: 40px;
list-style: disc;
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

@ -0,0 +1,24 @@
<template>
<el-card>
<img src="./img/1.png" />
<div
class="md-main"
v-html="htmlStr"
></div>
</el-card>
</template>
<script setup>
import { ref } from 'vue';
import { marked } from 'marked';
const htmlStr = ref('');
fetch('./summer.md', {
method: 'GET',
}).then((result) => {
result.text().then((res) => {
htmlStr.value = marked(res);
});
});
</script>
Loading…
Cancel
Save