├── .github
├── CODEOWNERS
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── config.yml
├── dependabot.yml
├── funding.yml
├── no-response.yml
├── release-drafter.yml
├── settings.yml
├── stale.yml
└── workflows
│ ├── ci.yml
│ └── codeql-analysis.yml
├── .gitignore
├── .rspec
├── .rubocop.yml
├── .rubocop_todo.yml
├── Gemfile
├── LICENSE.md
├── README.md
├── docs
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
└── SECURITY.md
├── jekyll-relative-links.gemspec
├── lib
├── jekyll-relative-links.rb
└── jekyll-relative-links
│ ├── context.rb
│ ├── filter.rb
│ ├── generator.rb
│ └── version.rb
├── script
├── bootstrap
└── cibuild
└── spec
├── fixtures
├── excerpt-in-frontmatter
│ ├── _layouts
│ │ └── default.html
│ ├── another-page.md
│ └── page-with-excerpt.md
└── site
│ ├── _items
│ ├── some-item.md
│ ├── some-subdir
│ │ └── another-item.md
│ └── static-file.md
│ ├── _posts
│ └── 2016-01-01-test.md
│ ├── another-page.md
│ ├── html-page.html
│ ├── jekyll-logo.png
│ ├── page with symbols?.md
│ ├── page-with-permalink.md
│ ├── page.md
│ └── subdir
│ ├── _posts
│ └── 2016-01-01-test.md
│ ├── another-subdir-page.md
│ └── page.md
├── jekyll-relative-links
└── generator_excerpt_frontmatter_spec.rb
├── jekyll_relative_links
├── context_spec.rb
├── filter_spec.rb
└── generator_spec.rb
└── spec_helper.rb
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | # Require @benbalter's :+1: for changes to the .github repo-config files
2 | # mainly due to https://github.com/probot/settings privilege escalation
3 | .github/* @benbalter
4 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 |
5 | ---
6 |
7 | ### Describe the bug
8 |
9 | A clear and concise description of what the bug is.
10 |
11 | ### Steps to reproduce the behavior
12 |
13 | 1. Go to '...'
14 | 2. Click on '....'
15 | 3. Scroll down to '....'
16 | 4. See error
17 |
18 | ### Expected behavior
19 |
20 | A clear and concise description of what you expected to happen.
21 |
22 | ### Screenshots
23 |
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | ### Additional context
27 |
28 | Add any other context about the problem here.
29 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 |
5 | ---
6 |
7 | ### Is your feature request related to a problem? Please describe the problem you're trying to solve.
8 |
9 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
10 |
11 | ### Describe the solution you'd like
12 |
13 | A clear and concise description of what you want to happen.
14 |
15 | ### Describe alternatives you've considered
16 |
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | ### Additional context
20 |
21 | Add any other context or screenshots about the feature request here.
22 |
--------------------------------------------------------------------------------
/.github/config.yml:
--------------------------------------------------------------------------------
1 | # Behaviorbot config. See https://github.com/behaviorbot/ for more information.
2 | # Note: Please Don't edit this file directly.
3 | # Edit https://github.com/benbalter/shared-community-files instead.
4 |
5 | # Configuration for update-docs - https://github.com/behaviorbot/update-docs
6 | updateDocsComment: "Thanks for the pull request! If you are making any changes to the user-facing functionality, please be sure to update the documentation in the `README` or `docs/` folder alongside your change. :heart:"
7 |
8 | # Configuration for request-info - https://github.com/behaviorbot/request-info
9 | requestInfoReplyComment: Thanks for this. Do you mind providing a bit more information about what problem you're trying to solve?
10 | requestInfoLabelToAdd: more-information-needed
11 |
12 | # Configuration for new-issue-welcome - https://github.com/behaviorbot/new-issue-welcome
13 | #newIssueWelcomeComment: >
14 | # Welcome!
15 |
16 | # Configuration for new-pr-welcome - https://github.com/behaviorbot/new-pr-welcome
17 | newPRWelcomeComment: Welcome! Congrats on your first pull request to Jekyll Relative Links. If you haven't already, please be sure to check out [the contributing guidelines](https://github.com/benbalter/jekyll-relative-links/blob/master/docs/CONTRIBUTING.md).
18 |
19 | # Configuration for first-pr-merge - https://github.com/behaviorbot/first-pr-merge
20 | firstPRMergeComment: "Congrats on getting your first pull request to Jekyll Relative Links merged! Without amazing humans like you submitting pull requests, we couldn’t run this project. You rock! :tada:
If you're interested in tackling another bug or feature, take a look at [the open issues](https://github.com/benbalter/jekyll-relative-links/issues), especially those [labeled `help wanted`](https://github.com/benbalter/jekyll-relative-links/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22)."
21 |
22 | # Bug workaround
23 | contact_links: []
24 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: bundler
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | time: "10:00"
8 | timezone: US/Eastern
9 | open-pull-requests-limit: 99
10 |
--------------------------------------------------------------------------------
/.github/funding.yml:
--------------------------------------------------------------------------------
1 | patreon: benbalter
2 |
--------------------------------------------------------------------------------
/.github/no-response.yml:
--------------------------------------------------------------------------------
1 | # Configuration for probot-no-response - https://github.com/probot/no-response
2 | # Note: Please Don't edit this file directly.
3 | # Edit https://github.com/benbalter/shared-community-files instead.
4 |
5 | # Number of days of inactivity before an Issue is closed for lack of response
6 | daysUntilClose: 14
7 | # Label requiring a response
8 | responseRequiredLabel: more-information-needed
9 | # Comment to post when closing an Issue for lack of response. Set to `false` to disable
10 | closeComment: >
11 | This issue has been automatically closed because there has been no response
12 | to our request for more information from the original author. With only the
13 | information that is currently in the issue, we don't have enough information
14 | to take action. Please reach out if you have or find the answers we need so
15 | that we can investigate further.
16 |
--------------------------------------------------------------------------------
/.github/release-drafter.yml:
--------------------------------------------------------------------------------
1 | template: |
2 | ## What's Changed
3 |
4 | $CHANGES
5 |
--------------------------------------------------------------------------------
/.github/settings.yml:
--------------------------------------------------------------------------------
1 | # Repository settings set via https://github.com/probot/settings
2 | # Note: Please Don't edit this file directly.
3 | # Edit https://github.com/benbalter/shared-community-files instead.
4 |
5 | repository:
6 | has_issues: true
7 | has_wiki: false
8 | has_projects: false
9 | has_downloads: false
10 |
11 | labels:
12 | - name: help wanted
13 | oldname: help-wanted
14 | color: 0e8a16
15 | - name: more-information-needed
16 | color: d93f0b
17 | - name: bug
18 | color: b60205
19 | - name: feature
20 | color: 1d76db
21 | - name: good first issue
22 | color: "5319e7"
23 |
24 | # Not currently implemented by probot/settings, but manually implemented in script/deploy
25 | branch_protection:
26 | restrictions: null
27 | enforce_admins: false
28 | required_status_checks:
29 | strict: true
30 | contexts:
31 | - "continuous-integration/travis-ci"
32 | required_pull_request_reviews:
33 | require_code_owner_reviews: true
34 |
--------------------------------------------------------------------------------
/.github/stale.yml:
--------------------------------------------------------------------------------
1 | # Configuration for probot-stale - https://github.com/probot/stale
2 | # Note: Please Don't edit this file directly.
3 | # Edit https://github.com/benbalter/shared-community-files instead.
4 |
5 | # Number of days of inactivity before an Issue or Pull Request becomes stale
6 | daysUntilStale: 60
7 |
8 | # Number of days of inactivity before a stale Issue or Pull Request is closed
9 | daysUntilClose: 7
10 |
11 | # Issues or Pull Requests with these labels will never be considered stale
12 | exemptLabels:
13 | - pinned
14 | - security
15 |
16 | # Label to use when marking as stale
17 | staleLabel: wontfix
18 |
19 | # Comment to post when marking as stale. Set to `false` to disable
20 | markComment: >
21 | This issue has been automatically marked as stale because it has not had
22 | recent activity. It will be closed if no further activity occurs. Thank you
23 | for your contributions.
24 |
25 | # Comment to post when closing a stale Issue or Pull Request. Set to `false` to disable
26 | closeComment: false
27 |
28 | # Limit to only `issues` or `pulls`
29 | # only: issues
30 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | jobs:
10 | test:
11 |
12 | runs-on: ubuntu-latest
13 | strategy:
14 | matrix:
15 | ruby-version: ['2.7', '3.0']
16 | jekyll-version: ["~> 3.0", "~> 4.0"]
17 |
18 | steps:
19 | - uses: actions/checkout@v2
20 |
21 | - name: Set up Ruby
22 | uses: ruby/setup-ruby@v1
23 | with:
24 | ruby-version: ${{ matrix.ruby-version }}
25 | bundler-cache: true
26 | env:
27 | JEKYLL_VERSION: ${{ matrix.jekyll-version }}
28 |
29 | - name: Run tests
30 | run: script/cibuild
31 |
--------------------------------------------------------------------------------
/.github/workflows/codeql-analysis.yml:
--------------------------------------------------------------------------------
1 | # For most projects, this workflow file will not need changing; you simply need
2 | # to commit it to your repository.
3 | #
4 | # You may wish to alter this file to override the set of languages analyzed,
5 | # or to provide custom queries or build logic.
6 | #
7 | # ******** NOTE ********
8 | # We have attempted to detect the languages in your repository. Please check
9 | # the `language` matrix defined below to confirm you have the correct set of
10 | # supported CodeQL languages.
11 | #
12 | name: "CodeQL"
13 |
14 | on:
15 | push:
16 | branches: [ main ]
17 | pull_request:
18 | # The branches below must be a subset of the branches above
19 | branches: [ main ]
20 | schedule:
21 | - cron: '29 0 * * 6'
22 |
23 | jobs:
24 | analyze:
25 | name: Analyze
26 | runs-on: ubuntu-latest
27 | permissions:
28 | actions: read
29 | contents: read
30 | security-events: write
31 |
32 | strategy:
33 | fail-fast: false
34 | matrix:
35 | language: [ 'ruby' ]
36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37 | # Learn more about CodeQL language support at https://git.io/codeql-language-support
38 |
39 | steps:
40 | - name: Checkout repository
41 | uses: actions/checkout@v2
42 |
43 | # Initializes the CodeQL tools for scanning.
44 | - name: Initialize CodeQL
45 | uses: github/codeql-action/init@v1
46 | with:
47 | languages: ${{ matrix.language }}
48 | # If you wish to specify custom queries, you can do so here or in a config file.
49 | # By default, queries listed here will override any specified in a config file.
50 | # Prefix the list here with "+" to use these queries and those in the config file.
51 | # queries: ./path/to/local/query, your-org/your-repo/queries@main
52 |
53 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54 | # If this step fails, then you should remove it and run the build manually (see below)
55 | - name: Autobuild
56 | uses: github/codeql-action/autobuild@v1
57 |
58 | # ℹ️ Command-line programs to run using the OS shell.
59 | # 📚 https://git.io/JvXDl
60 |
61 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62 | # and modify them (or add more) to build your code if your project
63 | # uses a compiled language
64 |
65 | #- run: |
66 | # make bootstrap
67 | # make release
68 |
69 | - name: Perform CodeQL Analysis
70 | uses: github/codeql-action/analyze@v1
71 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /_site
2 | /tmp
3 | /spec/examples.txt
4 | vendor/bundle
5 | .bundle
6 | /Gemfile.lock
7 | *.gem
8 | spec/fixtures/site/.jekyll-cache/
--------------------------------------------------------------------------------
/.rspec:
--------------------------------------------------------------------------------
1 | --color
2 | --require spec_helper
3 |
--------------------------------------------------------------------------------
/.rubocop.yml:
--------------------------------------------------------------------------------
1 | inherit_from: .rubocop_todo.yml
2 |
3 | require:
4 | - rubocop-jekyll
5 | - rubocop-performance
6 | - rubocop-rspec
7 |
8 | inherit_gem:
9 | rubocop-jekyll: .rubocop.yml
10 |
11 | AllCops:
12 | Exclude:
13 | - vendor/**/*
14 | NewCops: enable
15 |
16 | Metrics/BlockLength:
17 | Exclude:
18 | - spec/**/*
19 |
20 | RSpec/FilePath:
21 | Exclude:
22 | - spec/jekyll-relative-links/filter_spec.rb
23 | - spec/jekyll-relative-links/generator_excerpt_frontmatter_spec.rb
24 | - spec/jekyll-relative-links/context_spec.rb
25 | - spec/jekyll-relative-links/generator_spec.rb
26 |
--------------------------------------------------------------------------------
/.rubocop_todo.yml:
--------------------------------------------------------------------------------
1 | # This configuration was generated by
2 | # `rubocop --auto-gen-config`
3 | # on 2021-09-16 19:13:45 UTC using RuboCop version 1.18.4.
4 | # The point is for the user to remove these configuration records
5 | # one by one as the offenses are removed from the code base.
6 | # Note that changes in the inspected code, or installation of new
7 | # versions of RuboCop, may require this file to be generated again.
8 |
9 | # Offense count: 1
10 | # Configuration parameters: Include.
11 | # Include: **/*.gemspec
12 | Gemspec/RequiredRubyVersion:
13 | Exclude:
14 | - 'jekyll-relative-links.gemspec'
15 |
16 | # Offense count: 1
17 | # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
18 | Metrics/AbcSize:
19 | Max: 24
20 |
21 | # Offense count: 1
22 | # Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers.
23 | # SupportedStyles: snake_case, normalcase, non_integer
24 | # AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339
25 | Naming/VariableNumber:
26 | Exclude:
27 | - 'spec/jekyll-relative-links/generator_spec.rb'
28 |
29 | # Offense count: 2
30 | # Cop supports --auto-correct.
31 | # Configuration parameters: SafeMultiline.
32 | Performance/DeletePrefix:
33 | Exclude:
34 | - 'lib/jekyll-relative-links/generator.rb'
35 |
36 | # Offense count: 14
37 | # Configuration parameters: Prefixes.
38 | # Prefixes: when, with, without
39 | RSpec/ContextWording:
40 | Exclude:
41 | - 'spec/jekyll-relative-links/generator_spec.rb'
42 |
43 | # Offense count: 2
44 | # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
45 | # Include: **/*_spec*rb*, **/spec/**/*
46 | RSpec/FilePath:
47 | Exclude:
48 | - 'spec/jekyll-relative-links/context_spec.rb'
49 | - 'spec/jekyll-relative-links/generator_spec.rb'
50 |
51 | # Offense count: 4
52 | RSpec/MultipleExpectations:
53 | Max: 2
54 |
55 | # Offense count: 17
56 | # Configuration parameters: AllowSubject.
57 | RSpec/MultipleMemoizedHelpers:
58 | Max: 13
59 |
60 | # Offense count: 10
61 | # Configuration parameters: IgnoreSharedExamples.
62 | RSpec/NamedSubject:
63 | Exclude:
64 | - 'spec/jekyll-relative-links/context_spec.rb'
65 | - 'spec/jekyll-relative-links/generator_spec.rb'
66 |
67 | # Offense count: 6
68 | RSpec/NestedGroups:
69 | Max: 5
70 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | source "https://rubygems.org"
4 |
5 | gemspec
6 |
7 | gem "jekyll", ENV["JEKYLL_VERSION"] if ENV["JEKYLL_VERSION"]
8 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Ben Balter
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Jekyll Relative Links
2 |
3 | [](https://github.com/benbalter/jekyll-relative-links/actions/workflows/ci.yml)
4 |
5 | A Jekyll plugin to convert relative links to Markdown files to their rendered equivalents.
6 |
7 | ## What it does
8 |
9 | Let's say you have a link like this in a Markdown file:
10 |
11 | ```
12 | [foo](bar.md)
13 | ```
14 |
15 | While that would render as a valid link on GitHub.com, it would not be a valid link on Pages. Instead, this plugin converts that link to:
16 |
17 | ```
18 | [foo](bar.html)
19 | ```
20 |
21 | It even work with pages with custom permalinks. If you have `bar.md` with the following:
22 |
23 | ```
24 | ---
25 | permalink: /bar/
26 | ---
27 |
28 | # bar
29 | ```
30 |
31 | Then `[foo](bar.md)` will render as `[foo](/bar/)`.
32 |
33 | The default Jekyll's configuration `permalink: pretty` in the `_config.yaml`
34 | file removes the `.html` extensions from the generated links.
35 |
36 | ## Why
37 |
38 | Because Markdown files rendered by GitHub Pages should behave similar to Markdown files rendered on GitHub.com
39 |
40 | ## Usage
41 |
42 | 1. Add the following to your site's Gemfile:
43 |
44 | ```ruby
45 | gem 'jekyll-relative-links'
46 | ```
47 |
48 | 2. Add the following to your site's config file:
49 |
50 | ```yml
51 | plugins:
52 | - jekyll-relative-links
53 | ```
54 | Note: If you are using a Jekyll version less than 3.5.0, use the `gems` key instead of `plugins`.
55 |
56 | ## Configuration
57 |
58 | You can configure this plugin in `_config.yml` under the `relative_links` key. This is optional and defaults to:
59 |
60 | ```yml
61 | relative_links:
62 | enabled: true
63 | collections: false
64 | ```
65 |
66 | ### Excluding files
67 |
68 | To exclude specific directories and/or files:
69 |
70 | ```yml
71 | relative_links:
72 | exclude:
73 | - directory
74 | - file.md
75 | ```
76 |
77 | ### Processing Collections
78 |
79 | Setting the `collections` option to `true` enables relative links from collection items (including posts).
80 |
81 | Assuming this structure
82 |
83 | ~~~
84 | ├── _my_collection
85 | │ ├── some_doc.md
86 | │ └── some_subdir
87 | │ └── another_doc.md
88 | ├── _config.yml
89 | └── index.md
90 | ~~~
91 |
92 | the following will work:
93 |
94 | File | Link
95 | -|-
96 | `index.md` | `[Some Doc](_my_collection/some_doc.md)`
97 | `index.md` | `[Another Doc](_my_collection/some_subdir/another_doc.md)`
98 | `_my_collection/some_doc.md` | `[Index](../index.md)`
99 | `_my_collection/some_doc.md` | `[Another Doc](some_subdir/another_doc.md)`
100 | `_my_collection/some_subdir/another_doc.md` | `[Index](../../index.md)`
101 | `_my_collection/some_subdir/another_doc.md` | `[Some Doc](../some_doc.md)`
102 |
103 |
104 | ### Using the `rellinks` filter
105 |
106 | In addition to automatically converting relative links in your Markdown files, this plugin also provides a Liquid filter called `rellinks` that can be used to convert relative links in content that has already been processed by Jekyll's `markdownify` filter.
107 |
108 | This is especially useful when you have Markdown content in your front matter that you want to display with properly converted links.
109 |
110 | For example, if you have a page with a sidebar defined in the front matter:
111 |
112 | ```yaml
113 | ---
114 | title: My Page
115 | sidebar: |
116 | My page's sidebar **content**.
117 |
118 | Might have [a link somewhere](./other.md)
119 | ---
120 | ```
121 |
122 | You can use the `rellinks` filter in your template like this:
123 |
124 | ```liquid
125 |
128 | ```
129 |
130 | The `rellinks` filter will transform any relative links to Markdown files in the HTML output from the `markdownify` filter, converting them to their rendered equivalents.
131 |
132 | ### Disabling
133 |
134 | Even if the plugin is enabled (e.g., via the `:jekyll_plugins` group in your Gemfile) you can disable it by setting the `enabled` key to `false`.
135 |
--------------------------------------------------------------------------------
/docs/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6 |
7 | ## Our Standards
8 |
9 | Examples of behavior that contributes to creating a positive environment include:
10 |
11 | * Using welcoming and inclusive language
12 | * Being respectful of differing viewpoints and experiences
13 | * Gracefully accepting constructive criticism
14 | * Focusing on what is best for the community
15 | * Showing empathy towards other community members
16 |
17 | Examples of unacceptable behavior by participants include:
18 |
19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
20 | * Trolling, insulting/derogatory comments, and personal or political attacks
21 | * Public or private harassment
22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
23 | * Other conduct which could reasonably be considered inappropriate in a professional setting
24 |
25 | ## Our Responsibilities
26 |
27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28 |
29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30 |
31 | ## Scope
32 |
33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34 |
35 | ## Enforcement
36 |
37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at ben@balter.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38 |
39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40 |
41 | ## Attribution
42 |
43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44 |
45 | [homepage]: http://contributor-covenant.org
46 | [version]: http://contributor-covenant.org/version/1/4/
47 |
--------------------------------------------------------------------------------
/docs/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to Jekyll Relative Links
2 |
3 | Hi there! We're thrilled that you'd like to contribute to Jekyll Relative Links. Your help is essential for keeping it great.
4 |
5 | Jekyll Relative Links is an open source project supported by the efforts of an entire community and built one contribution at a time by users like you. We'd love for you to get involved. Whatever your level of skill or however much time you can give, your contribution is greatly appreciated. There are many ways to contribute, from writing tutorials or blog posts, improving the documentation, submitting bug reports and feature requests, helping other users by commenting on issues, or writing code which can be incorporated into Jekyll Relative Links itself.
6 |
7 | Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return, they should reciprocate that respect in addressing your issue, assessing changes, and helping you finalize your pull requests.
8 |
9 |
10 |
11 | ## How to report a bug
12 |
13 | Think you found a bug? Please check [the list of open issues](https://github.com/benbalter/jekyll-relative-links/issues) to see if your bug has already been reported. If it hasn't please [submit a new issue](https://github.com/benbalter/jekyll-relative-links/issues/new).
14 |
15 | Here are a few tips for writing *great* bug reports:
16 |
17 | * Describe the specific problem (e.g., "widget doesn't turn clockwise" versus "getting an error")
18 | * Include the steps to reproduce the bug, what you expected to happen, and what happened instead
19 | * Check that you are using the latest version of the project and its dependencies
20 | * Include what version of the project your using, as well as any relevant dependencies
21 | * Only include one bug per issue. If you have discovered two bugs, please file two issues
22 | * Include screenshots or screencasts whenever possible
23 | * Even if you don't know how to fix the bug, including a failing test may help others track it down
24 |
25 | **If you find a security vulnerability, do not open an issue. Please email ben@balter.com instead.**
26 |
27 | ## How to suggest a feature or enhancement
28 |
29 | If you find yourself wishing for a feature that doesn't exist in Jekyll Relative Links, you are probably not alone. There are bound to be others out there with similar needs. Many of the features that Jekyll Relative Links has today have been added because our users saw the need.
30 |
31 | Feature requests are welcome. But take a moment to find out whether your idea fits with the scope and goals of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Please provide as much detail and context as possible, including describing the problem you're trying to solve.
32 |
33 | [Open an issue](https://github.com/benbalter/jekyll-relative-links/issues/new) which describes the feature you would like to see, why you want it, how it should work, etc.
34 |
35 |
36 |
37 | ## Your first contribution
38 |
39 | We'd love for you to contribute to the project. Unsure where to begin contributing to Jekyll Relative Links? You can start by looking through these "good first issue" and "help wanted" issues:
40 |
41 | * [Good first issues](https://github.com/benbalter/jekyll-relative-links/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) - issues which should only require a few lines of code and a test or two
42 | * [Help wanted issues](https://github.com/benbalter/jekyll-relative-links/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) - issues which may be a bit more involved, but are specifically seeking community contributions
43 |
44 | *p.s. Feel free to ask for help; everyone is a beginner at first* :smiley_cat:
45 |
46 | ## How to propose changes
47 |
48 | Here's a few general guidelines for proposing changes:
49 |
50 | * If you are changing any user-facing functionality, please be sure to update the documentation
51 | * If you are adding a new behavior or changing an existing behavior, please be sure to update the corresponding test(s)
52 | * Each pull request should implement **one** feature or bug fix. If you want to add or fix more than one thing, submit more than one pull request
53 | * Do not commit changes to files that are irrelevant to your feature or bug fix
54 | * Don't bump the version number in your pull request (it will be bumped prior to release)
55 | * Write [a good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
56 |
57 | At a high level, [the process for proposing changes](https://guides.github.com/introduction/flow/) is:
58 |
59 | 1. [Fork](https://github.com/benbalter/jekyll-relative-links/fork) and clone the project
60 | 2. Configure and install the dependencies: `script/bootstrap`
61 | 3. Make sure the tests pass on your machine: `script/cibuild`
62 | 4. Create a descriptively named branch: `git checkout -b my-branch-name`
63 | 5. Make your change, add tests and documentation, and make sure the tests still pass
64 | 6. Push to your fork and [submit a pull request](https://github.com/benbalter/jekyll-relative-links/compare) describing your change
65 | 7. Pat your self on the back and wait for your pull request to be reviewed and merged
66 |
67 | **Interesting in submitting your first Pull Request?** It's easy! You can learn how from this *free* series [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github)
68 |
69 | ## Bootstrapping your local development environment
70 |
71 | `script/bootstrap`
72 |
73 | ## Running tests
74 |
75 | `script/cibuild`
76 |
77 | ## Code of conduct
78 |
79 | This project is governed by [the Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.
80 |
81 | ## Additional Resources
82 |
83 | * [Contributing to Open Source on GitHub](https://guides.github.com/activities/contributing-to-open-source/)
84 | * [Using Pull Requests](https://help.github.com/articles/using-pull-requests/)
85 | * [GitHub Help](https://help.github.com)
86 |
--------------------------------------------------------------------------------
/docs/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | To report a security vulnerability, please email [ben@balter.com](mailto:ben@balter.com).
4 |
--------------------------------------------------------------------------------
/jekyll-relative-links.gemspec:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | $LOAD_PATH.unshift File.expand_path("lib", __dir__)
4 | require "jekyll-relative-links/version"
5 |
6 | Gem::Specification.new do |s|
7 | s.name = "jekyll-relative-links"
8 | s.version = JekyllRelativeLinks::VERSION
9 | s.authors = ["Ben Balter"]
10 | s.email = ["ben.balter@github.com"]
11 | s.homepage = "https://github.com/benbalter/jekyll-relative-links"
12 | s.summary = "A Jekyll plugin to convert relative links to markdown files " \
13 | "to their rendered equivalents.\n"
14 |
15 | s.files = `git ls-files app lib`.split("\n")
16 | s.platform = Gem::Platform::RUBY
17 | s.require_paths = ["lib"]
18 | s.license = "MIT"
19 |
20 | s.add_dependency "jekyll", ">= 3.3", "< 5.0"
21 | s.add_development_dependency "kramdown-parser-gfm", "~> 1.0"
22 | s.add_development_dependency "rspec", "~> 3.5"
23 | s.add_development_dependency "rubocop", "~> 1.0"
24 | s.add_development_dependency "rubocop-factory_bot", "~> 2.22.0"
25 | s.add_development_dependency "rubocop-jekyll", "~> 0.10"
26 | s.add_development_dependency "rubocop-performance", "~> 1.5"
27 | s.add_development_dependency "rubocop-rspec", "~> 2.0.0"
28 | end
29 |
--------------------------------------------------------------------------------
/lib/jekyll-relative-links.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require "jekyll"
4 | require_relative "jekyll-relative-links/generator"
5 | require_relative "jekyll-relative-links/context"
6 | require_relative "jekyll-relative-links/filter"
7 |
8 | module JekyllRelativeLinks
9 | end
10 |
--------------------------------------------------------------------------------
/lib/jekyll-relative-links/context.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module JekyllRelativeLinks
4 | class Context
5 | attr_reader :site
6 |
7 | def initialize(site)
8 | @site = site
9 | end
10 |
11 | def registers
12 | { :site => site }
13 | end
14 | end
15 | end
16 |
--------------------------------------------------------------------------------
/lib/jekyll-relative-links/filter.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module JekyllRelativeLinks
4 | module Filter
5 | # This filter processes HTML content that's already been converted by the markdownify
6 | # filter and updates any relative links to markdown files to point to their HTML equivalents.
7 | # Usage: {{ content | markdownify | rellinks }}
8 | def rellinks(html)
9 | return html if html.nil? || html.empty?
10 | return html if @context.registers[:site].nil?
11 |
12 | process_links(html, @context.registers[:site])
13 | end
14 |
15 | def process_links(html, site)
16 | page = @context.registers[:page]
17 | url_base = page ? File.dirname(page["path"].to_s) : ""
18 |
19 | html.gsub(%r! e
66 | raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
67 | end
68 |
69 | private
70 |
71 | # Stores info on a Markdown Link (avoid rubocop's Metrics/ParameterLists warning)
72 | Link = Struct.new(:link_type, :text, :path, :fragment, :title)
73 |
74 | def link_parts(matches)
75 | last_inline = 5
76 | link_type = matches[2] ? :inline : :reference
77 | link_text = matches[link_type == :inline ? 2 : last_inline + 1]
78 | relative_path = matches[link_type == :inline ? 3 : last_inline + 2]
79 | fragment = matches[link_type == :inline ? 4 : last_inline + 3]
80 | title = matches[link_type == :inline ? 5 : last_inline + 4]
81 | Link.new(link_type, link_text, relative_path, fragment, title)
82 | end
83 |
84 | def context
85 | @context ||= JekyllRelativeLinks::Context.new(site)
86 | end
87 |
88 | def markdown_extension?(extension)
89 | markdown_converter.matches(extension)
90 | end
91 |
92 | def markdown_converter
93 | @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS)
94 | end
95 |
96 | def url_for_path(path)
97 | path = CGI.unescape(path)
98 | target = potential_targets.find { |p| p.relative_path.sub(%r!\A/!, "") == path }
99 | relative_url(target.url) if target&.url
100 | end
101 |
102 | def potential_targets
103 | @potential_targets ||= site.pages + site.static_files + site.docs_to_write
104 | end
105 |
106 | def path_from_root(relative_path, url_base)
107 | is_absolute = relative_path.start_with? "/"
108 |
109 | relative_path.sub!(%r!\A/!, "")
110 | base = is_absolute ? "" : url_base
111 | absolute_path = File.expand_path(relative_path, base)
112 | absolute_path.sub(%r!\A#{Regexp.escape(Dir.pwd)}/!, "")
113 | end
114 |
115 | # @param link [Link] A Link object describing the markdown link to make
116 | def replacement_text(link)
117 | link.path << link.fragment if link.fragment
118 |
119 | if link.link_type == :inline
120 | "[#{link.text}](#{link.path}#{link.title})"
121 | else
122 | "\n[#{link.text}]: #{link.path}#{link.title}"
123 | end
124 | end
125 |
126 | def absolute_url?(string)
127 | return false unless string
128 |
129 | Addressable::URI.parse(string).absolute?
130 | rescue Addressable::URI::InvalidURIError
131 | false
132 | end
133 |
134 | def fragment?(string)
135 | string&.start_with?("#")
136 | end
137 |
138 | def replaceable_link?(string)
139 | !fragment?(string) && !absolute_url?(string)
140 | end
141 |
142 | def option(key)
143 | config[CONFIG_KEY] && config[CONFIG_KEY][key]
144 | end
145 |
146 | def disabled?
147 | option(ENABLED_KEY) == false
148 | end
149 |
150 | def collections?
151 | option(COLLECTIONS_KEY) == true
152 | end
153 |
154 | def excluded?(document)
155 | return false unless option("exclude")
156 |
157 | entry_filter = if document.respond_to?(:collection)
158 | document.collection.entry_filter
159 | else
160 | global_entry_filter
161 | end
162 |
163 | entry_filter.glob_include?(option("exclude"), document.relative_path).tap do |excluded|
164 | Jekyll.logger.debug(LOG_KEY, "excluded #{document.relative_path}") if excluded
165 | end
166 | end
167 |
168 | def global_entry_filter
169 | @global_entry_filter ||= Jekyll::EntryFilter.new(site)
170 | end
171 |
172 | def replace_relative_links_excerpt!(document)
173 | return unless document.data["excerpt"] && !document.data["excerpt"].is_a?(String)
174 |
175 | document.data["excerpt"] = Jekyll::Excerpt.new(document)
176 | end
177 | end
178 | end
179 |
--------------------------------------------------------------------------------
/lib/jekyll-relative-links/version.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module JekyllRelativeLinks
4 | VERSION = "0.7.0"
5 | end
6 |
--------------------------------------------------------------------------------
/script/bootstrap:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | bundle install
4 |
--------------------------------------------------------------------------------
/script/cibuild:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | set -e
4 |
5 | bundle exec rspec
6 | bundle exec rubocop -S -D
7 | gem build jekyll-relative-links.gemspec
8 |
--------------------------------------------------------------------------------
/spec/fixtures/excerpt-in-frontmatter/_layouts/default.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | {{ page.title }}
5 |
6 |
7 | {{ page.excerpt }}
8 | {{ content }}
9 |
10 |
11 |
--------------------------------------------------------------------------------
/spec/fixtures/excerpt-in-frontmatter/another-page.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Another page
3 | ---
4 |
5 | This is another page
6 |
--------------------------------------------------------------------------------
/spec/fixtures/excerpt-in-frontmatter/page-with-excerpt.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Test page with excerpt field
3 | excerpt: This is a custom excerpt
4 | layout: default
5 | ---
6 |
7 | This is content with [a link](another-page.md)
8 |
--------------------------------------------------------------------------------
/spec/fixtures/site/_items/some-item.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Some item
5 |
6 | [Another Page](../another-page.md)
7 |
8 | [A post](../_posts/2016-01-01-test.md)
9 |
--------------------------------------------------------------------------------
/spec/fixtures/site/_items/some-subdir/another-item.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Another item
5 |
6 | [Another Page](../../another-page.md)
7 |
--------------------------------------------------------------------------------
/spec/fixtures/site/_items/static-file.md:
--------------------------------------------------------------------------------
1 | Static file
2 |
--------------------------------------------------------------------------------
/spec/fixtures/site/_posts/2016-01-01-test.md:
--------------------------------------------------------------------------------
1 | ---
2 | excerpt_separator:
3 | ---
4 |
5 | # Some post
6 |
7 | [Another Page](../another-page.md)
8 |
9 |
10 |
11 | [Page with permalink](../page-with-permalink.md)
12 |
13 | [Reference link][reference]
14 |
15 | [Reference with fragment][reference-with-fragment]
16 |
17 | [Reference fragment with brackets][reference-brackets]
18 |
19 | [Item](../_items/some-item.md)
20 |
21 | [reference]: ../another-page.md
22 |
23 | [reference-with-fragment]: ../another-page.md#foo
24 |
25 | [reference-brackets]: ../another-page.md#(bar)
26 |
--------------------------------------------------------------------------------
/spec/fixtures/site/another-page.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Another page
5 |
6 | [Page](page.md)
7 |
--------------------------------------------------------------------------------
/spec/fixtures/site/html-page.html:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | HTML Page
5 |
--------------------------------------------------------------------------------
/spec/fixtures/site/jekyll-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/benbalter/jekyll-relative-links/6cf68fcef569620dcd511c2d01fea9d56d482163/spec/fixtures/site/jekyll-logo.png
--------------------------------------------------------------------------------
/spec/fixtures/site/page with symbols?.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Page with Symbols?
5 |
6 | [Page](page.md)
7 |
--------------------------------------------------------------------------------
/spec/fixtures/site/page-with-permalink.md:
--------------------------------------------------------------------------------
1 | ---
2 | permalink: /page-with-permalink/
3 | ---
4 |
5 | # Page with permalink
6 |
--------------------------------------------------------------------------------
/spec/fixtures/site/page.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Page
5 |
6 | [Another Page](another-page.md)
7 |
8 | [Page with Symbols?](page%20with%20symbols%3F.md)
9 |
10 | [Page with permalink](page-with-permalink.md)
11 |
12 | [Page with leading slash](/another-page.md)
13 |
14 | [Page with leading slash multi level](/subdir/page.md)
15 |
16 | [Reference link][reference]
17 |
18 | [Reference with fragment][reference-with-fragment]
19 |
20 | [Reference fragment with brackets][reference-brackets]
21 |
22 | [Subdir Page](subdir/page.md)
23 |
24 | [HTML Page](html-page.html)
25 |
26 | [Ghost page](ghost-page.md)
27 |
28 | [Fragment](another-page.md#foo)
29 |
30 | [Fragment with permalink](page-with-permalink.md#foo)
31 |
32 | [A first fragment inline](another-page.md#foo) and [a second fragment in the same line](page-with-permalink.md#bar)
33 |
34 | [A indented reference][indented-reference]
35 |
36 | [A post](_posts/2016-01-01-test.md)
37 |
38 | [Another post](subdir/_posts/2016-01-01-test.md)
39 |
40 | [An item](_items/some-item.md)
41 |
42 | [Another item](_items/some-subdir/another-item.md)
43 |
44 | [[A link with square brackets]](another-page.md)
45 |
46 | [\[A link with escaped square brackets\]](/another-page.md)
47 |
48 | [A link with a title](another-page.md "This is a link with a \"title\"")
49 |
50 | [Quotes in url & title](/another-page.md#'apostrophe' 'Quotes in url & title')
51 |
52 | Content end
53 |
54 | [reference]: another-page.md
55 |
56 | [reference-with-fragment]: another-page.md#foo
57 |
58 | [reference-brackets]: another-page.md#(bar)
59 |
60 | [indented-reference]: another-page.md
61 |
62 | [reference-with-whitespace]: another-page.md
63 |
64 | [reference-with-title]: another-page.md "This is a reference with a title"
65 |
--------------------------------------------------------------------------------
/spec/fixtures/site/subdir/_posts/2016-01-01-test.md:
--------------------------------------------------------------------------------
1 | ---
2 | excerpt_separator:
3 | ---
4 |
5 | # Some post
6 |
7 | [Another Page](../../another-page.md)
8 |
9 | [Another Post](../../_posts/2016-01-01-test.md)
10 |
11 |
12 |
13 | Foobar
--------------------------------------------------------------------------------
/spec/fixtures/site/subdir/another-subdir-page.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Another subdir page
5 |
--------------------------------------------------------------------------------
/spec/fixtures/site/subdir/page.md:
--------------------------------------------------------------------------------
1 | ---
2 | ---
3 |
4 | # Subdir page
5 |
6 | [Another subdir page](another-subdir-page.md)
7 |
8 | [Relative subdir page](./another-subdir-page.md)
9 |
10 | [Dir traversal](../page.md)
11 |
12 | [leading slash starts from the root](/another-page.md)
13 |
14 | 
15 |
--------------------------------------------------------------------------------
/spec/jekyll-relative-links/generator_excerpt_frontmatter_spec.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | RSpec.describe JekyllRelativeLinks::Generator do
4 | let(:generator) { described_class.new(site.config) }
5 | let(:site) { fixture_site("excerpt-in-frontmatter") }
6 | let(:page) { page_by_path(site, "page-with-excerpt.md") }
7 |
8 | before do
9 | site.reset
10 | site.read
11 | generator.generate(site)
12 | end
13 |
14 | context "with excerpt in frontmatter" do
15 | it "doesn't raise an error" do
16 | expect { generator.generate(site) }.not_to raise_error
17 | end
18 |
19 | it "preserves the frontmatter excerpt as a string" do
20 | expect(page.data["excerpt"]).to be_a(String)
21 | expect(page.data["excerpt"]).to eq("This is a custom excerpt")
22 | end
23 |
24 | it "still converts relative links in content" do
25 | expect(page.content).to include("[a link](/another-page.html)")
26 | end
27 | end
28 | end
29 |
--------------------------------------------------------------------------------
/spec/jekyll_relative_links/context_spec.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | RSpec.describe JekyllRelativeLinks::Context do
4 | subject(:context) { described_class.new(site) }
5 |
6 | let(:site) { fixture_site("site") }
7 |
8 | it "stores the site" do
9 | expect(context.site).to eql(site)
10 | end
11 |
12 | it "returns the registers" do
13 | expect(context.registers).to have_key(:site)
14 | expect(context.registers[:site]).to eql(site)
15 | end
16 | end
17 |
--------------------------------------------------------------------------------
/spec/jekyll_relative_links/filter_spec.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | RSpec.describe JekyllRelativeLinks::Filter do
4 | let(:site) { fixture_site("site") }
5 | let(:page) { page_by_path(site, "page.md") }
6 | let(:subdir_page) { page_by_path(site, "subdir/page.md") }
7 | let(:filter) { make_filter }
8 |
9 | before do
10 | site.reset
11 | site.read
12 | site.liquid_renderer.reset
13 | end
14 |
15 | def make_filter
16 | @make_filter ||= begin
17 | filter_container = Class.new do
18 | include Jekyll::Filters::URLFilters
19 | include JekyllRelativeLinks::Filter
20 | end.new
21 |
22 | # Set up the context for the filter
23 | context = Liquid::Context.new({}, {}, {})
24 | context.registers[:site] = site
25 | context.registers[:page] = page
26 | filter_container.instance_variable_set(:@context, context)
27 |
28 | filter_container
29 | end
30 | end
31 |
32 | it "handles basic markdown links" do
33 | html = "Link
"
34 | expected = "Link
"
35 | expect(filter.rellinks(html)).to eq(expected)
36 | end
37 |
38 | it "handles links with fragments" do
39 | html = "Link with fragment
"
40 | expected = "Link with fragment
"
41 | expect(filter.rellinks(html)).to eq(expected)
42 | end
43 |
44 | it "doesn't affect links to non-markdown files" do
45 | html = "Image
"
46 | expect(filter.rellinks(html)).to eq(html)
47 | end
48 |
49 | it "doesn't affect absolute URLs" do
50 | html = "External
"
51 | expect(filter.rellinks(html)).to eq(html)
52 | end
53 |
54 | it "handles links from subdirectories" do
55 | set_subdir_context
56 | html = "Link in subdir
"
57 | expected = "Link in subdir
"
58 | expect(filter.rellinks(html)).to eq(expected)
59 | end
60 |
61 | it "doesn't modify invalid links" do
62 | html = "Ghost
"
63 | expect(filter.rellinks(html)).to eq(html)
64 | end
65 |
66 | private
67 |
68 | def set_subdir_context
69 | context = Liquid::Context.new({}, {}, {})
70 | context.registers[:site] = site
71 | context.registers[:page] = subdir_page
72 | filter.instance_variable_set(:@context, context)
73 | end
74 | end
75 |
--------------------------------------------------------------------------------
/spec/jekyll_relative_links/generator_spec.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | RSpec.describe JekyllRelativeLinks::Generator do
4 | subject(:generator) { described_class.new(site.config) }
5 |
6 | let(:site_config) do
7 | overrides["relative_links"] = plugin_config if plugin_config
8 | overrides
9 | end
10 | let(:overrides) { {} }
11 | let(:plugin_config) { nil }
12 | let(:site) { fixture_site("site", site_config) }
13 | let(:page) { page_by_path(site, "page.md") }
14 | let(:html_page) { page_by_path(site, "html-page.html") }
15 | let(:another_page) { page_by_path(site, "another-page.md") }
16 | let(:subdir_page) { page_by_path(site, "subdir/page.md") }
17 | let(:post) { doc_by_path(site, "_posts/2016-01-01-test.md") }
18 | let(:subdir_post) { doc_by_path(site, "subdir/_posts/2016-01-01-test.md") }
19 | let(:item) { doc_by_path(site, "_items/some-item.md") }
20 | let(:item_two) { doc_by_path(site, "_items/some-subdir/another-item.md") }
21 |
22 | before do
23 | site.reset
24 | site.read
25 | end
26 |
27 | it "saves the config" do
28 | expect(generator.config).to eql(site.config)
29 | end
30 |
31 | context "when detecting markdown" do
32 | before { generator.instance_variable_set :@site, site }
33 |
34 | it "knows when an extension is markdown" do
35 | expect(generator.send(:markdown_extension?, ".md")).to be(true)
36 | end
37 |
38 | it "knows when an extension isn't markdown" do
39 | expect(generator.send(:markdown_extension?, ".html")).to be(false)
40 | end
41 |
42 | it "knows the markdown converter" do
43 | expect(generator.send(:markdown_converter)).to be_a(Jekyll::Converters::Markdown)
44 | end
45 | end
46 |
47 | context "when generating" do
48 | before { generator.generate(site) }
49 |
50 | it "converts relative links" do
51 | expect(page.content).to include("[Another Page](/another-page.html)")
52 | end
53 |
54 | it "converts relative links with symbols" do
55 | expect(page.content).to include("[Page with Symbols?](/page%20with%20symbols?.html)")
56 | end
57 |
58 | it "converts relative links with permalinks" do
59 | expect(page.content).to include("[Page with permalink](/page-with-permalink/)")
60 | end
61 |
62 | it "converts relative links with leading slashes" do
63 | expect(page.content).to include("[Page with leading slash](/another-page.html)")
64 | end
65 |
66 | it "converts relative links with leading slashes in sub dir" do
67 | expect(page.content).to include("[Page with leading slash multi level](/subdir/page.html)")
68 | end
69 |
70 | it "converts pages in sub-directories" do
71 | expect(page.content).to include("[Subdir Page](/subdir/page.html)")
72 | end
73 |
74 | it "handles links within subdirectories" do
75 | expected = "[Another subdir page](/subdir/another-subdir-page.html)"
76 | expect(subdir_page.content).to include(expected)
77 | end
78 |
79 | it "handles relative links within subdirectories" do
80 | expected = "[Relative subdir page](/subdir/another-subdir-page.html)"
81 | expect(subdir_page.content).to include(expected)
82 | end
83 |
84 | it "handles directory traversal" do
85 | expect(subdir_page.content).to include("[Dir traversal](/page.html)")
86 | end
87 |
88 | it "handles paths from the root" do
89 | expected = "[leading slash starts from the root](/another-page.html)"
90 | expect(subdir_page.content).to include(expected)
91 | end
92 |
93 | it "Handles HTML pages" do
94 | expect(page.content).to include("[HTML Page](/html-page.html)")
95 | end
96 |
97 | it "doesn't mangle invalid pages" do
98 | expect(page.content).to include("[Ghost page](ghost-page.md)")
99 | end
100 |
101 | it "handles links with nested square brackets" do
102 | expected = "[[A link with square brackets]](/another-page.html)"
103 | expect(page.content).to include(expected)
104 | end
105 |
106 | it "handles links with escaped nested square brackets" do
107 | expected = "[\\[A link with escaped square brackets\\]](/another-page.html)"
108 | expect(page.content).to include(expected)
109 | end
110 |
111 | it "handles links with a title" do
112 | expected = "[A link with a title](/another-page.html \"This is a link with a \\\"title\\\"\")"
113 | expect(page.content).to include(expected)
114 | end
115 |
116 | it "handles links with quotes in url fragment and title" do
117 | # single_quotes are valid in urls
118 | expected = "[Quotes in url & title](/another-page.html#'apostrophe' 'Quotes in url & title')"
119 | expect(page.content).to include(expected)
120 | end
121 |
122 | context "with reference links" do
123 | it "handles reference links" do
124 | expect(page.content).to include("[reference]: /another-page.html")
125 | end
126 |
127 | it "handles indented reference links" do
128 | expect(page.content).to include("[indented-reference]: /another-page.html")
129 | end
130 |
131 | it "handles reference links with trailing whitespace" do
132 | expected = "[reference-with-whitespace]: /another-page.html"
133 | expect(page.content).to include(expected)
134 | end
135 |
136 | it "leaves newlines intact" do
137 | expected = "\n\nContent end\n\n[reference]: /another-page.html\n\n"
138 | expect(page.content).to include(expected)
139 | end
140 |
141 | it "handles reference links with titles" do
142 | expected = "[reference-with-title]: /another-page.html \"This is a reference with a title\""
143 | expect(page.content).to include(expected)
144 | end
145 | end
146 |
147 | context "with a baseurl" do
148 | let(:overrides) { { "baseurl" => "/foo" } }
149 |
150 | it "converts relative links" do
151 | expect(page.content).to include("[Another Page](/foo/another-page.html)")
152 | end
153 |
154 | it "handles links within subdirectories" do
155 | expected = "[Another subdir page](/foo/subdir/another-subdir-page.html)"
156 | expect(subdir_page.content).to include(expected)
157 | end
158 |
159 | it "handles relative links within subdirectories" do
160 | expected = "[Relative subdir page](/foo/subdir/another-subdir-page.html)"
161 | expect(subdir_page.content).to include(expected)
162 | end
163 |
164 | it "handles directory traversal" do
165 | expect(subdir_page.content).to include("[Dir traversal](/foo/page.html)")
166 | end
167 | end
168 |
169 | context "with a non-standard permalink structure" do
170 | let(:overrides) { { "permalink" => "/:year/:month/:title:output_ext" } }
171 |
172 | it "includes the extension" do
173 | expect(page.content).to include("[Another Page](/another-page.html)")
174 | end
175 | end
176 |
177 | context "when linking to page fragments" do
178 | it "converts relative links" do
179 | expect(page.content).to include("[Fragment](/another-page.html#foo)")
180 | end
181 |
182 | it "converts relative links with permalinks" do
183 | expected = "[Fragment with permalink](/page-with-permalink/#foo)"
184 | expect(page.content).to include(expected)
185 | end
186 |
187 | it "converts reference links" do
188 | expected = "[reference-with-fragment]: /another-page.html#foo"
189 | expect(page.content).to include(expected)
190 | end
191 |
192 | it "converts reference links with brackets in fragment" do
193 | expected = "[reference-brackets]: /another-page.html#(bar)"
194 | expect(page.content).to include(expected)
195 | end
196 |
197 | it "converts multiple fragments in the same line" do
198 | expected_fst = "[A first fragment inline](/another-page.html#foo)"
199 | expected_snd = "[a second fragment in the same line](/page-with-permalink/#bar)"
200 | expect(page.content).to include(expected_fst)
201 | expect(page.content).to include(expected_snd)
202 | end
203 | end
204 |
205 | context "with images" do
206 | it "handles images" do
207 | expect(subdir_page.content).to include("")
208 | end
209 | end
210 |
211 | context "when disabled" do
212 | let(:plugin_config) { { "enabled" => false } }
213 |
214 | it "does not process pages when disabled" do
215 | expect(page.content).to include("[Another Page](another-page.md)")
216 | end
217 | end
218 |
219 | context "with collections" do
220 | let(:plugin_config) { { "collections" => true } }
221 | let(:overrides) do
222 | {
223 | "collections" => {
224 | "items" => {
225 | "permalink" => "/items/:name/",
226 | "output" => true,
227 | },
228 | },
229 | }
230 | end
231 |
232 | it "converts relative links from pages to posts" do
233 | expect(page.content).to include("[A post](/2016/01/01/test.html)")
234 | end
235 |
236 | it "converts relative links from posts to pages" do
237 | expect(post.content).to include("[Another Page](/another-page.html)")
238 | end
239 |
240 | it "converts relative links from posts to pages in the excerpt" do
241 | expect(post.excerpt.content).to include("[Another Page](/another-page.html)")
242 | end
243 |
244 | it "converts relative links with permalinks from posts to pages " do
245 | expect(post.content).to include("[Page with permalink](/page-with-permalink/)")
246 | end
247 |
248 | it "handles reference links from posts to pages" do
249 | expect(post.content).to include("[reference]: /another-page.html")
250 | end
251 |
252 | it "converts reference links" do
253 | expected = "[reference-with-fragment]: /another-page.html#foo"
254 | expect(post.content).to include(expected)
255 | end
256 |
257 | it "converts reference links with brackets in fragment" do
258 | expected = "[reference-brackets]: /another-page.html#(bar)"
259 | expect(post.content).to include(expected)
260 | end
261 |
262 | context "with posts in subdirs" do
263 | it "converts relative links from pages to posts" do
264 | expect(page.content).to include("[Another post](/subdir/2016/01/01/test.html)")
265 | end
266 |
267 | it "converts relative links from posts to pages" do
268 | expect(subdir_post.content).to include("[Another Page](/another-page.html)")
269 | end
270 |
271 | it "converts relative links from posts to posts" do
272 | expect(subdir_post.content).to include("[Another Post](/2016/01/01/test.html)")
273 | end
274 |
275 | it "converts relative links from posts to posts in the excerpt" do
276 | expect(subdir_post.excerpt.content).to include("[Another Post](/2016/01/01/test.html)")
277 | end
278 | end
279 |
280 | context "with items (with output)" do
281 | it "converts relative links from pages to items" do
282 | expect(page.content).to include("[An item](/items/some-item/)")
283 | expect(page.content).to include("[Another item](/items/another-item/)")
284 | end
285 |
286 | it "converts relative links from items to pages" do
287 | expect(item.content).to include("[Another Page](/another-page.html)")
288 | expect(item_two.content).to include("[Another Page](/another-page.html)")
289 | end
290 |
291 | it "converts relative links from posts to items" do
292 | expect(post.content).to include("[Item](/items/some-item/)")
293 | end
294 |
295 | it "converts relative links from items to posts" do
296 | expect(item.content).to include("[A post](/2016/01/01/test.html)")
297 | end
298 | end
299 |
300 | context "with excludes" do
301 | let(:excludes) do
302 | [
303 | "another-page.md",
304 | "_posts/2016-01-01-test.md",
305 | "_items/some-subdir/another-item.md",
306 | ]
307 | end
308 | let(:plugin_config) { { "collections" => true, "exclude" => excludes } }
309 |
310 | context "with pages" do
311 | it "includes included pages" do
312 | expect(page.content).to include("[Another Page](/another-page.html)")
313 | end
314 |
315 | it "excludes excluded pages" do
316 | expect(another_page.content).to include("[Page](page.md)")
317 | end
318 | end
319 |
320 | context "with posts" do
321 | it "includes included posts" do
322 | expect(subdir_post.content).to include("[Another Page](/another-page.html)")
323 | end
324 |
325 | it "excludes excluded posts" do
326 | expect(post.content).to include("[Another Page](../another-page.md)")
327 | end
328 | end
329 |
330 | context "with collections" do
331 | it "includes included documents" do
332 | expect(item.content).to include("[Another Page](/another-page.html)")
333 | end
334 |
335 | it "excludes excluded documents" do
336 | expect(item_two.content).to include("[Another Page](../../another-page.md)")
337 | end
338 | end
339 | end
340 | end
341 | end
342 |
343 | context "with a page without content" do
344 | before { page_by_path(site, "page.md").content = nil }
345 |
346 | it "doesn't error out" do
347 | expect { generator.generate(site) }.not_to raise_error
348 | end
349 | end
350 | end
351 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require_relative "../lib/jekyll-relative-links"
4 |
5 | RSpec.configure do |config|
6 | config.expect_with :rspec do |expectations|
7 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true
8 | end
9 |
10 | config.mock_with :rspec do |mocks|
11 | mocks.verify_partial_doubles = true
12 | end
13 |
14 | config.filter_run_when_matching :focus
15 | config.example_status_persistence_file_path = "spec/examples.txt"
16 | config.disable_monkey_patching!
17 | config.default_formatter = "doc" if config.files_to_run.one?
18 | config.order = :random
19 | Kernel.srand config.seed
20 | end
21 |
22 | Jekyll.logger.adjust_verbosity(:quiet => true)
23 |
24 | def fixture_path(fixture)
25 | File.expand_path "./fixtures/#{fixture}", File.dirname(__FILE__)
26 | end
27 |
28 | def fixture_site(fixture, overrides = {})
29 | default_config = { "source" => fixture_path(fixture) }
30 | config = Jekyll::Utils.deep_merge_hashes(default_config, overrides)
31 | config = Jekyll.configuration(config)
32 | Jekyll::Site.new(config)
33 | end
34 |
35 | def page_by_path(site, path)
36 | site.pages.find { |p| p.path == path }
37 | end
38 |
39 | def doc_by_path(site, path)
40 | site.documents.find { |p| p.relative_path == path }
41 | end
42 |
--------------------------------------------------------------------------------