Skip to content

Commit

Permalink
fix: fields content language detection not working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Jul 9, 2022
1 parent 7537dff commit a91fa8f
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 29 deletions.
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -119,7 +119,6 @@
"@faker-js/faker": "~6.1.2",
"@mdi/font": "~6.9.96",
"@turf/helpers": "~6.5.0",
"@vscode/vscode-languagedetection": "~1.0.21",
"@vueuse/core": "~8.7.5",
"ace-builds": "~1.4.13",
"better-sqlite3": "~7.5.1",
Expand Down
182 changes: 182 additions & 0 deletions src/common/libs/langDetector.ts
@@ -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';
}
28 changes: 14 additions & 14 deletions src/renderer/components/WorkspaceTabQueryTableRow.vue
Expand Up @@ -194,9 +194,9 @@
import { computed, onBeforeUnmount, Prop, ref, Ref, watch, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import * as moment from 'moment';
import { ModelOperations } from '@vscode/vscode-languagedetection';
import { mimeFromHex } from 'common/libs/mimeFromHex';
import { formatBytes } from 'common/libs/formatBytes';
import { langDetector } from 'common/libs/langDetector';
import { bufferToBase64 } from 'common/libs/bufferToBase64';
import hexToBinary, { HexChar } from 'common/libs/hexToBinary';
import {
Expand Down Expand Up @@ -604,19 +604,19 @@ watch(() => props.fields, () => {
});
watch(isTextareaEditor, (val) => {
if (val) {
const modelOperations = new ModelOperations();
(async () => {
const detected = await modelOperations.runModel(editingContent.value);
const filteredLanguages = detected.filter(dLang =>
availableLanguages.value.some(aLang => aLang.id === dLang.languageId) &&
dLang.confidence > 0.1
);
if (filteredLanguages.length)
editorMode.value = availableLanguages.value.find(lang => lang.id === filteredLanguages[0].languageId).slug;
})();
}
if (val)
editorMode.value = langDetector(editingContent.value);
// const modelOperations = new ModelOperations();
// (async () => {
// const detected = await modelOperations.runModel(editingContent.value);
// const filteredLanguages = detected.filter(dLang =>
// availableLanguages.value.some(aLang => aLang.id === dLang.languageId) &&
// dLang.confidence > 0.1
// );
// if (filteredLanguages.length)
// editorMode.value = availableLanguages.value.find(lang => lang.id === filteredLanguages[0].languageId).slug;
// })();
});
watch(() => props.selected, (isSelected) => {
Expand Down

0 comments on commit a91fa8f

Please sign in to comment.