Repository navigation
fix(files): support arbitrary uploads and in-document links #8807
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
Merged
+366
−153
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
50 changes: 23 additions & 27 deletions
50
...kspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/heading-anchors.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,36 +1,32 @@ | ||
| import type { Node as ProseMirrorNode } from '@tiptap/pm/model' | ||
| import type { EditorView } from '@tiptap/pm/view' | ||
| import GithubSlugger from 'github-slugger' | ||
|
|
||
| /** | ||
| * Slugify heading text GitHub-style (lowercase, drop punctuation, collapse whitespace to hyphens) so | ||
| * that `[label](#slug)` fragment links — written against how GitHub renders the same markdown — | ||
| * resolve to the matching heading. Mirrors what `rehype-slug` produced in the old preview. | ||
| */ | ||
| export function slugifyHeading(text: string): string { | ||
| return text | ||
| .toLowerCase() | ||
| .trim() | ||
| .replace(/[^\w\s-]/g, '') | ||
| .replace(/\s+/g, '-') | ||
| .replace(/-+/g, '-') | ||
| } | ||
|
|
||
| /** | ||
| * The document position of the heading a `#slug` fragment link targets, or -1 if none matches. | ||
| * Computed on demand (at click time) rather than maintained as per-keystroke decorations. Duplicate | ||
| * slugs are disambiguated GitHub-style: `intro`, `intro-1`, `intro-2`, … | ||
| */ | ||
| export function findHeadingPos(doc: ProseMirrorNode, slug: string): number { | ||
| const seen = new Map<string, number>() | ||
| /** Resolves GitHub heading fragments, including Unicode and collisions with existing suffixes. */ | ||
| function findHeadingPos(doc: ProseMirrorNode, slug: string): number { | ||
| const slugger = new GithubSlugger() | ||
| let found = -1 | ||
| doc.descendants((node, pos) => { | ||
| if (found >= 0) return false | ||
| if (node.type.name !== 'heading') return true | ||
| const base = slugifyHeading(node.textContent) | ||
| if (!base) return true | ||
| const n = seen.get(base) ?? 0 | ||
| seen.set(base, n + 1) | ||
| if ((n === 0 ? base : `${base}-${n}`) === slug) found = pos | ||
| return found < 0 | ||
| if (slugger.slug(node.textContent) === slug) found = pos | ||
| return false | ||
| }) | ||
| return found | ||
| } | ||
|
|
||
| /** Scrolls within this editor without navigating the page or changing the document selection. */ | ||
| export function scrollToHeading(view: EditorView, fragment: string): boolean { | ||
| let slug: string | ||
| try { | ||
| slug = decodeURIComponent(fragment.slice(1)) | ||
| } catch { | ||
| return false | ||
| } | ||
| const pos = slug ? findHeadingPos(view.state.doc, slug) : 0 | ||
| if (pos < 0) return false | ||
| const target = slug ? view.nodeDOM(pos) : view.dom | ||
| if (!(target instanceof HTMLElement)) return false | ||
| target.scrollIntoView({ behavior: 'smooth', block: 'start' }) | ||
| return true | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.