Skip to content

Commit

Permalink
Merge branch 'arc53:main' into Srayash/Languages
Browse files Browse the repository at this point in the history
  • Loading branch information
Srayash authored Dec 11, 2024
2 parents 96c5726 + 3a60c31 commit acadd6b
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 201 deletions.
4 changes: 2 additions & 2 deletions application/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
anthropic==0.34.2
anthropic==0.40.0
boto3==1.34.153
beautifulsoup4==4.12.3
celery==5.3.6
Expand Down Expand Up @@ -43,7 +43,7 @@ multidict==6.1.0
mypy-extensions==1.0.0
networkx==3.3
numpy==1.26.4
openai==1.46.1
openai==1.55.3
openapi-schema-validator==0.6.2
openapi-spec-validator==0.6.0
openapi3-parser==1.1.18
Expand Down
61 changes: 34 additions & 27 deletions extensions/react-widget/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react'
import styled, { keyframes, createGlobalStyle, ThemeProvider } from 'styled-components';
import styled, { ThemeProvider } from 'styled-components';
import { WidgetCore } from './DocsGPTWidget';
import { SearchBarProps } from '@/types';
import { getSearchResults } from '../requests/searchAPI'
import { Result } from '@/types';
import MarkdownIt from 'markdown-it';
import DOMPurify from 'dompurify';
import { getOS } from '../utils/helper'
import { getOS, preprocessSearchResultsToHTML } from '../utils/helper'
const themes = {
dark: {
bg: '#000',
Expand Down Expand Up @@ -116,7 +115,7 @@ const ResultWrapper = styled.div`
const Markdown = styled.div`
line-height:20px;
font-size: 12px;
word-break: break-all;
white-space: pre-wrap;
pre {
padding: 8px;
width: 90%;
Expand Down Expand Up @@ -147,17 +146,18 @@ word-break: break-all;
code:not(pre code) {
border-radius: 6px;
padding: 4px 4px;
font-size: 12px;
display: inline-block;
padding: 2px 2px;
margin: 2px;
font-size: 10px;
display: inline;
background-color: #646464;
color: #fff ;
}
img{
max-width: 50%;
}
code {
white-space: pre-wrap ;
overflow-wrap: break-word;
word-break: break-all;
overflow-x: auto;
}
a{
color: #007ee6;
Expand Down Expand Up @@ -291,6 +291,8 @@ export const SearchBar = ({
}, 500);

return () => {
console.log(results);

abortController.abort();
clearTimeout(debounceTimeout.current ?? undefined);
};
Expand Down Expand Up @@ -341,22 +343,27 @@ export const SearchBar = ({
(results.length > 0 ?
results.map((res, key) => {
const containsSource = res.source !== 'local';
return (
<ResultWrapper
key={key}
onClick={() => {
if (!containsSource) return;
window.open(res.source, '_blank', 'noopener, noreferrer')
}}
className={containsSource ? "contains-source" : ""}>
<Title>{res.title}</Title>
<Content>
<Markdown
dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(md.render((res.text).substring(0, 256) + "...")) }}
/>
</Content>
</ResultWrapper>
)
const filteredResults = preprocessSearchResultsToHTML(res.text,input)
if (filteredResults)
return (
<ResultWrapper
key={key}
onClick={() => {
if (!containsSource) return;
window.open(res.source, '_blank', 'noopener, noreferrer')
}}
className={containsSource ? "contains-source" : ""}>
<Title>{res.title}</Title>
<Content>
<Markdown
dangerouslySetInnerHTML={{ __html: filteredResults }}
/>
</Content>
</ResultWrapper>
)
else {
setResults((prevItems) => prevItems.filter((_, index) => index !== key));
}
})
:
<NoResults>No results</NoResults>
Expand Down
110 changes: 85 additions & 25 deletions extensions/react-widget/src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,87 @@
import MarkdownIt from "markdown-it";
import DOMPurify from "dompurify";
export const getOS = () => {
const platform = window.navigator.platform;
const userAgent = window.navigator.userAgent || window.navigator.vendor;

if (/Mac/i.test(platform)) {
return 'mac';
const platform = window.navigator.platform;
const userAgent = window.navigator.userAgent || window.navigator.vendor;

if (/Mac/i.test(platform)) {
return 'mac';
}

if (/Win/i.test(platform)) {
return 'win';
}

if (/Linux/i.test(platform) && !/Android/i.test(userAgent)) {
return 'linux';
}

if (/Android/i.test(userAgent)) {
return 'android';
}

if (/iPhone|iPad|iPod/i.test(userAgent)) {
return 'ios';
}

return 'other';
};

export const preprocessSearchResultsToHTML = (text: string, keyword: string) => {
const md = new MarkdownIt();
const htmlString = md.render(text);

// Container for processed HTML
const filteredResults = document.createElement("div");
filteredResults.innerHTML = htmlString;

if (!processNode(filteredResults, keyword.trim())) return null;

return filteredResults.innerHTML.trim() ? filteredResults.outerHTML : null;
};



// Recursive function to process nodes
const processNode = (node: Node, keyword: string): boolean => {

const keywordRegex = new RegExp(`(${keyword})`, "gi");
if (node.nodeType === Node.TEXT_NODE) {
const textContent = node.textContent || "";

if (textContent.toLowerCase().includes(keyword.toLowerCase())) {
const highlightedHTML = textContent.replace(
keywordRegex,
`<mark>$1</mark>`
);
const tempContainer = document.createElement("div");
tempContainer.innerHTML = highlightedHTML;

// Replace the text node with highlighted content
while (tempContainer.firstChild) {
node.parentNode?.insertBefore(tempContainer.firstChild, node);
}
node.parentNode?.removeChild(node);

return true;
}
if (/Win/i.test(platform)) {
return 'win';
}

if (/Linux/i.test(platform) && !/Android/i.test(userAgent)) {
return 'linux';
}

if (/Android/i.test(userAgent)) {
return 'android';
}

if (/iPhone|iPad|iPod/i.test(userAgent)) {
return 'ios';
}

return 'other';
};

return false;
} else if (node.nodeType === Node.ELEMENT_NODE) {

const children = Array.from(node.childNodes);
let hasKeyword = false;

children.forEach((child) => {
if (!processNode(child, keyword)) {
node.removeChild(child);
} else {
hasKeyword = true;
}
});

return hasKeyword;
}

return false;
};
2 changes: 1 addition & 1 deletion extensions/react-widget/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/* Linting */
"strict": true,
"noUnusedLocals": false,
"noUnusedParameters": true,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true,
/* The "typeRoots" configuration specifies the locations where
TypeScript looks for type definitions (.d.ts files) to
Expand Down
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover" />
<meta name="apple-mobile-web-app-capable" content="yes">
<title>DocsGPT 🦖</title>
<title>DocsGPT</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
</head>

Expand Down
51 changes: 47 additions & 4 deletions frontend/package-lock.json

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

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"react-chartjs-2": "^5.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.3.1",
"react-helmet": "^6.1.0",
"react-dropzone": "^14.3.5",
"react-i18next": "^15.0.2",
"react-markdown": "^9.0.1",
Expand All @@ -41,6 +42,7 @@
"devDependencies": {
"@types/react": "^18.0.27",
"@types/react-dom": "^18.3.0",
"@types/react-helmet": "^6.1.11",
"@types/react-syntax-highlighter": "^15.5.13",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.62.0",
Expand Down
Binary file added frontend/src/assets/user.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion frontend/src/components/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function Avatar({
size,
className,
}: {
avatar: string | ReactNode;
avatar: ReactNode;
size?: 'SMALL' | 'MEDIUM' | 'LARGE';
className: string;
}) {
Expand Down
Loading

0 comments on commit acadd6b

Please sign in to comment.