GithubHelp home page GithubHelp logo

rubocop-md's Introduction

Gem Version Test

Rubocop Markdown

Run Rubocop against your Markdown files to make sure that code examples follow style guidelines and have valid syntax.

Features

  • Analyzes code blocks within Markdown files
  • Shows correct line numbers in output
  • Preserves specified language (i.e., do not try to analyze "```sh")
  • Supports autocorrect πŸ“

This project was developed to keep test-prof guides consistent with Ruby style guide.

Installation

Add this line to your application's Gemfile:

gem 'rubocop-md'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rubocop-md

Usage

Command line

Just require rubocop-md in your command:

rubocop -r "rubocop-md" ./lib

Autocorrect works too:

rubocop -r "rubocop-md" -a ./lib

Configuration

Code in the documentation does not make sense to be checked for some style guidelines (eg Style/FrozenStringLiteralComment).

We described such rules in the default config, but if you use the same settings in your project’s .rubocop.yml file, RoboCop will override them.

Fortunately, RuboCop supports directory-level configuration and we can do the next trick.

At first, add rubocop-md to your main .rubocop.yml:

# .rubocop.yml

require:
  - "rubocop-md"

Notice: additional options

# .rubocop.yml

Markdown:
  # Whether to run RuboCop against non-valid snippets
  WarnInvalid: true
  # Whether to lint codeblocks without code attributes
  Autodetect: true

Secondly, create empty .rubocop.yml in your docs directory.

β”œβ”€β”€ docs
β”‚   β”œβ”€β”€ .rubocop.yml
β”‚   β”œβ”€β”€ doc1.md
β”‚   β”œβ”€β”€ doc2.md
β”‚   └── doc3.md
β”œβ”€β”€ lib
β”œβ”€β”€ .rubocop.yml # main
└── ...

Third, just run

$ rubocop

Also you can add special rules in the second .rubocop.yml

# rubocop.yml in docs folder

Metrics/LineLength:
  Max: 100

Lint/Void:
  Exclude:
    - '*.md'

But if I want to use inline disabling some directive like in classic RuboCop?

You can use this tricks

# my_post.md

... some markdown ...

<span style="display:none;"># rubocop:disable all</span>

```ruby
def my_poor_method(foo)
  [:a, :b, :c] + ["#{foo}".to_sym]
end
``` end of snippet

<span style="display:none;"># rubocop:enable all</span>

... continuation of article ...

How it works?

  • Preprocess Markdown source into Ruby source preserving line numbers
  • Let RuboCop do its job
  • Restore Markdown from preprocessed Ruby if it has been autocorrected

Limitations

  • RuboCop cache is disabled for Markdown files (because cache knows nothing about preprocessing)
  • Uses naive Regexp-based approach to extract code blocks from Markdown, support only backticks-style code blocks*
  • No language detection included; if you do not specify language for your code blocks, you'd better turn WarnInvalid off (see above)

* It should be easy to integrate a real parser (e.g. Kramdown) and handle all possible syntax. Feel free to open an issue or pull request!

Development

After checking out the repo, run bundle install to install dependencies. Then, run rake test to run the tests.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rubocop-hq/rubocop-md.

License

The gem is available as open source under the terms of the MIT License.

rubocop-md's People

Contributors

palkan avatar dominicsayers avatar koic avatar ssnickolay avatar ybiquitous avatar sfeuga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.