Skip to content

Commit

Permalink
Merge pull request #314 from fedwiki/handle-bad-slugs
Browse files Browse the repository at this point in the history
Handle internal links that start/end with spaces
  • Loading branch information
paul90 authored Feb 27, 2024
2 parents 8fbad01 + 3ebe76b commit 70bd988
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions client/style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pre.error {
a {
text-decoration: none; }

/* highlight links that have title starting or ending with a space. */
body:has(footer span[style=""]) a.spaced {
text-decoration: wavy underline crimson;
}

a img {
border: 0; }

Expand Down
7 changes: 5 additions & 2 deletions lib/future.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ refresh = require './refresh'
emit = ($item, item) ->

$item.append """#{item.text}"""

$item.append """<br><br><button class="create">create</button> new blank page"""
proposedSlug = $item.parents('.page:first')[0].id
if wiki.asSlug(item.title) isnt proposedSlug
$item.append "<p style='font-weight: 500;'>Page titles with leading/trailing spaces can not be used to create a new page.</p>"
else
$item.append """<br><br><button class="create">create</button> new blank page"""

if transport = item.create?.source?.transport
$item.append """<br><button class="transport" data-slug=#{item.slug}>create</button> transport from #{transport}"""
Expand Down
3 changes: 2 additions & 1 deletion lib/resolve.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ resolve.resolveLinks = (string, sanitize=escape) ->

internal = (match, name) ->
slug = asSlug name
styling = if name is name.trim() then 'internal' else 'internal spaced'
if slug.length
stash """<a class="internal" href="/#{slug}.html" data-page-name="#{slug}" title="#{resolve.resolutionContext.join(' => ')}">#{escape name}</a>"""
stash """<a class="#{styling}" href="/#{slug}.html" data-page-name="#{slug}" title="#{resolve.resolutionContext.join(' => ')}">#{escape name}</a>"""
else
match

Expand Down
9 changes: 9 additions & 0 deletions test/wiki.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ describe 'wiki', ->
expect(s).to.contain 'href="/world.html"'
it 'should have data-page-name', ->
expect(s).to.contain 'data-page-name="world"'

describe 'internal links with space', ->
s = wiki.resolveLinks "hello [[ world]]"
it 'should be class spaced', ->
expect(s).to.contain 'class="internal spaced"'
it 'should relative reference html', ->
expect(s).to.contain 'href="/-world.html"'
it 'should have data-page-name', ->
expect(s).to.contain 'data-page-name="-world"'

describe 'external links', ->
s = wiki.resolveLinks "hello [http://world.com?foo=1&bar=2 world]"
Expand Down

0 comments on commit 70bd988

Please sign in to comment.