Skip to content

Commit

Permalink
Merge pull request #3269 from nextcloud/fix/noid/invalid-relative-lin…
Browse files Browse the repository at this point in the history
…k-rich-workspace

Fix: relative link in RichWorkspace
  • Loading branch information
Vinicius Reis authored Oct 27, 2022
2 parents f82828e + cf522aa commit 7fd67d7
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 29 deletions.
46 changes: 43 additions & 3 deletions cypress/e2e/workspace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { randHash } from '../utils/index.js'
const randUser = randHash()

describe('Workspace', function() {
let currentFolder

before(function() {
cy.nextcloudCreateUser(randUser, 'password')
Expand All @@ -33,11 +34,12 @@ describe('Workspace', function() {
cy.login(randUser, 'password').then(() => {
// isolate tests - each happens in its own folder
const retry = cy.state('test').currentRetry()
const folderName = retry

currentFolder = retry
? `${Cypress.currentTest.title} (${retry})`
: Cypress.currentTest.title
cy.createFolder(folderName)
cy.visit(`apps/files?dir=/${encodeURIComponent(folderName)}`)
cy.createFolder(currentFolder)
cy.visit(`apps/files?dir=/${encodeURIComponent(currentFolder)}`)
})
})

Expand Down Expand Up @@ -142,6 +144,44 @@ describe('Workspace', function() {
.contains('😀')
})

it.only('relative folder links', () => {
cy.createFolder(`${currentFolder}/sub-folder`)
cy.createFolder(`${currentFolder}/sub-folder/alpha`)

cy.uploadFile('test.md', 'text/markdown', `${currentFolder}/sub-folder/alpha/test.md`)

cy.openWorkspace()
.type('link me')
.type('{selectall}')

cy.getSubmenuEntry('insert-link', 'insert-link-file')
.click()

cy.get('#picker-filestable tr[data-entryname="sub-folder"]').click()
cy.get('#picker-filestable tr[data-entryname="alpha"]').click()
cy.get('#picker-filestable tr[data-entryname="test.md"]').click()
cy.get('.oc-dialog > .oc-dialog-buttonrow button').click()

cy.getEditor()
.find('a')
.should('have.attr', 'href')
.and('contains', `dir=/${currentFolder}/sub-folder/alpha`)
.and('contains', '#relPath=sub-folder/alpha/test.md')

cy.getEditor()
.find('a').click()

cy.getModal()
.find('.modal-header')
.contains('test.md')

cy.getModal()
.getEditor()
.contains('Hello world')

cy.getModal().find('button.header-close').click()
})

describe('callouts', () => {
const types = ['info', 'warn', 'error', 'success']

Expand Down
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files-modal.js

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

2 changes: 1 addition & 1 deletion js/files-modal.js.map

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

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/EditorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ const loadSyntaxHighlight = async (language) => {
}
}

const createEditor = ({ content, onCreate, onUpdate, extensions, enableRichEditing, session }) => {
const createEditor = ({ content, onCreate, onUpdate, extensions, enableRichEditing, session, relativePath }) => {
let richEditingExtensions = []
if (enableRichEditing) {
richEditingExtensions = [
Markdown,
RichText.configure({
relativePath,
extensions: [
EditableTable,
Mention.configure({
Expand Down
1 change: 1 addition & 0 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ export default {
(this.isRichEditor ? Promise.resolve() : loadSyntaxHighlight(language))
.then(() => {
this.$editor = createEditor({
relativePath: this.relativePath,
session: this.currentSession,
content,
onCreate: ({ editor }) => {
Expand Down
1 change: 1 addition & 0 deletions src/extensions/RichText.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default Extension.create({
...this.options.link,
openOnClick: true,
validate: href => /^https?:\/\//.test(href),
relativePath: this.options.relativePath,
}))
}
const additionalExtensionNames = this.options.extensions.map(e => e.name)
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const basedir = function(file) {
: file.slice(0, end + 1) // basedir('/toplevel') should return '/'
}

const domHref = function(node) {
const domHref = function(node, relativePath) {
const ref = node.attrs.href
if (!ref) {
return ref
Expand All @@ -61,10 +61,11 @@ const domHref = function(node) {
if (ref.startsWith('#')) {
return ref
}

const match = ref.match(/^([^?]*)\?fileId=(\d+)/)
if (match) {
const [, relPath, id] = match
const currentDir = basedir(OCA.Viewer.file)
const currentDir = basedir(relativePath || OCA.Viewer.file)
const dir = absolutePath(currentDir, basedir(relPath))
if (relPath.length > 1 && relPath.endsWith('/')) {
// is directory
Expand Down
15 changes: 10 additions & 5 deletions src/marks/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Link = TipTapLink.extend({
return {
...this.parent?.(),
onClick: openLink,
relativePath: null,
}
},

Expand All @@ -56,11 +57,15 @@ const Link = TipTapLink.extend({
},
],

renderHTML: ({ mark, HTMLAttributes }) => ['a', {
...mark.attrs,
href: domHref(mark),
rel: 'noopener noreferrer nofollow',
}, 0],
renderHTML(options) {
const { mark } = options

return ['a', {
...mark.attrs,
href: domHref(mark, this.options.relativePath),
rel: 'noopener noreferrer nofollow',
}, 0]
},

addProseMirrorPlugins() {
const plugins = this.parent()
Expand Down

0 comments on commit 7fd67d7

Please sign in to comment.