Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #82 from atom/ld-markdown-line-break
Browse files Browse the repository at this point in the history
Move special Markdown line break whitespace handling to configuration
  • Loading branch information
lee-dohm committed Jul 30, 2015
2 parents 641ce76 + 03a03f0 commit af9edc6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 39 deletions.
7 changes: 7 additions & 0 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module.exports =
scopes:
'.source.jade':
default: false
keepMarkdownLineBreakWhitespace:
type: 'boolean'
default: true
description: '''
Markdown uses two or more spaces at the end of a line to signify a line break. Enable this
option to keep this whitespace, even if other settings would remove it.
'''
ignoreWhitespaceOnCurrentLine:
type: 'boolean'
default: true
Expand Down
6 changes: 3 additions & 3 deletions lib/whitespace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class Whitespace
[whitespace] = match
return if ignoreWhitespaceOnlyLines and whitespace is lineText

if grammarScopeName is 'source.gfm'
# GitHub Flavored Markdown permits two spaces at the end of a line
replace('') unless whitespace is ' ' and whitespace isnt lineText
if grammarScopeName is 'source.gfm' and atom.config.get('whitespace.keepMarkdownLineBreakWhitespace')
# GitHub Flavored Markdown permits two or more spaces at the end of a line
replace('') unless whitespace.length >= 2 and whitespace isnt lineText

This comment has been minimized.

Copy link
@notslang

notslang Feb 15, 2016

Why was this changed to >= 2?

AFAIK, Markdown only needs 2 spaces to trigger a line break, and this change is allowing lines ending in obviously incorrect amounts of whitespace (10/+ spaces) to go uncleaned... And that is rather annoying when working with Markdown files created from PDF conversion (which can have horrible formatting), so if this change isn't necessary, it'd be nice to revert it.

This comment has been minimized.

Copy link
@lee-dohm

lee-dohm Feb 15, 2016

Author Contributor

From the spec:

When you do want to insert a <br /> break tag using Markdown, you end a line with two or more spaces, then type return.

Emphasis mine.

else
replace('')

Expand Down
124 changes: 88 additions & 36 deletions spec/whitespace-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -182,53 +182,105 @@ describe "Whitespace", ->
expect(editor.getText()).toBe "no trailing newline"

describe "GFM whitespace trimming", ->
beforeEach ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", false)
describe 'when keepMarkdownLineBreakWhitespace is true', ->
beforeEach ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", false)
atom.config.set("whitespace.keepMarkdownLineBreakWhitespace", true)

waitsForPromise ->
atom.packages.activatePackage("language-gfm")
waitsForPromise ->
atom.packages.activatePackage("language-gfm")

runs ->
editor.setGrammar(atom.grammars.grammarForScopeName("source.gfm"))
runs ->
editor.setGrammar(atom.grammars.grammarForScopeName("source.gfm"))

it "trims GFM text with a single space", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"
it "trims GFM text with a single space", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "leaves GFM text with double spaces alone", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"
it "leaves GFM text with double spaces alone", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "trims GFM text with a more than two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"
it "leaves GFM text with a more than two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "trims empty lines", ->
editor.insertText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"
it "trims empty lines", ->
editor.insertText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

editor.setText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"
editor.setText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

it "respects 'whitespace.ignoreWhitespaceOnCurrentLine' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", true)
it "respects 'whitespace.ignoreWhitespaceOnCurrentLine' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", true)

editor.insertText "foo \nline break!"
editor.setCursorBufferPosition([0, 4])
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"
editor.insertText "foo \nline break!"
editor.setCursorBufferPosition([0, 4])
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "respects 'whitespace.ignoreWhitespaceOnlyLines' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnlyLines", true)
it "respects 'whitespace.ignoreWhitespaceOnlyLines' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnlyLines", true)

editor.insertText "\t \nline break!"
editor.save()
expect(editor.getText()).toBe "\t \nline break!\n"
editor.insertText "\t \nline break!"
editor.save()
expect(editor.getText()).toBe "\t \nline break!\n"

describe 'when keepMarkdownLineBreakWhitespace is false', ->
beforeEach ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", false)
atom.config.set("whitespace.keepMarkdownLineBreakWhitespace", false)

waitsForPromise ->
atom.packages.activatePackage("language-gfm")

runs ->
editor.setGrammar(atom.grammars.grammarForScopeName("source.gfm"))

it "trims GFM text with a single space", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "trims GFM text with two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "trims GFM text with a more than two spaces", ->
editor.insertText "foo \nline break!"
editor.save()
expect(editor.getText()).toBe "foo\nline break!\n"

it "trims empty lines", ->
editor.insertText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

editor.setText "foo\n "
editor.save()
expect(editor.getText()).toBe "foo\n"

it "respects 'whitespace.ignoreWhitespaceOnCurrentLine' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnCurrentLine", true)

editor.insertText "foo \nline break!"
editor.setCursorBufferPosition([0, 4])
editor.save()
expect(editor.getText()).toBe "foo \nline break!\n"

it "respects 'whitespace.ignoreWhitespaceOnlyLines' setting", ->
atom.config.set("whitespace.ignoreWhitespaceOnlyLines", true)

editor.insertText "\t \nline break!"
editor.save()
expect(editor.getText()).toBe "\t \nline break!\n"

describe "when the editor is split", ->
it "does not throw exceptions when the editor is saved after the split is closed (regression)", ->
Expand Down

0 comments on commit af9edc6

Please sign in to comment.