-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update how tags are rendered to allow for custom styling (#1179)
* Updated how tags are rendered to allow for custom styling via space-styles * Wrapped the existing span for a tag with an anchor element <a> to improve ux for screen readers * Added data-tag-name attribute to tags so that given a tag `#my-tag` it will have the attribute `data-tag-name="my-tag"` added to the new `<a>` element wrapper allowing for end users to target tags for styling with a css selector such as `.sb-hashtag[data-tag-name="my-tag"]{...}` * Updated Docs
- Loading branch information
1 parent
a56db78
commit 2020e85
Showing
9 changed files
with
126 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { syntaxTree } from "@codemirror/language"; | ||
import { Decoration } from "@codemirror/view"; | ||
import { decoratorStateField } from "./util.ts"; | ||
import * as Constants from "../../plugs/index/constants.ts"; | ||
import { extractHashtag } from "../../plug-api/lib/tags.ts"; | ||
|
||
export function hashtagPlugin() { | ||
return decoratorStateField((state) => { | ||
const widgets: any[] = []; | ||
|
||
syntaxTree(state).iterate({ | ||
enter: ({ type, from, to }) => { | ||
if (type.name !== "Hashtag") { | ||
return; | ||
} | ||
|
||
const tag = state.sliceDoc(from, to); | ||
|
||
if (tag.length === 1) { | ||
// Invalid Hashtag, a length of 1 means its just # | ||
return; | ||
} | ||
|
||
const tagName = extractHashtag(tag); | ||
|
||
// Wrap the tag in html anchor element | ||
widgets.push( | ||
Decoration.mark({ | ||
tagName: "a", | ||
class: "sb-hashtag", | ||
attributes: { | ||
href: `/${Constants.tagPrefix}${tagName}`, | ||
rel: "tag", | ||
"data-tag-name": tagName, | ||
}, | ||
}).range(from, to), | ||
); | ||
}, | ||
}); | ||
|
||
return Decoration.set(widgets, true); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters