MCPcopy Index your code
hub / github.com/zxlie/FeHelper / createNode

Function createNode

apps/json-format/format-lib.js:1945–2275  ·  view source on GitHub ↗
(value)

Source from the content-addressed store, hash-verified

1943
1944 // 创建节点 - 直接复用webworker中的完整逻辑
1945 function createNode(value) {
1946 let node = {
1947 type: getType(value),
1948 value: value,
1949 children: [],
1950
1951 getHTML: function() {
1952 switch(this.type) {
1953 case 'string':
1954 const wrapLongString = shouldWrapLongString(this.value);
1955 const lineClass = wrapLongString ? 'item item-line item-line-wrap' : 'item item-line';
1956 const stringClass = wrapLongString ? 'string string-long' : 'string';
1957 // 判断原始字符串是否为URL
1958 if (isUrl(this.value)) {
1959 // 用JSON.stringify保证转义符显示,内容包裹在<a>里
1960 return '<div class="' + lineClass + '"><span class="' + stringClass + '"><a href="'
1961 + htmlspecialchars(this.value) + '" target="_blank" rel="noopener noreferrer" data-is-link="1" data-link-url="' + htmlspecialchars(this.value) + '">'
1962 + htmlspecialchars(JSON.stringify(this.value)) + '</a></span></div>';
1963 } else {
1964 // 检测字符串是否是有效的JSON(用于转义功能)
1965 // 当转义功能开启时,如果字符串是有效的JSON,就格式化显示
1966 if (escapeJsonStringEnabled) {
1967 const strValue = String(this.value);
1968 // 检查字符串是否看起来像JSON(以[或{开头,以]或}结尾)
1969 const trimmed = strValue.trim();
1970 if ((trimmed.startsWith('[') && trimmed.endsWith(']')) ||
1971 (trimmed.startsWith('{') && trimmed.endsWith('}'))) {
1972 try {
1973 // 尝试解析为JSON,使用全局的 JSON.parse(已被 json-bigint.js 覆盖)
1974 const parsed = JSON.parse(strValue);
1975 // 如果解析成功且是对象或数组,格式化显示
1976 if (typeof parsed === 'object' && parsed !== null) {
1977 const nestedNode = createNode(parsed);
1978 // 获取嵌套JSON的完整HTML(完全展开)
1979 let nestedHTML = nestedNode.getHTML();
1980 // 移除外层的item容器div,只保留内部内容
1981 nestedHTML = nestedHTML.replace(/^<div class="item[^"]*">/, '').replace(/<\/div>$/, '');
1982 // 返回格式化的JSON结构,但保持在外层的字符串容器中
1983 // 使用block显示,确保完全展开
1984 return '<div class="' + lineClass + '"><span class="' + stringClass + '">' +
1985 '<span class="quote">"</span>' +
1986 '<div class="string-json-nested" style="display:block;margin-left:0;padding-left:0;">' +
1987 nestedHTML +
1988 '</div>' +
1989 '<span class="quote">"</span>' +
1990 '</span></div>';
1991 }
1992 } catch (e) {
1993 // 解析失败,按普通字符串处理
1994 }
1995 }
1996 }
1997 return '<div class="' + lineClass + '"><span class="' + stringClass + '">' + formatStringValue(JSON.stringify(this.value)) + '</span></div>';
1998 }
1999 case 'number':
2000 // 确保大数字不使用科学计数法
2001 let numStr = typeof this.value === 'number' && this.value.toString().includes('e')
2002 ? this.value.toLocaleString('fullwide', {useGrouping: false})

Callers 1

formatJsonToHtmlFunction · 0.70

Calls 8

shouldWrapLongStringFunction · 0.85
_normalizePreservedKeyFunction · 0.85
getTypeFunction · 0.70
isUrlFunction · 0.70
htmlspecialcharsFunction · 0.70
formatStringValueFunction · 0.70
parseMethod · 0.45

Tested by

no test coverage detected