-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
adds dynamic loading to video-test #698
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,7 +98,7 @@ const NoteCue: React.FC<any> = ({ | |
const visible = | ||
player.activeMetadataTrackCues.includes(cue) && !player.seeking | ||
const startPosition = `${(cue.startTime / duration) * 100}%` | ||
const note = JSON.parse(cue.text) | ||
const note = cue.text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not using JSON, which might be something we test for in the future. We can just not needed for the current approach. |
||
|
||
React.useEffect(() => { | ||
if (visible) { | ||
|
@@ -120,12 +120,7 @@ const NoteCue: React.FC<any> = ({ | |
appendTo="parent" | ||
content={ | ||
<div className="p-2"> | ||
{note.title && ( | ||
<span className="pb-2 font-semibold inline-block"> | ||
{note.title} | ||
</span> | ||
)} | ||
<div className="line-clamp-2">{note.description}</div> | ||
<div className="line-clamp-2">{note}</div> | ||
{/* <ReactMarkdown className="prose prose-sm dark:prose-dark max-w-none"> */} | ||
{/* {note.description}</div> */} | ||
{/* {truncate(note.description, {length: 220, separator: '...'})} */} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {NextApiRequest, NextApiResponse} from 'next' | ||
import mdx from '@mdx-js/mdx' | ||
import VFile from 'vfile' | ||
import visit from 'unist-util-visit' | ||
import fetch from 'isomorphic-fetch' | ||
import toMarkdown from 'mdast-util-to-markdown' | ||
|
||
const loadGithubNotes = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'GET' && req.query.url) { | ||
const test = await loadNotesFromUrl(req.query.url as string) | ||
|
||
res.status(200).json(test) | ||
} else { | ||
res.status(200).end() | ||
} | ||
} | ||
|
||
export default loadGithubNotes | ||
|
||
export async function loadNotesFromUrl(url: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this loads mdx from a url and parses it looking for timestamp components which creates JS objects that are used to make a webvtt. it's currently very fragile! works though |
||
const result = await fetch(url) | ||
|
||
const text = await result.text() | ||
|
||
// @ts-ignore | ||
const file = new VFile(text.trimStart()) | ||
|
||
function extractNotes() { | ||
return function transformer(tree: any, file: any) { | ||
file.data.notes = [] | ||
visit(tree, 'mdxJsxFlowElement', function visitor(node) { | ||
file.data.notes.push(node) | ||
}) | ||
// remove(tree, 'mdxJsxFlowElement') | ||
} | ||
} | ||
|
||
// @ts-ignore | ||
const mdxCompiler = mdx.createCompiler({ | ||
remarkPlugins: [extractNotes], | ||
}) | ||
|
||
return mdxCompiler.process(file).then((file: any) => { | ||
function convertHMS(timeString: string) { | ||
let seconds = 0 | ||
|
||
const arr = timeString.split(':') | ||
|
||
if (arr.length === 3) { | ||
seconds = Number(arr[0]) * 3600 + Number(arr[1]) * 60 + +Number(arr[2]) | ||
} else if (arr.length === 2) { | ||
seconds = Number(arr[0]) * 60 + +Number(arr[1]) | ||
} else { | ||
throw new Error(`can't parse ${timeString}`) | ||
} | ||
return seconds | ||
} | ||
|
||
const notes = file.data.notes.map((note: any) => { | ||
const attributes = note.attributes.reduce((acc: any, attribute: any) => { | ||
return { | ||
...acc, | ||
[attribute.name]: convertHMS(attribute.value), | ||
} | ||
}, {}) | ||
note.type = 'root' | ||
const contents = toMarkdown(note) | ||
return { | ||
text: contents.trim(), | ||
...attributes, | ||
} | ||
}) | ||
|
||
let vtt = `WEBVTT\n\n` | ||
|
||
notes.forEach((note: any) => { | ||
vtt = | ||
vtt + | ||
`note\n${new Date(note.start * 1000) | ||
.toISOString() | ||
.substr(11, 8)}.000 --> ${new Date(note.end * 1000) | ||
.toISOString() | ||
.substr(11, 8)}.000 | ||
${note.text}\n\n` | ||
}) | ||
|
||
return vtt | ||
}) | ||
} |
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.
this is an older version of this library
syntax-tree/unist-util-visit#23
apparently the entire unified ecosystem is making a principled stance on using ESM modules which is incompatible with Next.js (and most of the rest of the world) at this time. This causes a massive headache and debugging nightmare to implement, but it's a principled stance and open source so you can only complain about it so much I guess.
Kind of sucks when you just want to make stuff, particularly since it'd be relatively simple to support commonjs syntax for now.
This issue, in fact, has caused mdx to be forked
I don't like it.
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.
Maintainer of unified and mdx here, sorry you ran into a spot of trouble.
This characterization is unfair on a few fronts:
MDXProvider
mdx-js/mdx#1425 and An alternative “shortcode” (mdxType
alternative) proposal mdx-js/mdx#1385, it has nothing to do with ESMIf you have questions on ESM or MDX in general, feel free to reach out to the MDX community https://github.com/mdx-js/mdx/discussions and/or unified community https://github.com/unifiedjs/unified/discussions
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.
Thanks for this detailed list of corrections.