Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fields content language detection not working properly
- Loading branch information
Showing
4 changed files
with
196 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
function isJSON (str: string) { | ||
try { | ||
if (!['{', '['].includes(str.trim()[0])) | ||
return false; | ||
|
||
JSON.parse(str); | ||
return true; | ||
} | ||
catch (_) { | ||
return false; | ||
} | ||
} | ||
|
||
function isHTML (str: string) { | ||
const tags = [ | ||
'a', | ||
'abbr', | ||
'address', | ||
'area', | ||
'article', | ||
'aside', | ||
'audio', | ||
'b', | ||
'base', | ||
'bdi', | ||
'bdo', | ||
'blockquote', | ||
'body', | ||
'br', | ||
'button', | ||
'canvas', | ||
'caption', | ||
'cite', | ||
'code', | ||
'col', | ||
'colgroup', | ||
'data', | ||
'datalist', | ||
'dd', | ||
'del', | ||
'details', | ||
'dfn', | ||
'dialog', | ||
'div', | ||
'dl', | ||
'dt', | ||
'em', | ||
'embed', | ||
'fieldset', | ||
'figcaption', | ||
'figure', | ||
'footer', | ||
'form', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
'head', | ||
'header', | ||
'hgroup', | ||
'hr', | ||
'html', | ||
'i', | ||
'iframe', | ||
'img', | ||
'input', | ||
'ins', | ||
'kbd', | ||
'label', | ||
'legend', | ||
'li', | ||
'link', | ||
'main', | ||
'map', | ||
'mark', | ||
'math', | ||
'menu', | ||
'menuitem', | ||
'meta', | ||
'meter', | ||
'nav', | ||
'noscript', | ||
'object', | ||
'ol', | ||
'optgroup', | ||
'option', | ||
'output', | ||
'p', | ||
'param', | ||
'picture', | ||
'pre', | ||
'progress', | ||
'q', | ||
'rb', | ||
'rp', | ||
'rt', | ||
'rtc', | ||
'ruby', | ||
's', | ||
'samp', | ||
'script', | ||
'section', | ||
'select', | ||
'slot', | ||
'small', | ||
'source', | ||
'span', | ||
'strong', | ||
'style', | ||
'sub', | ||
'summary', | ||
'sup', | ||
'svg', | ||
'table', | ||
'tbody', | ||
'td', | ||
'template', | ||
'textarea', | ||
'tfoot', | ||
'th', | ||
'thead', | ||
'time', | ||
'title', | ||
'tr', | ||
'track', | ||
'u', | ||
'ul', | ||
'var', | ||
'video', | ||
'wbr' | ||
]; | ||
const doc = new DOMParser().parseFromString(str, 'text/html'); | ||
if (Array.from(doc.body.childNodes).some(node => node.nodeType === 1)) | ||
return tags.some((tag) => str.includes(`<${tag}>`)); | ||
|
||
return false; | ||
} | ||
|
||
function isXML (str: string) { | ||
const doc = new DOMParser().parseFromString(str, 'text/xml'); | ||
const errorNode = doc.querySelector('parsererror'); | ||
return !errorNode; | ||
} | ||
|
||
function isMarkdown (str: string) { | ||
const mdChecks = [ | ||
'# ', | ||
'`', | ||
'- ', | ||
'+ ', | ||
'* ', | ||
'1. ', | ||
'**', | ||
'__', | ||
'~~', | ||
'>> ', | ||
'](http', | ||
'![', | ||
'[ ]', | ||
'[x]' | ||
]; | ||
|
||
return mdChecks.some((tag) => str.includes(tag)); | ||
} | ||
|
||
export function langDetector (str: string) { | ||
if (!str.trim().length) | ||
return 'text'; | ||
|
||
if (isJSON(str)) | ||
return 'json'; | ||
if (isHTML(str)) | ||
return 'html'; | ||
if (isXML(str)) | ||
return 'xml'; | ||
if (isMarkdown(str)) | ||
return 'markdown'; | ||
|
||
return 'text'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters