Skip to content

Commit

Permalink
upgrade deps and change impl to tag plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kairlec committed Jul 12, 2022
1 parent 3f79319 commit 82d3fce
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 393 deletions.
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/
test.js
.idea
node_modules
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
const { initPreConfig, isPluginDisabled, rendererFilterFunction, makeBodyEndInjectContent, makeHeadEndInjectContent } = require('./lib/index')
const {
initPreConfig,
isPluginDisabled,
ktpgResolveFunction,
makeBodyEndInjectContent,
makeHeadEndInjectContent
} = require('./lib/index')

initPreConfig(hexo.config.kotlin_playground || {})

if (!isPluginDisabled()) {
hexo.extend.injector.register('head_end', makeHeadEndInjectContent(), 'default')
hexo.extend.injector.register('head_end', makeHeadEndInjectContent(), 'default')

hexo.extend.injector.register('body_end', makeBodyEndInjectContent(), 'default')
hexo.extend.injector.register('body_end', makeBodyEndInjectContent(), 'default')

hexo.extend.filter.register('marked:renderer', rendererFilterFunction)
}

hexo.extend.tag.register('ktpg', (args, contents, err) => {
return ktpgResolveFunction(hexo, args, contents, err)
}, {
ends: true,
})
209 changes: 91 additions & 118 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,121 @@
const { toHorizontalLine, deepCopy } = require('./util')
const { registerSpecialKeyValue, getSpecialKeyValue, setSpecialKeyValue, getRealKey } = require('./SpecialKeyValue')
const {toHorizontalLine, deepCopy} = require('./util')
const {registerSpecialKeyValue, getSpecialKeyValue, setSpecialKeyValue, getRealKey} = require('./SpecialKeyValue')

function makeSpecialConfigKey (srcKey, targetKey, setFunction, getFunction) {
if (getFunction === undefined) {
if (typeof targetKey === 'function') {
registerSpecialKeyValue(toHorizontalLine(srcKey), srcKey, targetKey, setFunction)
} else if (typeof targetKey === 'undefined') {
registerSpecialKeyValue(toHorizontalLine(srcKey), srcKey, setFunction, getFunction)
function makeSpecialConfigKey(srcKey, targetKey, setFunction, getFunction) {
if (getFunction === undefined) {
if (typeof targetKey === 'function') {
registerSpecialKeyValue(toHorizontalLine(srcKey), srcKey, targetKey, setFunction)
} else if (typeof targetKey === 'undefined') {
registerSpecialKeyValue(toHorizontalLine(srcKey), srcKey, setFunction, getFunction)
} else {
registerSpecialKeyValue(toHorizontalLine(srcKey), targetKey, setFunction, getFunction)
}
} else {
registerSpecialKeyValue(toHorizontalLine(srcKey), targetKey, setFunction, getFunction)
registerSpecialKeyValue(toHorizontalLine(srcKey), toHorizontalLine(targetKey), setFunction, getFunction)
}
} else {
registerSpecialKeyValue(toHorizontalLine(srcKey), toHorizontalLine(targetKey), setFunction, getFunction)
}
}

function parseAttribute (str, pre) {
const config = new Config(pre)
while (true) {
// eslint-disable-next-line
let result = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/.exec(str)
if (result === null) {
break
}
config.set(result[1], result[2] || result[3] || result[4])
str = str.slice(result.index + result[0].length)
}
return config
function parseAttribute(config, args) {
args.forEach((arg) => {
// eslint-disable-next-line
let result = /^([^=]+)(?:=(.+))?$/.exec(arg)
if (result !== null) {
config.set(result[1].trim(), result[2].trim())
} else {
console.warn("unknown config format:", arg)
}
})
}

function getCodeBlockConfig (sourceCode, pre) {
// 获取第一行注释
const firstLineIndex = sourceCode.indexOf('\n')
if (firstLineIndex === -1) {
return { config: new Config(pre), code: sourceCode }
}
const firstLine = sourceCode.slice(0, firstLineIndex)
const comment = /^\s*\/{2,}@playground *(.*)$/m.exec(firstLine)
if (comment !== null && comment[1].length > 0) {
return { config: parseAttribute(comment[1], pre), code: sourceCode.slice(firstLineIndex + 1) }
} else {
return { config: new Config(pre), code: sourceCode }
}
function getCodeBlockConfig(args, pre) {
const config = new Config(pre)
parseAttribute(config, args)
return config
}

function __getRealKey (key) {
return getRealKey(toHorizontalLine(key))
function __getRealKey(key) {
return getRealKey(toHorizontalLine(key))
}

function Config (obj) {
if (obj instanceof Config) {
this.enabled = obj.enabled
this.config = deepCopy(obj.config)
} else {
this.config = {}
if (obj) {
for (const key in obj) {
this.set(__getRealKey(key), obj[key])
}
this.enabled = !!obj.enabled
function Config(obj) {
if (obj instanceof Config) {
this.enabled = obj.enabled
this.config = deepCopy(obj.config)
} else {
this.enabled = true
this.config = {}
if (obj) {
for (const key in obj) {
this.set(__getRealKey(key), obj[key])
}
this.enabled = !!obj.enabled
} else {
this.enabled = true
}
}
}

this.__set = function (key, value) {
if (key === 'enabled') {
if (typeof value === 'string') {
this.enable(value.toLowerCase() !== 'false')
} else {
this.enable(value)
}
} else {
this.config[key] = value
this.__set = function (key, value) {
this.config[key] = value
}
}

this.__get = function (key) {
return this.config[key]
}

this.set = function (key, ...value) {
if (typeof key !== 'string') {
throw TypeError('key type must be string')
this.__get = function (key) {
return this.config[key]
}
let realValue
for (const v of value) {
if (v !== undefined) {
realValue = v
break
}
}
setSpecialKeyValue(key, realValue, this.__set, this)
}

this.has = function (key) {
return __getRealKey(key) in this.config
}

this.remove = function (key) {
delete this.config[__getRealKey(key)]
}
this.set = function (key, ...value) {
if (typeof key !== 'string') {
throw TypeError('key type must be string')
}
let realValue
for (const v of value) {
if (v !== undefined) {
realValue = v
break
}
}
setSpecialKeyValue(key, realValue, this.__set, this)
}

this.get = function (key, defaultValue) {
if (typeof key !== 'string') {
throw TypeError('key type must be string')
this.has = function (key) {
return __getRealKey(key) in this.config
}
const value = getSpecialKeyValue(key, this.__get, this)
if (value === undefined) {
return defaultValue
} else {
return value

this.remove = function (key) {
delete this.config[__getRealKey(key)]
}
}

this.enable = function (enabled) {
if (enabled === undefined) {
this.enabled = true
} else {
this.enabled = !!enabled
this.get = function (key, defaultValue) {
if (typeof key !== 'string') {
throw TypeError('key type must be string')
}
const value = getSpecialKeyValue(key, this.__get, this)
if (value === undefined) {
return defaultValue
} else {
return value
}
}
}

this.disable = function (disabled) {
if (disabled === undefined) {
this.enabled = false
} else {
this.enabled = !disabled
this.enable = function (enabled) {
if (enabled === undefined) {
this.enabled = true
} else {
this.enabled = !!enabled
}
}
}

this.toString = function (ifNull = (item) => ` ${item}`) {
let str = ''
for (const item in this.config) {
if (this.config[item] === null) {
str += ifNull(item)
} else if (this.config[item] === undefined) {
str += ` ${item}`
} else {
str += ` ${item}="${this.config[item]}"`
}
this.toString = function (ifNull = (item) => ` ${item}`) {
let str = ''
for (const item in this.config) {
if (this.config[item] === null) {
str += ifNull(item)
} else if (this.config[item] === undefined) {
str += ` ${item}`
} else {
str += ` ${item}="${this.config[item]}"`
}
}
return str
}
return str
}
}

exports.getCodeBlockConfig = getCodeBlockConfig
Expand Down
Loading

0 comments on commit 82d3fce

Please sign in to comment.