提交 d4dba8fa 编写于 作者: D DCloud_LXH

feat: inject-json-to-md 使用新的方式

上级 9356d903
let cssJson = {}; let cssJson = {}
let utsJson = {}; let utsJson = {}
let utsApiJson = {}; let utsApiJson = {}
let utsComJson = {}; let utsComJson = {}
let utsUnicloudApiJson = {}; let utsUnicloudApiJson = {}
let customTypeJson = {}; let customTypeJson = {}
let vueJson = {}; let vueJson = {}
let manifestJson = {}; let manifestJson = {}
let pagesJson = {}; let pagesJson = {}
let specialStringJson = {}; let specialStringJson = {}
let pageInstanceJson = {}; let pageInstanceJson = {}
try { try {
cssJson = require('../utils/cssJson.json'); cssJson = require('../utils/cssJson.json')
} catch (error) {} } catch (error) {}
try { try {
utsJson = require('../utils/utsJson.json'); utsJson = require('../utils/utsJson.json')
} catch (error) {} } catch (error) {}
try { try {
utsApiJson = require('../utils/utsApiJson.json'); utsApiJson = require('../utils/utsApiJson.json')
} catch (error) {} } catch (error) {}
try { try {
utsComJson = require('../utils/utsComJson.json'); utsComJson = require('../utils/utsComJson.json')
} catch (error) {} } catch (error) {}
try { try {
utsUnicloudApiJson = require('../utils/utsUnicloudApiJson.json'); utsUnicloudApiJson = require('../utils/utsUnicloudApiJson.json')
} catch (error) {} } catch (error) {}
try { try {
customTypeJson = require('../utils/customTypeJson.json'); customTypeJson = require('../utils/customTypeJson.json')
} catch (error) {} } catch (error) {}
try { try {
vueJson = require('../utils/vueJson.json'); vueJson = require('../utils/vueJson.json')
} catch (error) {} } catch (error) {}
try { try {
manifestJson = require('../utils/maniFestJson.json'); manifestJson = require('../utils/manifestJson.json')
} catch (error) {} } catch (error) {}
try { try {
pagesJson = require('../utils/pagesJson.json'); pagesJson = require('../utils/pagesJson.json')
} catch (error) {} } catch (error) {}
try { try {
specialStringJson = require('../utils/specialStringJson.json'); specialStringJson = require('../utils/specialStringJson.json')
} catch (error) {} } catch (error) {}
try { try {
pageInstanceJson = require('../utils/pageInstanceJson.json'); pageInstanceJson = require('../utils/pageInstanceJson.json')
} catch (error) {} } catch (error) {}
function getRegExp(key) { function getRegExp(key) {
return new RegExp(`<!--\\s*${key}.([\\w\\W]+[^\\s])\\s*-->`) return new RegExp(`<!--\\s*${key}.([\\w\\W]+[^\\s])\\s*-->`)
} }
/**
*
* @param {string} text
* @returns {{match: RegExpMatchArray | null, json: {}}
*/
const getJSON = text => { const getJSON = text => {
let match = text.match(getRegExp('CSSJSON')); let match = text.match(getRegExp('CSSJSON'))
if (match) { if (match) {
return { return {
match, match,
json: cssJson, json: cssJson,
}; }
} }
match = text.match(getRegExp('UTSJSON')); match = text.match(getRegExp('UTSJSON'))
if (match) { if (match) {
return { return {
match, match,
json: utsJson, json: utsJson,
}; }
} }
match = text.match(getRegExp('UTSAPIJSON')); match = text.match(getRegExp('UTSAPIJSON'))
if (match) { if (match) {
return { return {
match, match,
json: utsApiJson, json: utsApiJson,
}; }
} }
match = text.match(getRegExp('UTSCOMJSON')); match = text.match(getRegExp('UTSCOMJSON'))
if (match) { if (match) {
return { return {
match, match,
json: utsComJson, json: utsComJson,
}; }
} }
match = text.match(getRegExp('UTSUNICLOUDAPIJSON')); match = text.match(getRegExp('UTSUNICLOUDAPIJSON'))
if (match) { if (match) {
return { return {
match, match,
json: utsUnicloudApiJson, json: utsUnicloudApiJson,
}; }
} }
match = text.match(getRegExp('CUSTOMTYPEJSON')); match = text.match(getRegExp('CUSTOMTYPEJSON'))
if (match) { if (match) {
return { return {
match, match,
json: customTypeJson, json: customTypeJson,
}; }
} }
match = text.match(getRegExp('VUEJSON')); match = text.match(getRegExp('VUEJSON'))
if (match) { if (match) {
return { return {
match, match,
json: vueJson, json: vueJson,
}; }
} }
match = text.match(getRegExp('MANIFESTJSON')); match = text.match(getRegExp('MANIFESTJSON'))
if (match) { if (match) {
return { return {
match, match,
json: manifestJson, json: manifestJson,
}; }
} }
match = text.match(getRegExp('PAGESJSON')); match = text.match(getRegExp('PAGESJSON'))
if (match) { if (match) {
return { return {
match, match,
json: pagesJson, json: pagesJson,
}; }
} }
match = text.match(getRegExp('SPECIALSTRINGJSON')); match = text.match(getRegExp('SPECIALSTRINGJSON'))
if (match) { if (match) {
return { return {
match, match,
json: specialStringJson, json: specialStringJson,
}; }
} }
match = text.match(getRegExp('PAGEINSTANCE')); match = text.match(getRegExp('PAGEINSTANCE'))
if (match) { if (match) {
return { return {
match, match,
json: pageInstanceJson, json: pageInstanceJson,
}; }
} }
return { return {
match: null, match: null,
json: {}, json: {},
}; }
}; }
module.exports = function (md, opts) { const NEWLINE_CHARACTER = /\r?\n/
if (Object.keys(cssJson) === 0) return false;
md.core.ruler.after('inline', 'merge-css', function (state) { module.exports = md => {
const ids = [] md.parse = (function (MD_PARSE) {
let idIdx = 1 return function (src, ...args) {
for (let index = 0; index < state.tokens.length; index++) { if (src && getJSON(src).match) {
const blockToken = state.tokens[index]; const lines = src.split(NEWLINE_CHARACTER)
if (blockToken.type === 'html_block') { for (let index = 0; index < lines.length; index++) {
const { match, json } = getJSON(blockToken.content); const line = lines[index]
if (match) {
const jsonPath = match[1]; const { match, json } = getJSON(line)
const path = jsonPath.split('.'); if (match) {
let temp = json; const jsonPath = match[1]
path.forEach(key => { const path = jsonPath.split('.')
if (!temp) return false; let temp = json
temp = temp[key]; path.forEach(key => {
}); if (!temp) return false
if (!temp) continue; temp = temp[key]
const parseTokens = md.parse(temp); })
parseTokens.forEach(token => { if (!temp) continue
if (token.type === 'heading_open') { lines[index] = temp
const id = token.attrGet('id') }
if(ids.includes(id)) { }
token.attrSet('id', `${id}_${idIdx++}`)
} else { return MD_PARSE.bind(this)(lines.join('\n'), ...args)
ids.push(id) }
} return MD_PARSE.bind(this)(src, ...args)
} }
}) })(md.parse)
state.tokens.splice(index, 1, ...parseTokens); }
index = index + parseTokens.length - 1;
// blockToken.content = temp
}
}
}
return false;
});
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册