-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ai): Save file to VectorStore in HTML format #9462
Merged
mergify
merged 13 commits into
master
from
feat/158281-save-to-vectorstore-in-html-format
Dec 23, 2024
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1e0c75d
Install rehype-stringify
miya 95eb6ec
Install rehype-meta
miya f87304f
Modified markdown to html conversion logic
miya 1b05ae3
sanitize-markdown.ts -> convert-markdown-to-html.ts
miya 20ecef0
sanitize -> sanitizeMarkdown
miya 23c9dbd
clean code
miya 621907a
rm debug log
miya 10d526c
Merge branch 'master' into feat/158281-save-to-vectorstore-in-html-fo…
miya c7068fb
Simplify cvertMarkdownToHtml argument types
miya 3e17890
Fix logic error in page revision body length check
miya bac5032
add comment
miya 1bebb75
Refactor uploadFile and convertMarkdownToHtml to use simplified param…
miya 35618a6
Simplify parameter type definition in convertMarkdownToHtml function
miya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
90 changes: 90 additions & 0 deletions
90
apps/app/src/features/openai/server/utils/convert-markdown-to-html.ts
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,90 @@ | ||
import { dynamicImport } from '@cspell/dynamic-import'; | ||
import type { IPagePopulatedToShowRevision } from '@growi/core/dist/interfaces'; | ||
import type { Root, Code } from 'mdast'; | ||
import type * as RehypeMeta from 'rehype-meta'; | ||
import type * as RehypeStringify from 'rehype-stringify'; | ||
import type * as RemarkParse from 'remark-parse'; | ||
import type * as RemarkRehype from 'remark-rehype'; | ||
import type * as Unified from 'unified'; | ||
import type * as UnistUtilVisit from 'unist-util-visit'; | ||
|
||
interface ModuleCache { | ||
unified?: typeof Unified.unified; | ||
visit?: typeof UnistUtilVisit.visit; | ||
remarkParse?: typeof RemarkParse.default; | ||
remarkRehype?: typeof RemarkRehype.default; | ||
rehypeMeta?: typeof RehypeMeta.default; | ||
rehypeStringify?: typeof RehypeStringify.default; | ||
} | ||
|
||
let moduleCache: ModuleCache = {}; | ||
|
||
const initializeModules = async(): Promise<void> => { | ||
if (moduleCache.unified != null | ||
&& moduleCache.visit != null | ||
&& moduleCache.remarkParse != null | ||
&& moduleCache.remarkRehype != null | ||
&& moduleCache.rehypeMeta != null | ||
&& moduleCache.rehypeStringify != null | ||
) { | ||
return; | ||
} | ||
|
||
const [ | ||
{ unified }, | ||
{ visit }, | ||
{ default: remarkParse }, | ||
{ default: remarkRehype }, | ||
{ default: rehypeMeta }, | ||
{ default: rehypeStringify }, | ||
] = await Promise.all([ | ||
dynamicImport<typeof Unified>('unified', __dirname), | ||
dynamicImport<typeof UnistUtilVisit>('unist-util-visit', __dirname), | ||
dynamicImport<typeof RemarkParse>('remark-parse', __dirname), | ||
dynamicImport<typeof RemarkRehype>('remark-rehype', __dirname), | ||
dynamicImport<typeof RehypeMeta>('rehype-meta', __dirname), | ||
dynamicImport<typeof RehypeStringify>('rehype-stringify', __dirname), | ||
]); | ||
|
||
moduleCache = { | ||
unified, | ||
visit, | ||
remarkParse, | ||
remarkRehype, | ||
rehypeMeta, | ||
rehypeStringify, | ||
}; | ||
}; | ||
|
||
export const convertMarkdownToHtml = async(page: IPagePopulatedToShowRevision): Promise<string> => { | ||
await initializeModules(); | ||
|
||
const { | ||
unified, visit, remarkParse, remarkRehype, rehypeMeta, rehypeStringify, | ||
} = moduleCache; | ||
|
||
if (unified == null || visit == null || remarkParse == null || remarkRehype == null || rehypeMeta == null || rehypeStringify == null) { | ||
throw new Error('Failed to initialize required modules'); | ||
} | ||
|
||
const sanitizeMarkdown = () => { | ||
return (tree: Root) => { | ||
visit(tree, 'code', (node: Code) => { | ||
if (node.lang === 'drawio') { | ||
node.value = '<!-- drawio content replaced -->'; | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
const processor = unified() | ||
.use(remarkParse) | ||
.use(sanitizeMarkdown) | ||
.use(remarkRehype) | ||
.use(rehypeMeta, { | ||
title: page.path, | ||
}) | ||
.use(rehypeStringify); | ||
|
||
return processor.processSync(page.revision?.body).toString(); | ||
}; |
65 changes: 0 additions & 65 deletions
65
apps/app/src/features/openai/server/utils/sanitize-markdown.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undefined 対策をして
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正しました: 1bebb75