Commit e59de28e authored by Jouke Witteveen's avatar Jouke Witteveen

Set AsciiDoc docname attribute using the requested path

parent 2ca2e390
---
title: Support Asciidoc docname attribute
merge_request: 22313
author: Jouke Witteveen
type: added
......@@ -91,6 +91,8 @@ stem:[sqrt(4) = 2]
### Attributes
**User-defined attributes**
```asciidoc
// define attributes in the document header
:name: value
......@@ -104,6 +106,15 @@ C{pp} is not required, only Ruby.
Use a leading backslash to output a word enclosed in curly braces, like \{name}.
```
**Environment attributes**
GitLab sets the following environment attributes:
| Attribute | Description |
| :-------------- | :--------------------------------------------------------------------------------------------------------------------- |
| `docname` | Root name of the source document (no leading path or file extension). |
| `outfilesuffix` | File extension corresponding to the backend output (defaults to `.adoc` to make inter-document cross references work). |
### Links
```asciidoc
......
......@@ -25,6 +25,19 @@ module Gitlab
'max-include-depth' => MAX_INCLUDE_DEPTH
}.freeze
def self.path_attrs(path)
return {} unless path
{
# Set an empty docname if the path is a directory
'docname' => if path[-1] == ::File::SEPARATOR
''
else
::File.basename(path, '.*')
end
}
end
# Public: Converts the provided Asciidoc markup into HTML.
#
# input - the source text in Asciidoc format
......@@ -35,9 +48,10 @@ module Gitlab
include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context)
end
extra_attrs = path_attrs(context[:requested_path])
asciidoc_opts = { safe: :secure,
backend: :gitlab_html5,
attributes: DEFAULT_ADOC_ATTRS,
attributes: DEFAULT_ADOC_ATTRS.merge(extra_attrs),
extensions: extensions }
context[:pipeline] = :ascii_doc
......
......@@ -46,6 +46,27 @@ module Gitlab
end
end
context "with requested path" do
input = <<~ADOC
Document name: {docname}.
ADOC
it "ignores {docname} when not available" do
expect(render(input, {})).to include(input.strip)
end
[
['/', '', 'root'],
['README', 'README', 'just a filename'],
['doc/api/', '', 'a directory'],
['doc/api/README.adoc', 'README', 'a complete path']
].each do |path, basename, desc|
it "sets {docname} for #{desc}" do
expect(render(input, { requested_path: path })).to include(": #{basename}.")
end
end
end
context "XSS" do
items = {
'link with extra attribute' => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment