Skip to content

Commit

Permalink
fix(0.5.1): 修复导入功能 (#86)
Browse files Browse the repository at this point in the history
* 修复 Valine 导入 #70
* 修复 Disqus 导入
  • Loading branch information
imaegoo authored Jan 4, 2021
1 parent 47f02e5 commit 8217507
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
.gitignore
.npmignore
_config.js
babel.config.json
cloudbaserc.json
demo
dist/demo.css
dist/demo.html
dist/index.html
docs
node_modules
public
src
stats.json
webpack.config.js
2 changes: 1 addition & 1 deletion babel.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-modules-commonjs","@babel/transform-runtime"]
"plugins": ["@babel/plugin-transform-modules-commonjs", "@babel/transform-runtime"]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/.vuepress/theme/layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"></script>
<div id="twikoo"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/twikoo.all.min.js" ref="twikooJs"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/twikoo.all.min.js" ref="twikooJs"></script>
</div>
</template>
</ParentLayout>
Expand Down
4 changes: 2 additions & 2 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Volantis 目前支持 Twikoo,请查看 [hexo-theme-volantis/_config.yml](https
``` yml
comments:
twikoo:
js: https://cdn.jsdelivr.net/npm/[email protected].0/dist/twikoo.all.min.js
js: https://cdn.jsdelivr.net/npm/[email protected].1/dist/twikoo.all.min.js
envId: xxxxxxxxxxxxxxx # 腾讯云环境id
```
Expand All @@ -124,7 +124,7 @@ twikoo:
``` html
<div id="tcomment"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/twikoo.all.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/twikoo.all.min.js"></script>
<script>
twikoo.init({
envId: '您的环境id',
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo",
"version": "0.5.0",
"version": "0.5.1",
"description": "A simple comment system based on Tencent CloudBase (tcb).",
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
Expand All @@ -14,8 +14,8 @@
},
"homepage": "https://twikoo.js.org",
"scripts": {
"dev": "node_modules/.bin/webpack serve",
"serve": "node_modules/.bin/webpack serve",
"dev": "webpack serve --mode development",
"serve": "webpack serve --mode development",
"build": "webpack --mode production",
"analyze": "webpack --profile --json > stats.json && webpack-bundle-analyzer stats.json",
"login": "tcb login",
Expand Down
44 changes: 33 additions & 11 deletions src/function/twikoo/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Twikoo cloudbase function v0.5.0
* Twikoo cloudbase function v0.5.1
* (c) 2020-2020 iMaeGoo
* Released under the MIT License.
*/
Expand Down Expand Up @@ -31,7 +31,7 @@ const window = new JSDOM('').window
const DOMPurify = createDOMPurify(window)

// 常量 / constants
const VERSION = '0.5.0'
const VERSION = '0.5.1'
const RES_CODE = {
SUCCESS: 0,
FAIL: 1000,
Expand Down Expand Up @@ -456,7 +456,7 @@ async function readFile (fileId, type, log) {
let content = result.fileContent.toString('utf8')
log('评论文件读取成功')
if (type === 'json') {
content = JSON.parse(content)
content = jsonParse(content)
log('评论文件 JSON 解析成功')
} else if (type === 'xml') {
content = await xml2js.parseStringPromise(content)
Expand All @@ -468,6 +468,23 @@ async function readFile (fileId, type, log) {
}
}

// 兼容 Leancloud 两种 JSON 导出格式
function jsonParse (content) {
try {
return JSON.parse(content)
} catch (e1) {
const results = []
const lines = content.split('\n')
for (const line of lines) {
// 逐行 JSON.parse
try {
results.push(JSON.parse(line))
} catch (e2) {}
}
return { results }
}
}

// Valine 导入
async function commentImportValine (valineDb, log) {
if (!valineDb || !valineDb.results) {
Expand Down Expand Up @@ -551,7 +568,11 @@ async function commentImportDisqus (disqusDb, log) {
nick: post.author[0].name[0],
mail: '',
link: '',
url: thread.url,
url: thread.url
? thread.url.indexOf('http') === 0
? getRelativeUrl(thread.url)
: thread.url
: getRelativeUrl(thread.href),
href: thread.href,
comment: post.message[0],
ua: '',
Expand All @@ -574,20 +595,21 @@ async function commentImportDisqus (disqusDb, log) {
return comments
}

function getRelativeUrl (url) {
let x = url.indexOf('/')
for (let i = 0; i < 2; i++) {
x = url.indexOf('/', x + 1)
}
return url.substring(x)
}

// Artalk 导入
async function commentImportArtalk (artalkDb, log) {
const comments = []
if (!artalkDb || !artalkDb.length) {
log('Artalk 评论文件格式有误')
return
}
const getRelativeUrl = (url) => {
let x = url.indexOf('/')
for (let i = 0; i < 2; i++) {
x = url.indexOf('/', x + 1)
}
return url.substring(x)
}
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
Expand Down
2 changes: 1 addition & 1 deletion src/function/twikoo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-func",
"version": "0.5.0",
"version": "0.5.1",
"description": "A simple comment system based on Tencent CloudBase (tcb).",
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
Expand Down
36 changes: 18 additions & 18 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const VueLoaderPlugin = require('vue-loader/lib/plugin')
const ROOT_PATH = path.resolve(__dirname)
const BUILD_PATH = path.resolve(ROOT_PATH, 'dist')
const version = require('./package.json').version
const TerserPlugin = require("terser-webpack-plugin");
const TerserPlugin = require('terser-webpack-plugin')
const banner =
'Twikoo v' + version + '\n' +
'(c) 2020-' + new Date().getFullYear() + ' iMaeGoo\n' +
Expand All @@ -18,7 +18,7 @@ module.exports = {
{ test: /\.vue$/, loader: 'vue-loader' },
{ test: /\.css$/, use: ['vue-style-loader', 'css-loader'] },
{ test: /\.svg$/, loader: 'svg-inline-loader' },
{ test: /\.js$/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] ,plugins: ["@babel/plugin-transform-modules-commonjs","@babel/transform-runtime"]} } }
{ test: /\.js$/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], plugins: ['@babel/plugin-transform-modules-commonjs', '@babel/transform-runtime'] } } }
]
},
entry: {
Expand All @@ -37,32 +37,32 @@ module.exports = {
new webpack.BannerPlugin(banner),
new CopyPlugin({
patterns: [
{ from: 'public/', to: './' }
{ from: 'demo/', to: './' }
]
}),
new VueLoaderPlugin(),
new TerserPlugin({
parallel: 4,
terserOptions: {
ecma: 5,
toplevel: true,
ie8: true,
safari10: true,
},
parallel: 4,
terserOptions: {
ecma: 5,
toplevel: true,
ie8: true,
safari10: true
}
})
],
devServer: {
static: [{
directory: BUILD_PATH,
publicPath: '/dist/',
serveIndex: true,
watch: true,
},],
directory: BUILD_PATH,
publicPath: '/dist/',
serveIndex: true,
watch: true
}],
port: 9820,
host: 'localhost',
open:true,
hot:true,
compress: true,
open: true,
hot: true,
compress: true
},
performance: {
maxEntrypointSize: 524288,
Expand Down

0 comments on commit 8217507

Please sign in to comment.