Skip to content

Commit 653f112

Browse files
tastyteatastytea
authored andcommitted
CB/ui: add styling for asciidoc classes (!44)
This adds a few asciidoctor classes to the sanitizer and adds styling for them. - support for making elements `float` - support for [tables without borders](https://codeberg.org/tastytea/asciidoctor-rendering-test/src/branch/main/README.adoc#_with_gridnone) - make [TOC-title](https://codeberg.org/tastytea/asciidoctor-rendering-test/src/branch/main/README.adoc) bold - make [admonition blocks](https://codeberg.org/tastytea/asciidoctor-rendering-test/src/branch/main/README.adoc#_admonitions) look nice I tried adding more border-styles for tables, but clashed with existing rules and couldn't figure out how to solve it. I don't think it's that important for this use-case so i didn't try too hard. 😊 Co-authored-by: tastytea <[email protected]> Reviewed-on: https://codeberg.org/Codeberg/gitea/pulls/44 Co-authored-by: tastytea <[email protected]> Co-committed-by: tastytea <[email protected]>
1 parent 6ae81ef commit 653f112

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

modules/markup/sanitizer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ func createDefaultPolicy() *bluemonday.Policy {
9494
// Allow 'color' and 'background-color' properties for the style attribute on text elements.
9595
policy.AllowStyles("color", "background-color").OnElements("span", "p")
9696

97+
// AsciiDoc: Allow classes for float-direction
98+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`(^| )(text-)?(left|center|right)$`)).OnElements("div")
99+
100+
// AsciiDoc: Allow classes for admonition blocks
101+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^admonitionblock (note|tip|important|caution|warning)( |$)`)).OnElements("div")
102+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^title$`)).OnElements("div")
103+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^(icon|content)$`)).OnElements("td")
104+
105+
// AsciiDoc: Allow classes for table styling: borders and horizontal alignment
106+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^tableblock frame-(all|ends|sides|none) grid-(all|rows|cols|none)( |$)`)).OnElements("table")
107+
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^tableblock halign-(left|center|right)( |$)`)).OnElements("th", "td")
108+
97109
// Allow generally safe attributes
98110
generalSafeAttrs := []string{
99111
"abbr", "accept", "accept-charset",

web_src/less/markup/content.less

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,83 @@
558558
border-top-left-radius: 0 !important;
559559
border-top-right-radius: 0 !important;
560560
}
561+
562+
.asciidoc {
563+
.left {
564+
float: left;
565+
}
566+
567+
.right {
568+
float: right
569+
}
570+
571+
.text-left,
572+
.halign-left {
573+
text-align: left;
574+
}
575+
576+
.text-center,
577+
.halign-center {
578+
text-align: center;
579+
}
580+
581+
.text-right,
582+
.halign-right {
583+
text-align: right;
584+
}
585+
586+
table {
587+
&.frame-none {
588+
border-width: 0 !important;
589+
}
590+
591+
&.grid-none {
592+
tr,
593+
th,
594+
td {
595+
border-width: 0 !important;
596+
}
597+
}
598+
}
599+
600+
#user-content-toctitle {
601+
font-weight: bold;
602+
}
603+
604+
.admonitionblock {
605+
> table {
606+
border: 0 !important;
607+
border-radius: 0.2em;
608+
609+
tr,
610+
td {
611+
border: 0 !important;
612+
}
613+
614+
.icon {
615+
font-weight: bold;
616+
border-right: 0.1em solid !important;
617+
vertical-align: top;
618+
}
619+
620+
.content {
621+
background-color: var(--color-body);
622+
}
623+
}
624+
625+
.admonition-colours() {
626+
tip: var(--color-green);
627+
note: var(--color-blue);
628+
important: var(--color-red);
629+
caution: var(--color-yellow);
630+
warning: var(--color-orange);
631+
}
632+
633+
each(.admonition-colours(), .(@colour, @class) {
634+
&.@{class} .icon {
635+
color: @colour;
636+
background: linear-gradient(to left, @colour, transparent 0.5em);
637+
}
638+
})
639+
}
640+
}

0 commit comments

Comments
 (0)