master
parent
2c19a5a489
commit
4345926d07
@ -1,65 +1,72 @@
|
||||
import SourceMenu from "./sourceMenu.js";
|
||||
import prettier from 'prettier/standalone';
|
||||
import parserHtml from 'prettier/parser-html';
|
||||
|
||||
/**
|
||||
* 在编辑器中得到的html源码是没有格式的html字符串
|
||||
* 所以需要格式化展示代码
|
||||
* 格式化html代码
|
||||
*/
|
||||
export const parserHtmlCode = (code) => {
|
||||
try {
|
||||
return prettier.format(code, {
|
||||
parser: 'html',
|
||||
plugins: [parserHtml],
|
||||
// 格式化的标签不换行 例如span标签等>格式化后会换行
|
||||
htmlWhitespaceSensitivity: 'ignore'
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('格式化代码错误', e);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将编辑器html转换为代码块内容
|
||||
*/
|
||||
export const parseEditorCode = (html) => {
|
||||
const code = html
|
||||
.replace(/ /g, '')
|
||||
.replace(new RegExp('<p><br></p>', 'g'), '');
|
||||
const data = parserHtmlCode(code).trim();
|
||||
const textCode = data
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
import SourceMenu from "./sourceMenu.js";
|
||||
import prettier from 'prettier/standalone';
|
||||
import parserHtml from 'prettier/parser-html';
|
||||
|
||||
/**
|
||||
* 在编辑器中得到的html源码是没有格式的html字符串
|
||||
* 所以需要格式化展示代码
|
||||
* 格式化html代码
|
||||
*/
|
||||
export const parserHtmlCode = (code) => {
|
||||
try {
|
||||
return prettier.format(code, {
|
||||
parser: 'html',
|
||||
plugins: [parserHtml],
|
||||
// 格式化的标签不换行 例如span标签等>格式化后会换行
|
||||
htmlWhitespaceSensitivity: 'ignore'
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('格式化代码错误', e);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将编辑器html转换为代码块内容
|
||||
*/
|
||||
export const parseEditorCode = (html) => {
|
||||
const code = html
|
||||
.replace(/ /g, '')
|
||||
.replace(new RegExp('<p><br></p>', 'g'), '');
|
||||
const data = parserHtmlCode(code).trim();
|
||||
const textCode = data
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/ /g, " ");
|
||||
return `<pre><code class="language-html">${textCode}</code></pre>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将代码块转换为编辑器html
|
||||
*/
|
||||
export const parseCodeEditor = (preCode) => {
|
||||
// 转码
|
||||
let data = encodeURI(preCode);
|
||||
// 将 转换为空格
|
||||
data = data.replace(/%C2%A0/g, '%20');
|
||||
// 解码
|
||||
data = decodeURI(data);
|
||||
const htmlStr = data
|
||||
.replace('<pre><code class="language-html">', '')
|
||||
.replace('</code></pre>', '')
|
||||
.replace(/</ig, "<")
|
||||
console.log("textcode",textCode)
|
||||
return `<pre><code class="language-html">${textCode}</code></pre>`;
|
||||
// return `${textCode}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将代码块转换为编辑器html
|
||||
*/
|
||||
export const parseCodeEditor = (preCode) => {
|
||||
// 转码
|
||||
let data = encodeURI(preCode);
|
||||
// 将 转换为空格
|
||||
data = data.replace(/%C2%A0/g, '%20');
|
||||
// 解码
|
||||
data = decodeURI(data);
|
||||
const htmlStr = data
|
||||
.replace('<pre><code class="language-html">', '')
|
||||
.replace('</code></pre>', '')
|
||||
.replace(/</ig, "<")
|
||||
.replace(/>/ig, ">");
|
||||
return htmlStr
|
||||
.replace(new RegExp('\\n', 'g'), '')
|
||||
.replace(new RegExp('<p><br></p>', 'g'), '')
|
||||
.trim();
|
||||
}
|
||||
|
||||
export const sourceConf = {
|
||||
// 工具栏中的唯一key
|
||||
key: 'source',
|
||||
// 组件
|
||||
factory: () => new SourceMenu()
|
||||
console.log("htmlStr",htmlStr
|
||||
.replace(new RegExp('\\n', 'g'), '')
|
||||
.replace(new RegExp('<p><br></p>', 'g'), '')
|
||||
.trim())
|
||||
return htmlStr
|
||||
.replace(new RegExp('\\n', 'g'), '')
|
||||
.replace(new RegExp('<p><br></p>', 'g'), '')
|
||||
.trim();
|
||||
}
|
||||
|
||||
export const sourceConf = {
|
||||
// 工具栏中的唯一key
|
||||
key: 'source',
|
||||
// 组件
|
||||
factory: () => new SourceMenu()
|
||||
};
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue